-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmal_proxy.py
More file actions
501 lines (422 loc) · 18.6 KB
/
Copy pathmal_proxy.py
File metadata and controls
501 lines (422 loc) · 18.6 KB
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
"""
MAL Tools Proxy
===============
Tiny local server. Fetches anime/manga lists from MyAnimeList for any
username and returns clean JSON. Works around the browser CORS block.
Run:
python3 mal_proxy.py
Endpoints:
GET /ptw/<username> - Plan to Watch (anime)
GET /ptr/<username> - Plan to Read (manga)
GET /completed/<username> - Completed anime (with user score)
GET /anime/<id> - Anime details (Jikan v4 enrichment)
GET /manga/<id> - Manga details (Jikan v4 enrichment)
GET /profile/<username> - Jikan profile + statistics block
GET /enriched/<username> - Full completed anime list, each entry
enriched with studios/genres (cached on disk)
GET /enriched-manga/<user> - Full completed manga list, each entry
enriched with authors/serializations (cached)
"""
import json
import mimetypes
import os
import sys
import threading
import time
import urllib.parse
import urllib.request
import webbrowser
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
PORT = 8765
PAGE_SIZE = 300 # MAL's load.json returns up to 300 entries per page
# Where the static frontend lives (resolved relative to this file)
ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
# MAL status codes
STATUS_PTW = 6 # Plan to Watch
STATUS_COMPLETED = 2 # Completed (anime)
STATUS_PTR = 6 # Plan to Read (same code, different list endpoint)
USER_AGENT = (
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) "
"AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/124.0.0.0 Safari/537.36"
)
def _extract_genres(item):
"""Pull genres + themes + demographics into a single list."""
out = []
for key in ("genres", "demographics", "themes"):
for g in (item.get(key) or []):
if g.get("name"):
out.append({"id": g.get("id"), "name": g.get("name")})
return out
def _fetch_mal_list(list_kind: str, username: str, status: int):
"""
Fetch a paginated MAL list.
list_kind: 'animelist' or 'mangalist'
Returns the raw items as a list of dicts (MAL's payload format).
"""
out = []
offset = 0
while True:
url = (
f"https://myanimelist.net/{list_kind}/{urllib.parse.quote(username)}"
f"/load.json?status={status}&offset={offset}"
)
# Match curl's minimal request - MAL is strict on header set
headers = {"User-Agent": "curl/8.0.1", "Accept": "*/*"}
req = urllib.request.Request(url, headers=headers)
try:
with urllib.request.urlopen(req, timeout=15) as resp:
raw = resp.read()
except urllib.error.HTTPError as e:
body = e.read().decode("utf-8", errors="replace")[:500]
raise urllib.error.HTTPError(
e.url, e.code, f"{e.reason} | body: {body}", e.headers, None
)
data = json.loads(raw.decode("utf-8"))
if not isinstance(data, list):
raise ValueError("Unexpected MAL response (list may be private).")
out.extend(data)
if len(data) < PAGE_SIZE:
break
offset += PAGE_SIZE
return out
def fetch_ptw(username: str):
"""Plan to Watch (anime). Normalized output shape."""
items = _fetch_mal_list("animelist", username, STATUS_PTW)
return [{
"title": it.get("anime_title", "Unknown"),
"type": it.get("anime_media_type_string", "Unknown"),
"episodes": it.get("anime_num_episodes", 0) or 0,
"id": it.get("anime_id"),
"image": _clean_image(it.get("anime_image_path", "")),
"genres": _extract_genres(it),
"studios": [],
} for it in items]
def fetch_completed(username: str):
"""Completed anime, including the user's score for ranker use."""
items = _fetch_mal_list("animelist", username, STATUS_COMPLETED)
return [{
"title": it.get("anime_title", "Unknown"),
"type": it.get("anime_media_type_string", "Unknown"),
"episodes": it.get("anime_num_episodes", 0) or 0,
"id": it.get("anime_id"),
"image": _clean_image(it.get("anime_image_path", "")),
"genres": _extract_genres(it),
"studios": [],
# User's own score (0 = unscored)
"score": it.get("score", 0) or 0,
} for it in items]
def fetch_ptr(username: str):
"""Plan to Read (manga). Normalized output shape."""
items = _fetch_mal_list("mangalist", username, STATUS_PTR)
return [{
"title": it.get("manga_title", "Unknown"),
"type": it.get("manga_media_type_string", "Unknown"),
"chapters": it.get("manga_num_chapters", 0) or 0,
"volumes": it.get("manga_num_volumes", 0) or 0,
"id": it.get("manga_id"),
"image": _clean_image(it.get("manga_image_path", "")),
"genres": _extract_genres(it),
} for it in items]
def fetch_anime_details(anime_id: int):
"""Fetch a single anime's details via Jikan v4."""
url = f"https://api.jikan.moe/v4/anime/{anime_id}"
req = urllib.request.Request(url, headers={"User-Agent": USER_AGENT})
with urllib.request.urlopen(req, timeout=15) as resp:
data = json.loads(resp.read().decode("utf-8")).get("data") or {}
return {
"id": data.get("mal_id"),
"title": data.get("title"),
"synopsis": data.get("synopsis"),
"score": data.get("score"),
"year": data.get("year"),
"genres": [{"id": g["mal_id"], "name": g["name"]} for g in data.get("genres", [])],
"studios": [{"id": s["mal_id"], "name": s["name"]} for s in data.get("studios", [])],
"themes": [{"id": t["mal_id"], "name": t["name"]} for t in data.get("themes", [])],
"image": (data.get("images", {}).get("jpg", {}) or {}).get("large_image_url", ""),
}
def fetch_manga_details(manga_id: int):
"""Fetch a single manga's details via Jikan v4."""
url = f"https://api.jikan.moe/v4/manga/{manga_id}"
req = urllib.request.Request(url, headers={"User-Agent": USER_AGENT})
with urllib.request.urlopen(req, timeout=15) as resp:
data = json.loads(resp.read().decode("utf-8")).get("data") or {}
return {
"id": data.get("mal_id"),
"title": data.get("title"),
"synopsis": data.get("synopsis"),
"score": data.get("score"),
"published": (data.get("published", {}) or {}).get("string"),
"chapters": data.get("chapters"),
"volumes": data.get("volumes"),
"status": data.get("status"),
"genres": [{"id": g["mal_id"], "name": g["name"]} for g in data.get("genres", [])],
"themes": [{"id": t["mal_id"], "name": t["name"]} for t in data.get("themes", [])],
"authors": [{"id": a["mal_id"], "name": a["name"]} for a in data.get("authors", [])],
"serializations": [{"id": s["mal_id"], "name": s["name"]} for s in data.get("serializations", [])],
"image": (data.get("images", {}).get("jpg", {}) or {}).get("large_image_url", ""),
}
def _clean_image(path: str) -> str:
if not path:
return ""
# Remove /r/192x272/ style resize prefix
import re
return re.sub(r"/r/\d+x\d+/", "/", path)
# ─── Jikan rate limiter ────────────────────────────────────────────────────
# Jikan allows ~3 requests per second. We serialize the gate across threads
# so concurrent enrichment requests from the dashboard don't burst Jikan.
_JIKAN_LOCK = threading.Lock()
_JIKAN_LAST = [0.0] # mutable container so we can update under lock
_JIKAN_GAP_SEC = 0.34 # ~3 req/s
def _jikan_throttle():
with _JIKAN_LOCK:
now = time.monotonic()
wait = _JIKAN_GAP_SEC - (now - _JIKAN_LAST[0])
if wait > 0:
time.sleep(wait)
_JIKAN_LAST[0] = time.monotonic()
# ─── Disk cache for Jikan details ──────────────────────────────────────────
# Cuts repeat enrichment from "60s for 150 anime" to instant.
CACHE_DIR = os.path.join(os.path.expanduser("~"), ".mal_tools_cache")
def _cache_path(kind: str, item_id: int) -> str:
os.makedirs(CACHE_DIR, exist_ok=True)
return os.path.join(CACHE_DIR, f"{kind}_{item_id}.json")
def _cache_get(kind: str, item_id: int):
path = _cache_path(kind, item_id)
if not os.path.isfile(path):
return None
try:
with open(path, "r", encoding="utf-8") as f:
return json.load(f)
except (OSError, json.JSONDecodeError):
return None
def _cache_put(kind: str, item_id: int, data: dict):
try:
with open(_cache_path(kind, item_id), "w", encoding="utf-8") as f:
json.dump(data, f)
except OSError:
pass # cache is best-effort
def fetch_anime_cached(anime_id: int):
"""Anime details with disk cache. Throttles to Jikan's ~3 req/s globally."""
if anime_id is None:
return None
hit = _cache_get("anime", anime_id)
if hit is not None:
return hit
_jikan_throttle()
try:
details = fetch_anime_details(anime_id)
_cache_put("anime", anime_id, details)
return details
except Exception:
return None
def fetch_manga_cached(manga_id: int):
if manga_id is None:
return None
hit = _cache_get("manga", manga_id)
if hit is not None:
return hit
_jikan_throttle()
try:
details = fetch_manga_details(manga_id)
_cache_put("manga", manga_id, details)
return details
except Exception:
return None
# ─── User profile / statistics ─────────────────────────────────────────────
def fetch_profile(username: str):
"""Jikan v4 /users/{username}/full — has statistics block."""
url = f"https://api.jikan.moe/v4/users/{urllib.parse.quote(username)}/full"
req = urllib.request.Request(url, headers={"User-Agent": USER_AGENT})
with urllib.request.urlopen(req, timeout=15) as resp:
data = json.loads(resp.read().decode("utf-8")).get("data") or {}
stats = data.get("statistics", {}) or {}
return {
"username": data.get("username"),
"url": data.get("url"),
"joined": data.get("joined"),
"image": (data.get("images", {}).get("jpg", {}) or {}).get("image_url"),
"anime": stats.get("anime", {}),
"manga": stats.get("manga", {}),
}
# ─── Enriched lists (with cached Jikan details merged in) ──────────────────
def fetch_enriched_anime(username: str):
"""Completed anime list, each entry enriched via Jikan (studios)."""
base = fetch_completed(username)
out = []
for a in base:
details = fetch_anime_cached(a["id"])
out.append({
**a,
"studios": (details or {}).get("studios", []),
"themes": (details or {}).get("themes", []),
# Override genres with Jikan's canonical list if available
"genres": (details or {}).get("genres", a.get("genres", [])),
})
return out
def fetch_completed_manga(username: str):
"""Completed manga list (status=2 on /mangalist/load.json)."""
items = _fetch_mal_list("mangalist", username, 2)
return [{
"title": it.get("manga_title", "Unknown"),
"type": it.get("manga_media_type_string", "Unknown"),
"chapters": it.get("manga_num_chapters", 0) or 0,
"volumes": it.get("manga_num_volumes", 0) or 0,
"id": it.get("manga_id"),
"image": _clean_image(it.get("manga_image_path", "")),
"genres": _extract_genres(it),
"score": it.get("score", 0) or 0,
} for it in items]
def fetch_enriched_manga(username: str):
"""Completed manga list, enriched via Jikan (authors, serializations)."""
base = fetch_completed_manga(username)
out = []
for m in base:
details = fetch_manga_cached(m["id"])
out.append({
**m,
"authors": (details or {}).get("authors", []),
"serializations": (details or {}).get("serializations", []),
"themes": (details or {}).get("themes", []),
"genres": (details or {}).get("genres", m.get("genres", [])),
})
return out
class Handler(BaseHTTPRequestHandler):
def _cors(self):
self.send_header("Access-Control-Allow-Origin", "*")
self.send_header("Access-Control-Allow-Methods", "GET, OPTIONS")
self.send_header("Access-Control-Allow-Headers", "Content-Type")
def _json(self, code, payload):
body = json.dumps(payload).encode("utf-8")
self.send_response(code)
self.send_header("Content-Type", "application/json; charset=utf-8")
self.send_header("Content-Length", str(len(body)))
self._cors()
self.end_headers()
self.wfile.write(body)
def do_OPTIONS(self):
self.send_response(204)
self._cors()
self.end_headers()
# API routes that should NOT fall through to static file serving
API_ROUTES = {
"health", "ptw", "ptr", "completed", "anime", "manga",
"profile", "enriched", "enriched-manga",
}
def do_GET(self):
# Strip query string for routing
path_only = self.path.split("?", 1)[0]
parts = path_only.strip("/").split("/", 1)
route = parts[0]
arg = urllib.parse.unquote(parts[1]) if len(parts) == 2 and parts[1] else None
if route == "health":
return self._json(200, {"ok": True})
# User list endpoints — same shape, different fetcher
list_handlers = {
"ptw": ("anime", fetch_ptw),
"ptr": ("manga", fetch_ptr),
"completed": ("anime", fetch_completed),
"enriched": ("anime", fetch_enriched_anime),
"enriched-manga": ("manga", fetch_enriched_manga),
}
if route in list_handlers and arg:
payload_key, fetcher = list_handlers[route]
try:
items = fetcher(arg)
return self._json(200, {
"username": arg,
"count": len(items),
payload_key: items,
})
except urllib.error.HTTPError as e:
if e.code == 404:
return self._json(404, {"error": f'User "{arg}" not found on MAL.'})
return self._json(e.code, {"error": f"MAL returned HTTP {e.code}."})
except Exception as e:
return self._json(500, {"error": str(e)})
# Detail endpoints — anime/manga by id via Jikan (cached on disk)
detail_handlers = {
"anime": fetch_anime_cached,
"manga": fetch_manga_cached,
}
if route in detail_handlers and arg and arg.isdigit():
try:
payload = detail_handlers[route](int(arg))
if payload is None:
return self._json(502, {"error": "Failed to fetch from Jikan."})
return self._json(200, payload)
except urllib.error.HTTPError as e:
return self._json(e.code, {"error": f"Jikan returned HTTP {e.code}."})
except Exception as e:
return self._json(500, {"error": str(e)})
# Profile / stats — returns Jikan's statistics block
if route == "profile" and arg:
try:
return self._json(200, fetch_profile(arg))
except urllib.error.HTTPError as e:
if e.code == 404:
return self._json(404, {"error": f'User "{arg}" not found on MAL.'})
return self._json(e.code, {"error": f"Jikan returned HTTP {e.code}."})
except Exception as e:
return self._json(500, {"error": str(e)})
# If route looks like an API call but didn't match, return JSON 404
if route in self.API_ROUTES:
return self._json(404, {
"error": "Routes: /ptw/<user>, /ptr/<user>, /completed/<user>, /anime/<id>, /manga/<id>"
})
# Otherwise: serve static file from ROOT_DIR
return self._serve_static(path_only)
def _serve_static(self, url_path):
# "/" → index.html
rel = url_path.lstrip("/") or "index.html"
# Resolve against ROOT_DIR and prevent path-traversal
target = os.path.normpath(os.path.join(ROOT_DIR, rel))
if not target.startswith(ROOT_DIR + os.sep) and target != ROOT_DIR:
return self._json(403, {"error": "forbidden"})
if not os.path.isfile(target):
return self._json(404, {"error": f"not found: {rel}"})
ctype, _ = mimetypes.guess_type(target)
ctype = ctype or "application/octet-stream"
try:
with open(target, "rb") as f:
body = f.read()
except OSError as e:
return self._json(500, {"error": str(e)})
self.send_response(200)
self.send_header("Content-Type", ctype)
self.send_header("Content-Length", str(len(body)))
self._cors()
self.end_headers()
self.wfile.write(body)
def log_message(self, fmt, *args):
sys.stderr.write(f"[mal_proxy] {fmt % args}\n")
def main():
no_browser = "--no-browser" in sys.argv
server = ThreadingHTTPServer(("127.0.0.1", PORT), Handler)
url = f"http://localhost:{PORT}/"
print("─" * 50)
print(f" MAL Tools — running at {url}")
print("─" * 50)
print(f" Open the hub in your browser: {url}")
print(f" API:")
print(f" /ptw/<user> Plan to Watch")
print(f" /ptr/<user> Plan to Read")
print(f" /completed/<user> Completed anime (with score)")
print(f" /profile/<user> Profile + stats (Jikan)")
print(f" /enriched/<user> Completed anime, enriched (cached)")
print(f" /enriched-manga/<user> Completed manga, enriched (cached)")
print(f" /anime/<id> Anime details (Jikan)")
print(f" /manga/<id> Manga details (Jikan)")
print()
print(" Ctrl-C to stop.")
print()
if not no_browser:
# Open the hub once the server has had a moment to start
threading.Timer(0.6, lambda: webbrowser.open(url)).start()
try:
server.serve_forever()
except KeyboardInterrupt:
print("\nShutting down.")
server.server_close()
if __name__ == "__main__":
main()