This repository has been archived by the owner on Aug 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathapi.py
75 lines (59 loc) · 2.43 KB
/
api.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import re
from aiohttp import ClientTimeout, ClientResponse
from aiohttp.formdata import FormData
from aiohttp_retry import RetryClient, ClientSession
class Request:
def __init__(self, *args, **kwargs):
self.client_session = ClientSession()
self.retry_client = RetryClient(client_session=self.client_session)
self.request = self.retry_client.request(*args, **kwargs)
async def __aenter__(self) -> ClientResponse:
return await self.request
async def __aexit__(self, exc_type, exc_val, exc_tb):
await self.client_session.close()
await self.retry_client.close()
def request(method, url, data=None, json=None):
if json is not None:
return Request(method, url, json=json, timeout=ClientTimeout(total=1000))
else:
return Request(method, url, data=data, timeout=ClientTimeout(total=1000))
class Memo:
def __init__(self, domain, openid):
self.domain = domain
self.openid = openid
self.url = f"{domain}api/memo?openId={openid}"
async def send_memo(self, content="", visibility="PRIVATE", res_id_list=None):
if res_id_list is None:
res_id_list = []
data = {
"content": content,
"visibility": visibility,
"resourceIdList": res_id_list,
}
tags = re.findall(r"#\S+", content)
if tags:
tag = Tag(self.domain, self.openid)
for t in tags:
t = t.replace("#", "")
await tag.create_tag(t)
async with request("POST", url=self.url, json=data) as resp:
assert resp.status == 200
resp_data = await resp.json()
return resp_data["data"]["id"]
class Resource:
def __init__(self, domain, openid):
self.url = f"{domain}api/resource/blob?openId={openid}"
async def create_res(self, file):
data = FormData()
data.add_field("file", file, filename="telegram-file.jpg", content_type="image/jpeg")
async with request("POST", url=self.url, data=data) as resp:
assert resp.status == 200
resp_data = await resp.json()
return resp_data["data"]["id"]
class Tag:
def __init__(self, domain, openid):
self.url = f"{domain}api/tag?openId={openid}"
async def create_tag(self, name):
data = {"name": name}
async with request("POST", url=self.url, json=data) as resp:
assert resp.status == 200