forked from moepoi/moepoi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
favorites_updater.py
129 lines (120 loc) · 2.93 KB
/
favorites_updater.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
from python_graphql_client import GraphqlClient
import pathlib
import re
import os
root = pathlib.Path(__file__).parent.resolve()
client = GraphqlClient(endpoint="https://graphql.anilist.co")
TOKEN = os.environ.get("ANILIST_TOKEN", "")
def replace_chunk(content, marker, chunk, inline=False):
r = re.compile(
r"<!\-\- {} starts \-\->.*<!\-\- {} ends \-\->".format(marker, marker),
re.DOTALL,
)
if not inline:
chunk = "\n{}\n".format(chunk)
chunk = "<!-- {} starts -->{}<!-- {} ends -->".format(marker, chunk, marker)
return r.sub(chunk, content)
def make_query():
return """
query($favPage: Int) {
Viewer {
favourites {
anime(page: $favPage) {
nodes {
title {
romaji
}
siteUrl
}
pageInfo {
total
currentPage
lastPage
perPage
hasNextPage
}
}
manga(page: $favPage) {
nodes {
title {
romaji
}
siteUrl
}
pageInfo {
total
currentPage
lastPage
perPage
hasNextPage
}
}
characters(page: $favPage) {
nodes {
name {
full
}
siteUrl
}
pageInfo {
total
currentPage
lastPage
perPage
hasNextPage
}
}
}
}
}
"""
def fetch_favorites(oauth_token, types='anime'):
results = []
variables = {"favPage": 1}
data = client.execute(
query=make_query(),
variables=variables,
headers={"Authorization": "Bearer {}".format(oauth_token)},
)
for x in data['data']['Viewer']['favourites'][types]['nodes']:
results.append(
{
'title': x['title']['romaji'] if types != 'characters' else x['name']['full'],
'url': x['siteUrl']
}
)
return results
if __name__ == "__main__":
readme = root / "README.md"
readme_contents = readme.open().read()
# Favorites Anime
data = fetch_favorites(TOKEN, types='anime')
res = "\n".join(
[
"* [{title}]({url})".format(**x)
for x in data
]
)
print (res)
rewritten = replace_chunk(readme_contents, "favorites_anime", res)
# Favorites Manga
data = fetch_favorites(TOKEN, types='manga')
res = "\n".join(
[
"* [{title}]({url})".format(**x)
for x in data
]
)
print (res)
rewritten = replace_chunk(readme_contents, "favorites_manga", res)
# Favorites Characters
data = fetch_favorites(TOKEN, types='characters')
res = "\n".join(
[
"* [{title}]({url})".format(**x)
for x in data
]
)
print (res)
rewritten = replace_chunk(readme_contents, "favorites_characters", res)
readme.open("w").write(rewritten)