-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
246 lines (209 loc) · 9.81 KB
/
main.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import requests
from typing import List, Dict, Set
from colorama import Fore, init
import sys
import time
import os
init(autoreset=True)
class MangaDexSync:
def __init__(self):
self.anilist_api = 'https://graphql.anilist.co'
self.mangadex_token_url = 'https://auth.mangadex.org/realms/mangadex/protocol/openid-connect/token'
self.mangadex_base_url = 'https://api.mangadex.org'
self.username = os.getenv('MANGADEX_USERNAME')
self.password = os.getenv('MANGADEX_PASSWORD')
self.mangadex_client_id = os.getenv('MANGADEX_CLIENT_ID')
self.mangadex_client_secret = os.getenv('MANGADEX_CLIENT_SECRET')
self.access_token = None
self.refresh_token = None
self.mangadex_manga_cache: Dict[str, str] = {} # title -> id mapping
def _request(self, method: str, url: str, params: dict = None, data: dict = None, json: dict = None) -> requests.Response:
if not self.access_token and not self.authenticate():
raise Exception(Fore.RED + "Authentication failed")
headers = {'Authorization': f'Bearer {self.access_token}'}
response = requests.request(method, url, params=params, data=data, json=json, headers=headers)
if response.status_code == 401 and self.refresh_access_token():
headers['Authorization'] = f'Bearer {self.access_token}'
response = requests.request(method, url, params=params, data=data, json=json, headers=headers)
return response
def get_current_mangadex_list(self) -> Set[str]:
"""Fetch current manga list from MangaDex"""
manga_ids = set()
limit = 100
offset = 0
while True:
response = self._request('GET', f'{self.mangadex_base_url}/user/follows/manga',
params={'limit': limit, 'offset': offset})
if not response or response.status_code != 200:
break
data = response.json().get('data', [])
if not data:
break
for manga in data:
manga_id = manga.get('id')
titles = manga.get('attributes', {}).get('title', {})
if manga_id and titles:
manga_ids.add(manga_id)
# Cache the manga titles and IDs for later use
for title in titles.values():
if title:
self.mangadex_manga_cache[title.lower()] = manga_id
offset += limit
if len(data) < limit:
break
print(Fore.BLUE + f"Found {len(manga_ids)} manga in your MangaDex list")
return manga_ids
def authenticate(self) -> bool:
creds = {
"grant_type": "password",
"username": self.username,
"password": self.password,
"client_id": self.mangadex_client_id,
"client_secret": self.mangadex_client_secret
}
try:
response = requests.post(self.mangadex_token_url, data=creds)
if response.status_code == 200:
token_data = response.json()
self.access_token = token_data["access_token"]
self.refresh_token = token_data.get("refresh_token")
print(Fore.GREEN + "Authentication successful!")
return True
print(Fore.RED + f"Authentication failed: {response.text}")
return False
except Exception as e:
print(Fore.RED + f"Authentication error: {e}")
return False
def refresh_access_token(self) -> bool:
if not self.refresh_token:
return False
refresh_data = {
"grant_type": "refresh_token",
"refresh_token": self.refresh_token,
"client_id": self.mangadex_client_id,
"client_secret": self.mangadex_client_secret
}
try:
response = requests.post(self.mangadex_token_url, data=refresh_data)
if response.status_code == 200:
token_data = response.json()
self.access_token = token_data["access_token"]
self.refresh_token = token_data.get("refresh_token")
return True
return False
except Exception:
return False
def find_mangadex_manga(self, anilist_title: str) -> str:
# First check the cache
title_lower = anilist_title.lower()
if title_lower in self.mangadex_manga_cache:
return self.mangadex_manga_cache[title_lower]
search_strategies = [
lambda title: title,
lambda title: title.lower(),
lambda title: title.replace(':', ''),
lambda title: title.split(':')[0].strip()
]
for strategy in search_strategies:
modified_title = strategy(anilist_title)
params = {'title': modified_title, 'limit': 5}
response = self._request('GET', f'{self.mangadex_base_url}/manga', params=params)
if response and response.status_code == 200:
results = response.json().get('data', [])
if results:
manga_id = results[0]['id']
self.mangadex_manga_cache[title_lower] = manga_id
return manga_id
return None
def get_anilist_manga_list(self, username: str) -> List[dict]:
query = '''
query ($username: String) {
MediaListCollection(userName: $username, type: MANGA) {
lists {
entries {
media {
title { romaji english native }
id
}
status
progress
}
}
}
}
'''
variables = {'username': username}
response = requests.post(self.anilist_api, json={'query': query, 'variables': variables})
if response.status_code == 200:
data = response.json()
return [entry for list_group in data['data']['MediaListCollection']['lists'] for entry in list_group['entries']]
return []
def update_mangadex_reading_status(self, manga_id: str, status: str) -> bool:
status_mapping = {
'CURRENT': 'reading', 'COMPLETED': 'completed', 'PAUSED': 'on_hold',
'DROPPED': 'dropped', 'PLANNING': 'plan_to_read'
}
follow_response = self._request('POST', f'{self.mangadex_base_url}/manga/{manga_id}/follow')
if follow_response and follow_response.status_code == 200:
payload = {'status': status_mapping.get(status, 'reading')}
status_response = self._request('POST', f'{self.mangadex_base_url}/manga/{manga_id}/status', json=payload)
return status_response and status_response.status_code == 200
return False
def sync_manga_list(self, anilist_username: str):
print(Fore.YELLOW + "Fetching your current MangaDex list...")
current_mangadex_ids = self.get_current_mangadex_list()
print(Fore.YELLOW + "Fetching your AniList manga...")
anilist_manga = self.get_anilist_manga_list(anilist_username)
if not anilist_manga:
print(Fore.RED + "No AniList manga to sync")
return
total_manga = len(anilist_manga)
synced_manga = 0
skipped_manga = 0
failed_manga = []
for index, manga in enumerate(anilist_manga, 1):
title = manga['media']['title'].get('english') or manga['media']['title'].get('romaji') or manga['media']['title'].get('native')
print(Fore.YELLOW + f"Processing manga {index}/{total_manga}: {title}")
try:
mangadex_id = self.find_mangadex_manga(title)
if not mangadex_id:
print(Fore.RED + f"Could not find manga on MangaDex: {title}")
failed_manga.append(title)
continue
if mangadex_id in current_mangadex_ids:
print(Fore.BLUE + f"Skipping already followed manga: {title}")
skipped_manga += 1
continue
if self.update_mangadex_reading_status(mangadex_id, manga['status']):
synced_manga += 1
print(Fore.GREEN + f"Synced: {title}")
else:
failed_manga.append(title)
time.sleep(0.5) # Rate limiting
except Exception as e:
print(Fore.RED + f"Error processing {title}: {e}")
failed_manga.append(title)
print(Fore.YELLOW + f"\n--- Synchronization complete ---")
print(Fore.GREEN + f"Successfully synced: {synced_manga}/{total_manga}")
print(Fore.BLUE + f"Already in list (skipped): {skipped_manga}")
if failed_manga:
print(Fore.RED + "\nManga that failed to sync:")
for manga in failed_manga:
print(Fore.RED + f"- {manga}")
def main():
print(Fore.YELLOW + "--- MangaDex Sync ---")
try:
syncer = MangaDexSync()
if not all([syncer.username, syncer.password, syncer.mangadex_client_id, syncer.mangadex_client_secret]):
print(Fore.RED + "Error: Required environment variables are missing")
sys.exit(1)
anilist_username = os.getenv('ANILIST_USERNAME')
if not anilist_username:
print(Fore.RED + "Error: ANILIST_USERNAME environment variable is missing")
sys.exit(1)
syncer.sync_manga_list(anilist_username)
except Exception as e:
print(Fore.RED + f"An error occurred: {str(e)}")
sys.exit(1)
if __name__ == '__main__':
main()