-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
400 lines (316 loc) · 13.3 KB
/
main.py
File metadata and controls
400 lines (316 loc) · 13.3 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
#!/usr/bin/env python3
"""
AutoCCF - 百度贴吧互联网踪迹存档系统
统一入口,提供交互式菜单界面。
用法:
python main.py # 交互模式
python main.py --user 用户名 # 直接操作指定用户
"""
import sys
import os
from pathlib import Path
from typing import Optional
# 确保项目根目录在 Python 路径中
sys.path.insert(0, str(Path(__file__).parent))
from AutoCCF.cli import CLI, Colors
from AutoCCF.config import config_manager, UnifiedConfig
from AutoCCF.utils import is_valid_bduss, UserPaths
VERSION = "2.0.0"
class MainMenu:
"""主菜单界面"""
def __init__(self):
self.cli = CLI("AutoCCF - 互联网踪迹存档系统", VERSION)
self.config: Optional[UnifiedConfig] = None
def run(self):
"""运行主程序"""
self.cli.print_banner("百度贴吧数据存档工具")
# 加载或创建配置
self.config = config_manager.load_or_setup()
# 确保数据目录存在
self.config.ensure_database_dir()
# 主循环
while True:
try:
action = self.show_main_menu()
if action == "quit":
break
except KeyboardInterrupt:
print()
self.cli.info("再见!")
break
def show_main_menu(self) -> str:
"""显示主菜单"""
assert self.config is not None
self.cli.print_section("主菜单")
# 显示当前状态
users = config_manager.list_users()
db_path = self.config.get_database_path()
print(f" {Colors.GRAY}数据目录:{Colors.RESET} {db_path}")
print(f" {Colors.GRAY}已存档用户:{Colors.RESET} {len(users)}")
print()
# 菜单选项
options = [
("1", "获取用户发言列表", "APoU - 爬取指定用户的所有发言"),
("2", "获取帖子详情", "DoPJ - 爬取帖子的完整内容"),
("3", "查看用户列表", "浏览已存档的用户数据"),
("4", "配置设置", "修改配置和账户信息"),
("0", "退出", ""),
]
for key, title, desc in options:
if desc:
print(f" {Colors.CYAN}[{key}]{Colors.RESET} {title}")
print(f" {Colors.DIM}{desc}{Colors.RESET}")
else:
print(f" {Colors.CYAN}[{key}]{Colors.RESET} {title}")
print()
try:
choice = input(f"{Colors.CYAN}? 请选择操作 [1-4, 0]: {Colors.RESET}").strip()
except (EOFError, KeyboardInterrupt):
return "quit"
if choice == "1":
self.run_apou()
elif choice == "2":
self.run_dopj()
elif choice == "3":
self.show_users()
elif choice == "4":
self.show_settings()
elif choice == "0":
return "quit"
else:
self.cli.warning("无效选择")
return "continue"
def run_apou(self):
"""运行 APoU 模块"""
assert self.config is not None
self.cli.print_section("APoU - 获取用户发言列表")
# 检查账户配置
if not self.config.has_valid_accounts():
self.cli.error("未配置有效的 BDUSS,请先在配置中添加账户")
print(f" {Colors.DIM}编辑 config.json 添加 BDUSS{Colors.RESET}")
return
try:
username = input(f"{Colors.CYAN}? 请输入要爬取的用户名: {Colors.RESET}").strip()
if not username:
self.cli.warning("已取消")
return
# 使用 UserPaths 管理路径,确保使用新版目录结构
paths = UserPaths(self.config.database_dir, username)
paths.ensure_apou()
# 检查是否已有数据(兼容新旧路径),询问是否增量更新
posts_file = paths.get_posts_file()
incremental = False
if posts_file is not None:
choice = input(
f"{Colors.YELLOW}? 检测到已有数据,是否只获取新发言?[Y/n]: {Colors.RESET}"
).strip().lower()
incremental = choice != "n"
if incremental:
self.cli.info("将使用增量模式,只获取新发言")
# 获取第一个有效的 BDUSS
bduss = ""
for acc in self.config.accounts:
if is_valid_bduss(acc.bduss):
bduss = acc.bduss
self.cli.info(f"使用账户: {acc.name}")
break
# 导入并运行 APoU
from APoU import UserPostsCrawler
from APoU.config import CrawlerConfig
apou_config = CrawlerConfig(
bduss=bduss,
page_delay=self.config.apou.page_delay,
max_retries=self.config.apou.max_retries,
)
mode_text = "增量更新" if incremental else "完整获取"
self.cli.print_config([
("目标用户", username),
("输出目录", str(paths.apou_dir)),
("页间延迟", f"{self.config.apou.page_delay} 秒"),
("模式", mode_text),
("API", "aiotieba (protobuf)"),
])
print()
import asyncio
import time
start_time = time.time()
crawler = UserPostsCrawler(apou_config, cli=self.cli)
posts = asyncio.run(crawler.crawl(
username=username,
save_incremental=True,
output_dir=str(paths.apou_dir),
incremental=incremental,
))
elapsed = time.time() - start_time
if posts:
self.cli.success(f"已保存 {len(posts)} 条发言到 {paths.posts_file}")
self.cli.info(f"耗时: {self.cli.format_duration(elapsed)}")
else:
self.cli.warning("未获取到任何发言")
except KeyboardInterrupt:
print()
self.cli.warning("已取消")
except Exception as e:
self.cli.error(f"爬取失败: {e}")
def run_dopj(self):
"""运行 DoPJ 模块"""
assert self.config is not None
self.cli.print_section("DoPJ - 获取帖子详情")
# 检查账户配置
if not self.config.has_valid_accounts():
self.cli.error("未配置有效的 BDUSS,请先在配置中添加账户")
print(f" {Colors.DIM}编辑 config.json 添加 BDUSS{Colors.RESET}")
return
# 查找可用的 APoU 输出
apou_outputs = config_manager.find_apou_outputs()
if not apou_outputs:
self.cli.warning("未找到任何用户数据,请先运行 APoU 获取用户发言列表")
return
# 显示用户选择
print()
print(f" {Colors.GRAY}可用的用户数据:{Colors.RESET}")
print()
for i, item in enumerate(apou_outputs, 1):
status = ""
if item["has_index"]:
status = f"{Colors.GREEN}[已完成]{Colors.RESET}"
else:
status = f"{Colors.YELLOW}[待处理]{Colors.RESET}"
print(f" {Colors.CYAN}[{i}]{Colors.RESET} {item['username']} "
f"{Colors.DIM}({item['posts_count']} 条发言){Colors.RESET} {status}")
print()
try:
choice = input(f"{Colors.CYAN}? 请选择用户 [1-{len(apou_outputs)}]: {Colors.RESET}").strip()
if not choice:
self.cli.warning("已取消")
return
idx = int(choice) - 1
if idx < 0 or idx >= len(apou_outputs):
self.cli.error("无效选择")
return
selected = apou_outputs[idx]
except (ValueError, KeyboardInterrupt):
print()
self.cli.warning("已取消")
return
# 运行 DoPJ
self._run_dopj_for_user(selected["username"], selected["path"])
def _run_dopj_for_user(self, username: str, posts_file: str):
"""为指定用户运行 DoPJ"""
assert self.config is not None
from DoPJ.cli import DoPJRunner
user_dir = self.config.get_user_dir(username)
self.cli.print_config([
("目标用户", username),
("输入文件", posts_file),
("输出目录", str(user_dir)),
("并发线程", str(self.config.dopj.threads)),
])
# 构建账户配置
config_dict = {
"accounts": [
{"name": acc.name, "bduss": acc.bduss}
for acc in self.config.accounts
],
"min_interval": self.config.dopj.min_interval,
"max_fails": self.config.dopj.max_fails,
}
try:
runner = DoPJRunner(
input_json=posts_file,
config_dict=config_dict,
output_dir=str(user_dir),
threads=self.config.dopj.threads,
max_retries=self.config.dopj.max_retries,
cli=self.cli,
)
# 默认启用增量模式(跳过已存档的帖子)
runner.load_tasks(incremental=True)
runner.run()
except Exception as e:
self.cli.error(f"爬取失败: {e}")
def show_users(self):
"""显示用户列表"""
self.cli.print_section("已存档用户")
users = config_manager.list_users()
if not users:
print(f" {Colors.GRAY}暂无数据{Colors.RESET}")
return
# 表格显示
headers = ["用户名", "发言数", "帖子数", "状态"]
rows = []
for user in users:
if user["has_index"]:
status = "已完成"
elif user["has_posts"]:
status = "待详情"
else:
status = "空目录"
rows.append([
user["name"],
str(user["posts_count"]),
str(user["threads_count"]),
status,
])
self.cli.print_table(headers, rows, ["cyan", "white", "white", "green"])
print()
# 选择用户查看详情或操作
try:
choice = input(
f"{Colors.CYAN}? 输入用户名查看详情,或按回车返回: {Colors.RESET}"
).strip()
if choice:
self._show_user_detail(choice)
except (EOFError, KeyboardInterrupt):
print()
def _show_user_detail(self, username: str):
"""显示用户详情"""
assert self.config is not None
user_dir = self.config.get_user_dir(username)
if not user_dir.exists():
self.cli.error(f"用户 {username} 不存在")
return
self.cli.print_section(f"用户详情: {username}")
# 显示文件列表
print(f" {Colors.GRAY}目录: {user_dir}{Colors.RESET}")
print()
for item in sorted(user_dir.iterdir()):
if item.is_file():
size = item.stat().st_size
size_str = f"{size/1024:.1f} KB" if size > 1024 else f"{size} B"
print(f" {Colors.WHITE}{item.name}{Colors.RESET} {Colors.DIM}({size_str}){Colors.RESET}")
elif item.is_dir():
count = len(list(item.iterdir()))
print(f" {Colors.CYAN}{item.name}/{Colors.RESET} {Colors.DIM}({count} 项){Colors.RESET}")
def show_settings(self):
"""显示设置"""
assert self.config is not None
self.cli.print_section("配置设置")
self.cli.print_config([
("配置文件", self.config.config_path),
("数据目录", self.config.database_dir),
("账户数量", str(len(self.config.accounts))),
])
print()
print(f" {Colors.GRAY}APoU 设置:{Colors.RESET}")
print(f" 页间延迟: {self.config.apou.page_delay} 秒")
print(f" 重试次数: {self.config.apou.max_retries}")
print()
print(f" {Colors.GRAY}DoPJ 设置:{Colors.RESET}")
print(f" 并发线程: {self.config.dopj.threads}")
print(f" 请求间隔: {self.config.dopj.min_interval} 秒")
print()
print(f" {Colors.GRAY}账户列表:{Colors.RESET}")
for acc in self.config.accounts:
bduss_preview = acc.bduss[:20] + "..." if len(acc.bduss) > 20 else acc.bduss
valid = is_valid_bduss(acc.bduss)
status = f"{Colors.GREEN}有效{Colors.RESET}" if valid else f"{Colors.RED}无效{Colors.RESET}"
print(f" {acc.name}: {bduss_preview} [{status}]")
print()
print(f" {Colors.DIM}编辑 {self.config.config_path} 修改配置{Colors.RESET}")
def main():
"""主入口"""
menu = MainMenu()
menu.run()
if __name__ == "__main__":
main()