-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
809 lines (703 loc) · 29.5 KB
/
Copy pathserver.py
File metadata and controls
809 lines (703 loc) · 29.5 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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
"""
FastAPI 后端 -- 封装爬虫为 REST API,支持并发下载、进度追踪、PDF 导出。
启动: python server.py → http://127.0.0.1:8765
"""
from __future__ import annotations
import sys
import os
import re
import random
import time
import json
import uuid
import threading
from pathlib import Path
from concurrent.futures import ThreadPoolExecutor, as_completed
from typing import Any
from urllib.parse import urljoin, quote as urllib_quote
import httpx
from selectolax.parser import HTMLParser
from fastapi import FastAPI, BackgroundTasks, Form, HTTPException, Query
from fastapi.responses import FileResponse, HTMLResponse, JSONResponse
from fastapi.staticfiles import StaticFiles
import uvicorn
from PIL import Image
from utils import detect_encoding as _detect_encoding, safe_filename
from novel import _guess_chapter_links as _novel_chapter_links, _extract_content as _novel_content, _clean_text as _clean_novel
# ---------- 工具 ----------
STATIC_DIR = Path(__file__).parent / "static"
OUTPUT_BASE = Path(os.environ.get("CRAWLER_OUTPUT", str(Path(__file__).parent / "downloads")))
OUTPUT_BASE.mkdir(exist_ok=True)
HEADERS = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8",
}
# 任务状态存储
tasks_store: dict[str, dict] = {}
_executor = ThreadPoolExecutor(max_workers=8)
app = FastAPI(title="全能爬虫", docs_url=None, redoc_url=None)
# ---------- 进度追踪 ----------
def new_task(task_type: str, name: str) -> str:
tid = uuid.uuid4().hex[:8]
tasks_store[tid] = {
"id": tid, "type": task_type, "name": name,
"status": "running", "progress": 0, "total": 0,
"message": "初始化...", "output_dir": "", "error": None,
}
return tid
def update_task(tid: str, **kw):
if tid in tasks_store:
tasks_store[tid].update(kw)
# ---------- HTTP 客户端 ----------
PROXY_URL = os.environ.get("CRAWLER_PROXY", "http://127.0.0.1:7897")
def _client(timeout: int = 30):
"""直连客户端(内网、localhost)。"""
return httpx.Client(headers=HEADERS, timeout=timeout, follow_redirects=True,
http2=True, verify=False, trust_env=False, proxy=None)
def _ext_client(timeout: int = 15):
"""外网客户端(走代理)。"""
return httpx.Client(headers=HEADERS, timeout=timeout, follow_redirects=True,
http2=True, verify=False, trust_env=False, proxy=PROXY_URL)
# ---------- 小说爬虫 ----------
# _detect_encoding, safe_filename, _novel_chapter_links, _novel_content, _clean_novel
# are all imported from utils.py / novel.py — no local duplication.
def run_novel(tid: str, url: str, output: str, delay: float, limit: int, threads: int):
out_dir = OUTPUT_BASE / output
out_dir.mkdir(parents=True, exist_ok=True)
client = _ext_client()
update_task(tid, message="获取章节目录...")
resp = client.get(url)
resp.encoding = _detect_encoding(resp)
chapters = _novel_chapter_links(resp.text, url)
if limit > 0:
chapters = chapters[:limit]
update_task(tid, total=len(chapters), message=f"共 {len(chapters)} 章", output_dir=str(out_dir))
if not chapters:
update_task(tid, status="error", error="未识别到章节链接")
client.close(); return
ch_dir = out_dir / "chapters"; ch_dir.mkdir(exist_ok=True)
merged: list[str] = []
lock = threading.Lock()
completed = [0]
def dl_one(idx_title_url):
i, title, ch_url = idx_title_url
try:
fn = ch_dir / f"{i:04d}_{safe_filename(title)}.txt"
if fn.exists():
txt = fn.read_text("utf-8")
else:
r = client.get(ch_url)
r.encoding = _detect_encoding(r)
txt = _novel_content(r.text)
fn.write_text(txt, "utf-8")
time.sleep(delay * (0.2 + random.random() * 0.3))
with lock:
merged.append(f"\n\n{title}\n\n"); merged.append(txt)
completed[0] += 1
update_task(tid, progress=completed[0], message=f"[{completed[0]}/{len(chapters)}] {title}")
return True
except Exception as e:
with lock:
completed[0] += 1
update_task(tid, progress=completed[0], message=f"失败: {title} - {e}")
return False
items = [(i, t, u) for i, (t, u) in enumerate(chapters, 1)]
if threads > 1:
with ThreadPoolExecutor(max_workers=min(threads, 6)) as pool:
list(pool.map(dl_one, items))
else:
for item in items:
dl_one(item)
# 合并
if merged:
merged_path = out_dir / "full_novel.txt"
merged_path.write_text("".join(merged).strip(), "utf-8")
update_task(tid, status="done", progress=len(chapters), message="小说下载完成")
client.close()
# ---------- 漫画爬虫 ----------
def _comic_chapter_links(html: str, base_url: str) -> list[tuple[str, str]]:
tree = HTMLParser(html)
links = []
for sel in (".chapter-list a", ".chapters a", "#chapterlist a",
".detail-list a", ".episode-list a", "ul.list a"):
for a in tree.css(sel):
h, t = a.attributes.get("href", ""), a.text(strip=True)
if h and t and len(t) < 60:
links.append((t, urljoin(base_url, h)))
if links: return links
for a in tree.css("a"):
h, t = a.attributes.get("href", ""), a.text(strip=True)
if re.search(r"(第\d+|\d+话|\d+卷|episode|chapter)", t + h, re.I):
if h and len(t) < 60:
links.append((t, urljoin(base_url, h)))
return links
def _comic_images(html: str, base_url: str) -> list[str]:
tree = HTMLParser(html)
urls = []
for sel in (".comicpage img", ".comic-content img", "#cp_img img",
".reader-img img", ".chapter-content img", ".comicimg img"):
for img in tree.css(sel):
src = img.attributes.get("src") or img.attributes.get("data-src") or img.attributes.get("data-original")
if src:
urls.append(urljoin(base_url, src))
if urls: return list(dict.fromkeys(urls))
for img in tree.css("img"):
src = img.attributes.get("src") or img.attributes.get("data-src") or img.attributes.get("data-original")
if not src: continue
l = src.lower()
if any(x in l for x in ("avatar", "icon", "logo", "banner", "ad.", "qr_code")): continue
urls.append(urljoin(base_url, src))
return list(dict.fromkeys(urls))
def run_comic(tid: str, url: str, output: str, delay: float, limit: int, threads: int):
out_dir = OUTPUT_BASE / output
out_dir.mkdir(parents=True, exist_ok=True)
client = _ext_client()
update_task(tid, message="获取章节列表...")
resp = client.get(url)
resp.encoding = _detect_encoding(resp)
chapters = _comic_chapter_links(resp.text, url)
if limit > 0: chapters = chapters[:limit]
update_task(tid, total=len(chapters), message=f"共 {len(chapters)} 话", output_dir=str(out_dir))
if not chapters:
update_task(tid, status="error", error="未识别到章节链接")
client.close(); return
completed = [0]; lock = threading.Lock()
def dl_chapter(idx_title_url):
i, title, ch_url = idx_title_url
sd = safe_filename(title)
ch_dir = out_dir / f"{i:04d}_{sd}"; ch_dir.mkdir(exist_ok=True)
existing = list(ch_dir.glob("*"))
if existing and all(f.suffix.lower() in (".jpg",".jpeg",".png",".webp",".gif") for f in existing):
with lock:
completed[0] += 1
update_task(tid, progress=completed[0], message=f"[{completed[0]}/{len(chapters)}] {title} (已存在)")
return
try:
r = client.get(ch_url)
r.encoding = _detect_encoding(r)
imgs = _comic_images(r.text, ch_url)
if not imgs:
with lock:
completed[0] += 1
update_task(tid, progress=completed[0], message=f"跳过: {title} (无图片)")
return
img_headers = {"User-Agent": HEADERS["User-Agent"], "Referer": ch_url}
for j, img_url in enumerate(imgs, 1):
ext = ".jpg"
for e in (".jpg",".jpeg",".png",".webp",".gif"):
if e in img_url.lower().split("?")[0]: ext = e; break
pf = ch_dir / f"{j:03d}{ext}"
if pf.exists(): continue
pf.write_bytes(client.get(img_url, headers=img_headers).content)
time.sleep(delay * 0.3)
with lock:
completed[0] += 1
update_task(tid, progress=completed[0], message=f"[{completed[0]}/{len(chapters)}] {title} ({len(imgs)}页)")
except Exception as e:
with lock:
completed[0] += 1
update_task(tid, progress=completed[0], message=f"失败: {title} - {e}")
items = [(i, t, u) for i, (t, u) in enumerate(chapters, 1)]
if threads > 1:
with ThreadPoolExecutor(max_workers=min(threads, 4)) as pool:
list(pool.map(dl_chapter, items))
else:
for item in items:
dl_chapter(item)
update_task(tid, status="done", progress=len(chapters), message="漫画下载完成")
client.close()
# ---------- PDF 导出 ----------
def run_comic_pdf(tid: str, output: str):
"""将已下载的漫画导出为 PDF。"""
out_dir = OUTPUT_BASE / output
if not out_dir.exists():
update_task(tid, status="error", error="目录不存在,请先下载")
return
from reportlab.lib.pagesizes import A4
from reportlab.pdfgen import canvas as rl_canvas
update_task(tid, message="正在导出 PDF...")
pdf_path = out_dir / f"{out_dir.name}.pdf"
ch_dirs = sorted([d for d in out_dir.iterdir() if d.is_dir()])
c = rl_canvas.Canvas(str(pdf_path), pagesize=A4)
pw, ph = A4
for ch_dir in ch_dirs:
imgs = sorted(ch_dir.glob("*"))
imgs = [f for f in imgs if f.suffix.lower() in (".jpg", ".jpeg", ".png", ".webp", ".gif")]
for img_path in imgs:
try:
img = Image.open(img_path)
iw, ih = img.size
scale = min(pw / iw, ph / ih, 1.0)
dw, dh = iw * scale, ih * scale
x, y = (pw - dw) / 2, (ph - dh) / 2
c.drawImage(str(img_path), x, y, dw, dh)
c.showPage()
except Exception:
continue
c.save()
update_task(tid, status="done", progress=100, total=100,
message=f"PDF 导出完成: {pdf_path.name}", output_dir=str(pdf_path))
# ---------- 搜索功能 ----------
def _parse_search_results(html: str, base_url: str, source_name: str,
keyword: str, url_attr: str = "href") -> list[dict]:
"""通用:从 HTML 中提取包含关键词的链接作为搜索结果。"""
tree = HTMLParser(html)
results = []
seen = set()
for a in tree.css("a"):
href = (a.attributes.get(url_attr) or "").strip()
title = a.text(strip=True)
if not href or not title:
continue
if len(title) < 3 or len(title) > 120:
continue
if any(x in href for x in ("javascript:", "#", "mailto:", "logout", "login")):
continue
# 必须与关键词相关
if keyword not in title:
continue
full_url = urljoin(base_url, href)
key = title + full_url
if key in seen:
continue
seen.add(key)
# 分类标签
extra = ""
parent = a.parent
if parent:
for tag in parent.css("span, em, small, .cat, .type, .category"):
extra = tag.text(strip=True)
if extra and len(extra) < 20:
break
results.append({
"title": title, "url": full_url, "source": source_name,
"extra": extra or "",
})
return results
def search_novels(keyword: str) -> list[dict]:
"""多源小说搜索——并行请求多个笔趣阁镜像站。"""
results: list[dict] = []
seen: set[str] = set()
def _add(url: str, title: str, source: str, extra: str = "") -> None:
if url not in seen:
seen.add(url)
results.append({"title": title, "url": url, "source": source, "extra": extra})
novel_sites = [
("bpshu", "https://www.bpshu.cc/search.php?q={}", "https://www.bpshu.cc"),
("biquge", "https://www.biquge.com.cn/search/?q={}", "https://www.biquge.com.cn"),
("biqubu", "https://www.biqubu.com/search.html?keyword={}","https://www.biqubu.com"),
("trxs", "https://www.trxs.cc/search.html?keyword={}", "https://www.trxs.cc"),
("xbiquge", "https://www.xbiquge.com/search.html?keyword={}","https://www.xbiquge.com"),
("69shu", "https://www.69shu.com/modules/article/search.php?searchkey={}","https://www.69shu.com"),
("biquyun", "https://www.biquyun.com/search.html?keyword={}","https://www.biquyun.com"),
("ibiquge", "https://www.ibiquge.cc/modules/article/search.php?searchkey={}","https://www.ibiquge.cc"),
]
def _fetch_one(name: str, tpl: str, base: str) -> list[dict]:
out: list[dict] = []
cl = _client(10)
try:
r = cl.get(tpl.format(urllib_quote(keyword)))
r.encoding = _detect_encoding(r)
out = _parse_search_results(r.text, base, name, keyword)
except Exception:
pass
finally:
cl.close()
return out
with ThreadPoolExecutor(max_workers=6) as pool:
futs = {pool.submit(_fetch_one, n, t, b): n for n, t, b in novel_sites}
for fut in as_completed(futs, timeout=12):
try:
for r in fut.result():
_add(r["url"], r["title"], r["source"], r.get("extra", ""))
except Exception:
pass
# Bing fallback
if len(results) < 5:
try:
ext = _ext_client(10)
tree = HTMLParser(ext.get(
f"https://www.bing.com/search?q={urllib_quote(keyword)}+%E5%B0%8F%E8%AF%B4+%E7%AB%A0%E8%8A%82&cc=us&count=20").text)
for item in tree.css("li.b_algo"):
h2 = item.css_first("h2 a")
if not h2: continue
href, title = h2.attributes.get("href", ""), h2.text(strip=True)
if not href.startswith("http"): continue
bad = ("百科","百度","知乎","贴吧","微博","小红书","闲鱼","淘宝","京东","拼多多","抖音","快手","Amazon","Instagram")
if any(b in title for b in bad): continue
snippet = ""
for p in item.css("p,.b_caption"): snippet = p.text(strip=True)[:60]; break
_add(href, title, "Bing", snippet)
ext.close()
except Exception:
pass
return _dedupe_results(results, keyword)
def search_comics(keyword: str) -> list[dict]:
"""多源漫画搜索——中英文并行。"""
results: list[dict] = []
seen: set[str] = set()
def _add(url: str, title: str, source: str, extra: str = "") -> None:
if url not in seen:
seen.add(url)
results.append({"title": title, "url": url, "source": source, "extra": extra})
# 国内直连站点
zh_sites = [
("包子漫画", "https://www.baozimh.com/search?q={}", "https://www.baozimh.com"),
("可乐漫画", "https://www.colamanhua.com/search?keyword={}","https://www.colamanhua.com"),
("动漫屋", "https://www.dm5.com/search?title={}", "https://www.dm5.com"),
("动漫之家", "https://manhua.idmzj.com/search?q={}", "https://manhua.idmzj.com"),
("漫画人", "https://www.manhuaren.com/search?keywords={}","https://www.manhuaren.com"),
("极速漫画", "https://www.1kkk.com/search/?keywords={}", "https://www.1kkk.com"),
("SF漫画", "https://manga.sfacg.com/ALLA/Common/Search/Search.aspx?searchstr={}","https://manga.sfacg.com"),
]
# 海外漫画站(走代理)
en_sites = [
("MangaPill", "https://mangapill.com/search?q={}&type=all","https://mangapill.com"),
("MangaNato", "https://manganato.com/search/story/{}", "https://manganato.com"),
("MangaDex", "https://mangadex.org/?q={}", "https://mangadex.org"),
]
def _fetch_one(name: str, tpl: str, base: str, proxy: bool) -> list[dict]:
out: list[dict] = []
cl = _ext_client(12) if proxy else _client(12)
try:
r = cl.get(tpl.format(urllib_quote(keyword)))
r.encoding = _detect_encoding(r)
out = _parse_search_results(r.text, base, name, keyword)
except Exception:
pass
finally:
cl.close()
return out
with ThreadPoolExecutor(max_workers=8) as pool:
futs = {}
for n, t, b in zh_sites:
futs[pool.submit(_fetch_one, n, t, b, False)] = n
for n, t, b in en_sites:
futs[pool.submit(_fetch_one, n, t, b, True)] = n
for fut in as_completed(futs, timeout=15):
try:
for r in fut.result():
_add(r["url"], r["title"], r["source"], r.get("extra", ""))
except Exception:
pass
# Bing fallback
if len(results) < 5:
try:
ext = _ext_client(10)
for query in ("漫画 在线阅读", "manga read online"):
tree = HTMLParser(ext.get(
f"https://www.bing.com/search?q={urllib_quote(keyword)}+{urllib_quote(query)}&cc=us&count=20").text)
for item in tree.css("li.b_algo"):
h2 = item.css_first("h2 a")
if not h2: continue
href, title = h2.attributes.get("href", ""), h2.text(strip=True)
if not href.startswith("http"): continue
skip = ("百科","百度","知乎","豆瓣","Amazon","Instagram","Facebook","Twitter")
if any(s in title for s in skip): continue
snippet = ""
for p in item.css("p,.b_caption"): snippet = p.text(strip=True)[:60]; break
_add(href, title, "Bing", snippet)
ext.close()
except Exception:
pass
return _dedupe_results(results, keyword)
def search_videos(keyword: str) -> list[dict]:
"""搜索视频 —— 用 yt-dlp 的搜索功能(YouTube + B站)。"""
import subprocess, shutil, json as _json
if not shutil.which("yt-dlp"):
subprocess.check_call([sys.executable, "-m", "pip", "install", "yt-dlp", "-q"])
results = []
# YouTube 搜索
try:
proc = subprocess.run(
["yt-dlp", f"ytsearch8:{keyword}", "--dump-json", "--no-warnings",
"--skip-download", "--flat-playlist"],
capture_output=True, text=True, timeout=20,
)
for line in proc.stdout.strip().splitlines():
if not line.strip():
continue
try:
info = _json.loads(line)
vid = info.get("id", "")
title = info.get("title", "")
if title and vid:
results.append({
"title": title,
"url": f"https://www.youtube.com/watch?v={vid}",
"source": "YouTube",
"extra": info.get("duration_string", "") or info.get("uploader", ""),
})
except Exception:
pass
except Exception:
pass
# B站搜索
try:
proc2 = subprocess.run(
["yt-dlp", f"bilisearch8:{keyword}", "--dump-json", "--no-warnings",
"--skip-download", "--flat-playlist"],
capture_output=True, text=True, timeout=20,
)
for line in proc2.stdout.strip().splitlines():
if not line.strip():
continue
try:
info = _json.loads(line)
vid = info.get("id", "")
title = info.get("title", "")
if title and vid:
results.append({
"title": title,
"url": f"https://www.bilibili.com/video/{vid}",
"source": "B站",
"extra": info.get("duration_string", "") or info.get("uploader", ""),
})
except Exception:
pass
except Exception:
pass
return results
def _safe_path(rel_path: str) -> Path:
"""防路径穿越:只能访问 OUTPUT_BASE 内的文件。"""
full = (OUTPUT_BASE / rel_path).resolve()
if not full.is_relative_to(OUTPUT_BASE.resolve()):
raise HTTPException(status_code=403, detail="path traversal denied")
return full
def _dedupe_results(results: list[dict], keyword: str) -> list[dict]:
"""去重,并按标题与关键词的匹配度排序。"""
seen = set()
uniq = []
for r in results:
k = r["title"] + r["url"]
if k not in seen:
seen.add(k)
uniq.append(r)
# 排序:标题完全匹配的排前面
def score(r):
s = 0
if keyword in r["title"]:
s += 100
if r["title"].startswith(keyword):
s += 50
# 优先有 extra 的
if r["extra"]:
s += 10
return -s
uniq.sort(key=score)
return uniq[:30]
# ---------- 视频下载 ----------
def run_video(tid: str, url: str, output: str, quality: str, subs: bool, playlist: bool):
import subprocess, shutil
out_dir = OUTPUT_BASE / output
out_dir.mkdir(parents=True, exist_ok=True)
update_task(tid, message="启动 yt-dlp 下载...", output_dir=str(out_dir))
if not shutil.which("yt-dlp"):
subprocess.check_call([sys.executable, "-m", "pip", "install", "yt-dlp", "-q"])
tmpl = str(out_dir / "%(title).100s_%(resolution)s.%(ext)s")
if quality == "audio":
tmpl = str(out_dir / "%(title).100s.%(ext)s")
cmd = ["yt-dlp", "-o", tmpl, "--no-playlist" if not playlist else "",
"--write-subs" if subs else "", "--ignore-errors", "--no-warnings", url]
if quality == "best":
cmd.insert(-1, "-f"); cmd.insert(-1, "bestvideo+bestaudio/best")
elif quality == "audio":
cmd.insert(-1, "-x"); cmd.insert(-1, "--audio-format"); cmd.insert(-1, "mp3")
else:
h = quality.replace("p", "")
cmd.insert(-1, "-f"); cmd.insert(-1, f"bestvideo[height<={h}]+bestaudio/best[height<={h}]/best")
cmd = [c for c in cmd if c]
try:
proc = subprocess.run(cmd, check=False, capture_output=False)
if proc.returncode == 0:
update_task(tid, status="done", progress=100, total=100, message="视频下载完成")
else:
update_task(tid, status="error", error=f"yt-dlp 退出码: {proc.returncode}")
except Exception as e:
update_task(tid, status="error", error=str(e))
# ---------- API 路由 ----------
@app.get("/")
def index():
return HTMLResponse((STATIC_DIR / "index.html").read_text("utf-8"))
@app.get("/api/task/{tid}")
def api_task(tid: str):
if tid not in tasks_store:
return JSONResponse({"error": "not found"}, 404)
return tasks_store[tid]
@app.get("/api/tasks")
def api_tasks():
return list(tasks_store.values())
@app.post("/api/novel")
def api_novel(
url: str = Form(...),
output: str = Form(""),
delay: float = Form(1.0),
limit: int = Form(0),
threads: int = Form(1),
):
tid = new_task("novel", url)
out = output or f"novel_{time.strftime('%Y%m%d_%H%M%S')}"
_executor.submit(run_novel, tid, url, out, delay, limit, threads)
return {"task_id": tid}
@app.post("/api/comic")
def api_comic(
url: str = Form(...),
output: str = Form(""),
delay: float = Form(1.5),
limit: int = Form(0),
threads: int = Form(1),
):
tid = new_task("comic", url)
out = output or f"comic_{time.strftime('%Y%m%d_%H%M%S')}"
_executor.submit(run_comic, tid, url, out, delay, limit, threads)
return {"task_id": tid}
@app.post("/api/comic/pdf")
def api_comic_pdf(output: str = Form(...)):
"""将已下载的漫画目录导出为 PDF。"""
tid = new_task("comic_pdf", output)
_executor.submit(run_comic_pdf, tid, output)
return {"task_id": tid}
@app.post("/api/video")
def api_video(
url: str = Form(...),
output: str = Form(""),
quality: str = Form("best"),
subs: bool = Form(False),
playlist: bool = Form(False),
):
tid = new_task("video", url)
out = output or f"video_{time.strftime('%Y%m%d_%H%M%S')}"
_executor.submit(run_video, tid, url, out, quality, subs, playlist)
return {"task_id": tid}
@app.get("/api/download/{output:path}")
def api_download_file(output: str):
"""下载已完成的文件。"""
p = _safe_path(output)
if not p.exists():
return JSONResponse({"error": "file not found"}, 404)
return FileResponse(p, filename=p.name)
@app.get("/api/files")
def api_files():
"""列出已下载的文件/目录。"""
items = []
for p in sorted(OUTPUT_BASE.iterdir(), key=lambda x: -x.stat().st_mtime):
if p.is_dir():
size = sum(f.stat().st_size for f in p.rglob("*") if f.is_file())
else:
size = p.stat().st_size
items.append({
"name": p.name, "is_dir": p.is_dir(),
"path": str(p.relative_to(OUTPUT_BASE)),
"size": size, "mtime": p.stat().st_mtime,
})
return items[:50]
@app.delete("/api/files/{path:path}")
def api_delete_file(path: str):
p = _safe_path(path)
if not p.exists():
return JSONResponse({"error": "not found"}, 404)
if p.is_dir():
import shutil; shutil.rmtree(p)
else:
p.unlink()
return {"ok": True}
@app.get("/api/browse/{path:path}")
def api_browse(path: str):
"""浏览目录内容:列出章节文件夹、图片文件、TXT文件等。"""
p = _safe_path(path)
if not p.exists():
return JSONResponse({"error": "not found"}, 404)
if not p.is_dir():
return JSONResponse({"error": "not a directory"}, 400)
items = []
for child in sorted(p.iterdir()):
if child.is_dir():
file_count = sum(1 for _ in child.rglob("*") if _.is_file())
items.append({
"name": child.name, "is_dir": True,
"size": file_count, "mtime": child.stat().st_mtime,
})
elif child.is_file():
items.append({
"name": child.name, "is_dir": False,
"size": child.stat().st_size,
"ext": child.suffix.lower(),
"mtime": child.stat().st_mtime,
})
# 排序:目录在前,文件在后,各自按名称排序
dirs = [i for i in items if i["is_dir"]]
files = [i for i in items if not i["is_dir"]]
dirs.sort(key=lambda x: x["name"])
files.sort(key=lambda x: x["name"])
return {
"path": path,
"name": p.name,
"items": dirs + files,
}
@app.get("/api/read/{path:path}")
def api_read(path: str):
"""读取文本文件内容(小说章节、合并TXT等)。"""
p = _safe_path(path)
if not p.exists():
return JSONResponse({"error": "not found"}, 404)
if not p.is_file():
return JSONResponse({"error": "not a file"}, 400)
ext = p.suffix.lower()
# 只允许文本类型
if ext not in (".txt", ".html", ".htm", ".md", ".xml", ".json"):
return JSONResponse({"error": f"不支持预览: {ext}"}, 400)
try:
content = p.read_text("utf-8")
except UnicodeDecodeError:
try:
content = p.read_text("gbk")
except Exception:
return JSONResponse({"error": "无法解码文件"}, 400)
return {
"path": path,
"name": p.name,
"size": len(content),
"content": content,
}
@app.get("/api/raw/{path:path}")
def api_raw(path: str):
"""直接返回文件内容(图片/视频等二进制文件在浏览器查看)。"""
p = _safe_path(path)
if not p.exists():
return JSONResponse({"error": "not found"}, 404)
return FileResponse(p)
# ---------- 搜索 API ----------
@app.get("/api/search")
def api_search(q: str = "", type: str = "novel"):
"""搜索小说/漫画/视频。
type: novel | comic | video
"""
if not q.strip():
return {"results": [], "query": q}
q = q.strip()
if type == "novel":
results = search_novels(q)
elif type == "comic":
results = search_comics(q)
elif type == "video":
results = search_videos(q)
else:
return JSONResponse({"error": "type 必须是 novel/comic/video"}, 400)
return {"results": results, "query": q, "type": type}
# ---------- Tarot ----------
@app.get("/tarot")
def tarot_page():
return HTMLResponse((STATIC_DIR / "tarot.html").read_text("utf-8"))
@app.post("/api/tarot")
async def api_tarot(cards: str = Form(...), question: str = Form(""), spread: str = Form("single")):
from tarot_engine import get_reading
card_list = [c.strip() for c in cards.split(",") if c.strip()]
if not card_list:
return JSONResponse({"error": "no cards"}, 400)
reading = get_reading(card_list, question, spread)
return {"reading": reading}
# ---------- 启动 ----------
if __name__ == "__main__":
STATIC_DIR.mkdir(exist_ok=True)
uvicorn.run(app, host="0.0.0.0", port=8765, log_level="info")