-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
74 lines (59 loc) · 2.46 KB
/
Copy pathmain.py
File metadata and controls
74 lines (59 loc) · 2.46 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
#!/usr/bin/env python3
"""
Ogle — 学术搜索爬虫
用法:
python main.py # 启动 GUI
python main.py --cli # 命令行模式(读取 keywords.txt)
python main.py --cli "人工智能" "深度学习" # 命令行模式 + 关键词
"""
import sys
from pathlib import Path
# 确保 src 目录在 sys.path 中
sys.path.insert(0, str(Path(__file__).parent))
from src.config_loader import load_config
from src.crawler import run_crawler
from src.html_writer import write_results
def parse_keywords_arg(args: list) -> list:
"""从命令行参数或 keywords.txt 读取关键词。"""
keywords = []
for a in args:
if a.startswith("--"):
continue
keywords.append(a)
if not keywords:
txt_path = Path(__file__).parent / "keywords.txt"
if txt_path.exists():
lines = txt_path.read_text(encoding="utf-8").strip().splitlines()
keywords = [line.strip() for line in lines
if line.strip() and not line.strip().startswith("#")]
return keywords
def main():
args = sys.argv[1:]
# --cli 模式
if "--cli" in args:
keywords = parse_keywords_arg(args)
if not keywords:
print("❌ 没有提供关键词。用法: python main.py --cli 关键词1 关键词2")
print(" 或在 keywords.txt 中每行写一个关键词")
return
print(f"📋 关键词 ({len(keywords)}): {', '.join(keywords)}")
config = load_config()
print(f"🚀 开始搜索...\n")
def cli_progress(collected, max_total, keyword):
"""CLI 进度回调:每获取一条结果立刻刷新。"""
bar_width = 30
ratio = collected / max_total if max_total > 0 else 1
filled = int(bar_width * min(ratio, 1))
bar = "█" * filled + "░" * (bar_width - filled)
print(f"\r [{bar}] {collected}/{max_total} 条 ← {keyword}", end="", flush=True)
results = run_crawler(keywords, config, progress_callback=cli_progress)
total = sum(len(v) for v in results.values())
output_dir = write_results(results, config)
print(f"\n✅ 完成!共 {total} 条结果,保存至: {output_dir.resolve()}")
print(f" 汇总页: {output_dir / 'all_results' / 'index.html'}")
return
# 默认启动 GUI
from src.gui import launch_gui
launch_gui()
if __name__ == "__main__":
main()