forked from RickDB/PlexAniSync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraphql.py
183 lines (158 loc) · 4.92 KB
/
graphql.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# coding=utf-8
import collections
import json
import logging
import time
from typing import Any, Dict
import requests
logger = logging.getLogger("PlexAniSync")
ANILIST_ACCESS_TOKEN = ""
ANILIST_SKIP_UPDATE = False
def search_by_id(anilist_id: int):
query = """
query ($id: Int) {
media: Media (id: $id, type: ANIME) {
id
type
format
status
source
season
episodes
title {
romaji
english
native
}
synonyms
startDate {
year
}
endDate {
year
}
}
}
"""
variables = {"id": anilist_id}
response = send_graphql_request(query, variables)
return json.loads(response.content, object_hook=to_object)
def search_by_name(anilist_show_name: str):
query = """
query ($page: Int, $perPage: Int, $search: String) {
Page (page: $page, perPage: $perPage) {
pageInfo {
total
currentPage
lastPage
hasNextPage
perPage
}
media (search: $search, type: ANIME) {
id
type
format
status
source
season
episodes
title {
romaji
english
native
}
synonyms
startDate {
year
}
endDate {
year
}
}
}
}
"""
variables = {"search": anilist_show_name, "page": 1, "perPage": 50}
response = send_graphql_request(query, variables)
return json.loads(response.content, object_hook=to_object)
def fetch_user_list(username: str):
query = """
query ($username: String) {
MediaListCollection(userName: $username, type: ANIME) {
lists {
name
status
isCustomList
entries {
id
progress
status
repeat
media {
id
type
format
status
source
season
episodes
startDate {
year
}
endDate {
year
}
title {
romaji
english
native
}
synonyms
}
}
}
}
}
"""
variables = {"username": username}
response = send_graphql_request(query, variables)
return json.loads(response.content, object_hook=to_object)
def update_series(media_id: int, progress: int, status: str):
if ANILIST_SKIP_UPDATE:
logger.warning("[ANILIST] Skip update is enabled in settings so not updating this item")
return
query = """
mutation ($mediaId: Int, $status: MediaListStatus, $progress: Int) {
SaveMediaListEntry (mediaId: $mediaId, status: $status, progress: $progress) {
id
status,
progress
}
}
"""
variables = {"mediaId": media_id, "status": status, "progress": int(progress)}
send_graphql_request(query, variables)
def send_graphql_request(query: str, variables: Dict[str, Any]):
url = "https://graphql.anilist.co"
headers = {
"Authorization": "Bearer " + ANILIST_ACCESS_TOKEN,
"Accept": "application/json",
"Content-Type": "application/json",
}
while True:
response = requests.post(
url, headers=headers, json={"query": query, "variables": variables}
)
if response.status_code == 429:
wait_time = int(response.headers.get('retry-after', 0))
logger.warning(f"[ANILIST] Rate limit hit, waiting for {wait_time}s")
time.sleep(wait_time + 1)
else:
response.raise_for_status()
# wait a bit to not overload AniList API
time.sleep(0.20)
return response
def to_object(obj):
keys, values = zip(*obj.items())
# print(keys, values)
return collections.namedtuple("X", keys)(*values)