-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
203 lines (176 loc) · 7.18 KB
/
server.py
File metadata and controls
203 lines (176 loc) · 7.18 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
import re
import json
import threading
import urllib.request
import urllib.parse
from http.server import HTTPServer, BaseHTTPRequestHandler
from config import API, HEADERS, PROXY_PORT
from scraper import find_pages
from reader import generate_html
# -- Shared state -------------------------------------------------------------
_state = {
"chapters": [],
"current_index": 0,
"manga_title": "",
}
_state_lock = threading.Lock()
# -- Caches -------------------------------------------------------------------
_chapter_html_cache = {}
_prefetch_lock = threading.Lock()
_prefetching = set()
_img_cache = {}
_img_cache_lock = threading.Lock()
# -- Image prefetch -----------------------------------------------------------
def prefetch_images(page_urls):
def _fetch_one(url):
with _img_cache_lock:
if url in _img_cache:
return
try:
req = urllib.request.Request(url, headers=HEADERS)
with urllib.request.urlopen(req, timeout=20) as resp:
data = resp.read()
ct = resp.headers.get("Content-Type", "image/jpeg")
with _img_cache_lock:
_img_cache[url] = (ct, data)
except Exception:
pass
threads = [threading.Thread(target=_fetch_one, args=(u,), daemon=True) for u in page_urls]
for t in threads:
t.start()
def prefetch_adjacent(idx):
with _state_lock:
chapters = _state["chapters"]
total = len(chapters)
for target in [idx - 1, idx + 1]:
if target < 0 or target >= total:
continue
with _prefetch_lock:
with _state_lock:
already = _state.get("pages_cache", {}).get(target)
if already or target in _prefetching:
if already:
threading.Thread(target=prefetch_images, args=(already,), daemon=True).start()
continue
_prefetching.add(target)
def _do_prefetch(i=target):
try:
chapter = chapters[i]
pages = find_pages(chapter["id"])
with _state_lock:
if "pages_cache" not in _state:
_state["pages_cache"] = {}
_state["pages_cache"][i] = pages
prefetch_images(pages)
finally:
with _prefetch_lock:
_prefetching.discard(i)
threading.Thread(target=_do_prefetch, daemon=True).start()
# -- Proxy handler ------------------------------------------------------------
class ProxyHandler(BaseHTTPRequestHandler):
def log_message(self, *args):
pass
def do_GET(self):
parsed = urllib.parse.urlparse(self.path)
params = urllib.parse.parse_qs(parsed.query)
# redirect / to /read
if parsed.path == "/":
with _state_lock:
idx = _state["current_index"]
self.send_response(302)
self.send_header("Location", f"http://127.0.0.1:{PROXY_PORT}/read?idx={idx}")
self.end_headers()
return
# serve chapter reader
if parsed.path == "/read":
idx = int(params.get("idx", [0])[0])
s_layout = params.get("layout", ["long-strip"])[0]
s_fit = params.get("fit", ["fullscreen"])[0]
s_tap = params.get("tap", ["ltr"])[0]
s_dir = params.get("dir", ["ltr"])[0]
with _state_lock:
chapters = _state["chapters"]
manga_title = _state["manga_title"]
if idx < 0 or idx >= len(chapters):
self.send_error(404)
return
chapter = chapters[idx]
pages = find_pages(chapter["id"])
html = generate_html(pages, manga_title, chapter["title"], idx, len(chapters),
chapters=chapters,
init_layout=s_layout, init_fit=s_fit,
init_tap=s_tap, init_dir=s_dir)
threading.Thread(target=prefetch_images, args=(pages,), daemon=True).start()
with _state_lock:
_state["current_index"] = idx
_chapter_html_cache[idx] = html
threading.Thread(target=prefetch_adjacent, args=(idx,), daemon=True).start()
data = html.encode("utf-8")
self.send_response(200)
self.send_header("Content-Type", "text/html; charset=utf-8")
self.send_header("Content-Length", str(len(data)))
self.end_headers()
self.wfile.write(data)
return
# chapter nav
if parsed.path == "/chapter":
direction = int(params.get("nav", [0])[0])
current = int(params.get("current", [0])[0])
new_idx = current + direction
s_layout = params.get("layout", ["long-strip"])[0]
s_fit = params.get("fit", ["fullscreen"])[0]
s_tap = params.get("tap", ["ltr"])[0]
s_dir = params.get("dir", ["ltr"])[0]
with _state_lock:
total = len(_state["chapters"])
if 0 <= new_idx < total:
url = (f"http://127.0.0.1:{PROXY_PORT}/read?idx={new_idx}"
f"&layout={s_layout}&fit={s_fit}&tap={s_tap}&dir={s_dir}")
resp_data = json.dumps({"url": url}).encode()
else:
resp_data = json.dumps({"url": None}).encode()
self.send_response(200)
self.send_header("Content-Type", "application/json")
self.send_header("Content-Length", str(len(resp_data)))
self.end_headers()
self.wfile.write(resp_data)
return
# proxy images
img_url = params.get("url", [None])[0]
if img_url:
img_url = re.sub(r'&_t=\d+$', '', img_url)
with _img_cache_lock:
cached = _img_cache.get(img_url)
if cached:
ct, data = cached
self.send_response(200)
self.send_header("Content-Type", ct)
self.send_header("Content-Length", str(len(data)))
self.end_headers()
self.wfile.write(data)
return
try:
req = urllib.request.Request(img_url, headers=HEADERS)
with urllib.request.urlopen(req, timeout=20) as resp:
data = resp.read()
ct = resp.headers.get("Content-Type", "image/jpeg")
with _img_cache_lock:
_img_cache[img_url] = (ct, data)
self.send_response(200)
self.send_header("Content-Type", ct)
self.send_header("Content-Length", str(len(data)))
self.end_headers()
self.wfile.write(data)
except Exception as e:
self.send_error(502, str(e))
return
self.send_error(404)
_proxy_started = False
def start_proxy():
global _proxy_started
if _proxy_started:
return
server = HTTPServer(("127.0.0.1", PROXY_PORT), ProxyHandler)
t = threading.Thread(target=server.serve_forever, daemon=True)
t.start()
_proxy_started = True