-
Notifications
You must be signed in to change notification settings - Fork 0
/
MiAPI.py
49 lines (44 loc) · 1.16 KB
/
MiAPI.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import datetime
import json
import urllib.error
import urllib.request
import MiConfig
USER_AGENT = "mozilla/5.0"
def timeline(since_id):
data = {
"i": MiConfig.token,
"limit": 10
}
return call("notes/timeline", data)
def post(text):
url = "https://" + MiConfig.host + "/api/notes/create"
data = {
"i": MiConfig.token,
"text": text
}
call("notes/create", data)
return
def i():
data = {
"i": MiConfig.token
}
return call("i", data)
def call(api, data):
url = "https://" + MiConfig.host + "/api/" + api
headers = {
"Content-Type": "application/json",
"User-Agent": USER_AGENT
}
req = urllib.request.Request(url, data = json.dumps(data).encode("utf-8"), headers = headers, method = "POST")
res = urllib.request.urlopen(req)
body = res.read()
return json.loads(body)
def download(url, path):
headers = {
"User-Agent": USER_AGENT
}
req = urllib.request.Request(url, headers = headers)
with urllib.request.urlopen(req) as file_web:
with open(path, "wb") as file_local:
file_local.write(file_web.read())
return