-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseiga_client.py
87 lines (79 loc) · 2.63 KB
/
seiga_client.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
76
77
78
79
80
81
82
83
84
85
86
87
from .NicoSeigaApi.nicoseiga import NicoSeiga
import requests
import json
class SeigaGetter():
def __init__(self, authFile="seiga_auth.json"):
self.cl = NicoSeiga(
filename=authFile
)
def validateText(self, text):
ngWords = [
"{",
"}",
"[",
"]",
"request",
"config",
"<script>",
"</script>",
"class",
"import",
"__globals__",
"__getitem__",
"self"
]
for g in ngWords:
text = text.replace(g, "")
return text
def getIllustSourceUrl(self, seiga_address):
try:
if "https://seiga.nicovideo.jp/seiga/" not in seiga_address:
raise Exception()
hasParam = seiga_address.find("?")
if hasParam != -1:
seiga_address = seiga_address[:hasParam]
seiga_id = int(seiga_address.split("im")[-1])
sourceUrl = self.cl.getIllustSourceUrl(seiga_id)
except Exception as e:
raise Exception("Fetch failed")
return sourceUrl
def downloadIllust(self, illust_src, path):
resp = requests.get(illust_src)
if resp.status_code != 200:
raise Exception("HTTP Error")
with open(path, "wb") as f:
f.write(resp.content)
return True
def getSeiga(self, seiga_address):
try:
if "https://seiga.nicovideo.jp/seiga/" not in seiga_address:
raise Exception()
hasParam = seiga_address.find("?")
if hasParam != -1:
seiga_address = seiga_address[:hasParam]
seiga_id = int(seiga_address.split("im")[-1])
seiga = self.cl.getIllustDetail(seiga_id)
except Exception as e:
return {}
resp = {
'illust': {
'type': 'nico_seiga',
'id': seiga_id,
'title': seiga['title'],
'caption': seiga['description'],
'imgs': [{"thumb_src":seiga['thumbnail']}],
'tags': seiga['tags'],
'source': seiga_address,
'artist': seiga["user"]["name"],
'R18': False
},
'user': {
'id': seiga["user"]["id"],
'name': seiga["user"]["name"],
'profile_image': seiga['user']['thumbnail']
}
}
return resp
if __name__ == "__main__":
sg = SeigaGetter("seiga_auth.json")
print(sg.getSeiga("https://twitter.com/usagicandy_taku/status/1246029844590149633"))