forked from lzc256/misskey-telegram-forwarder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
misskey.py
59 lines (54 loc) · 1.93 KB
/
misskey.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
import requests
import json
from dateutil import parser
from datetime import datetime
from typing import List, Dict
class Note:
def __init__(self, createdAt: datetime, text: str, replyId: str, cw: str, files: List[Dict[str, str]]):
self.createdAt = createdAt
self.text = text
self.replyId = replyId
self.cw = cw
self.files = files
def _get_raw_notes(site: str, user_id: str) -> List[Dict]:
req_body = {
"userId": user_id,
"limit": 10
}
req_header = {
"User-Agent": "MisskeyTelegramForwarder",
'Content-type': 'application/json',
'Accept': '*/*'
}
res = requests.post(
url=f"{site}/api/users/notes",
headers=req_header,
data=json.dumps(req_body)
)
return json.loads(res.content)
def get_notes(site: str, user_id: str) -> List[Note]:
raw_notes = _get_raw_notes(site, user_id)
notes = []
for n in raw_notes:
note = Note(
createdAt=parser.parse(n["createdAt"]),
text=n.get("text", ""),
replyId=n.get("replyId", ""),
cw=n.get("cw", ""),
files=[{"type": f["type"], "url": f["url"]} for f in n.get("files", [])]
)
if "renote" in n:
renote = n["renote"]
note.text = (
f"<code>❀Title : </code><b>{renote['text']}</b>\n\n"
f"<code>❀Artist : </code><b><a href=\"https://t.me/ChuangBian/\">{renote['user']['name']}</a></b>\n\n"
f"► <b><a href=\"https://t.me/ChuangBian/5/\">ᴍɪꜱꜱᴋᴇʏ</a></b>"
)
if note.cw:
note.text = f"<tg-spoiler>{note.text}</tg-spoiler>"
note.files = [{"type": f["type"], "url": f["url"]} for f in renote.get("files", [])]
else:
if note.cw:
note.text = f"<tg-spoiler>{note.text}</tg-spoiler>"
notes.append(note)
return notes