-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
174 lines (148 loc) · 7.16 KB
/
main.py
File metadata and controls
174 lines (148 loc) · 7.16 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
from network.core.builder import QZoneNetworkBuilder
from network.core.visualizer import NetworkVisualizer
from network.core.personal import PersonalNetworkGenerator
from qqzone.qr_login import QQZoneCookieManager
from qqzone.qzone_qq import QzoneCrawler
from qqzone.member_manager import MemberManager
import json
def get_depths(depths_input):
"""将输入的深度字符串转换为整数列表"""
try:
# 将输入的字符串分割成列表,并转换为整数
depths = [int(d) for d in depths_input.split(',')]
return depths
except ValueError:
print("输入的深度列表包含非整数值,请重新输入。")
return None
def main():
# 初始化网络构建器和成员管理器
builder = QZoneNetworkBuilder()
dm = MemberManager()
while True:
print("\n===== QQ空间社交网络分析系统 =====")
print("1. Cookie管理")
print("2. 构建社交网络")
print("3. 可视化网络")
print("4. 生成个人关系图")
print("5. 从群成员文件加载QQ号")
print("6. 批量爬取数据")
print("7. 退出")
choice = input("请选择操作 (1-7): ").strip()
if choice == "1":
# Cookie管理功能
while True:
print("\n===== QQ空间Cookie管理器 =====")
print("1. 扫码登录添加新Cookie")
print("2. 查看所有Cookie")
print("3. 使用指定Cookie")
print("4. 删除Cookie")
print("5. 手动添加cookie")
print("6. 返回主菜单")
sub_choice = input("请选择操作 (1-6): ").strip()
if sub_choice == "1":
name = input("输入账号备注名称 (可选): ").strip()
manager.get_cookie_by_qrcode(name)
elif sub_choice == "2":
manager.list_cookies()
if manager.cookies:
idx = input("输入编号查看详情 (直接回车返回): ").strip()
if idx.isdigit():
cookie = manager.get_cookie(int(idx))
print(json.dumps(cookie, indent=4, ensure_ascii=False))
elif sub_choice == "3":
manager.list_cookies()
if manager.cookies:
idx = input("选择要使用的Cookie编号: ").strip()
if idx.isdigit():
cookie = manager.get_cookie(int(idx))
print(f"\n已选择: {cookie['account']}")
print("示例使用方式:")
print(f"uin = '{cookie['uin']}'")
print(f"skey = '{cookie['skey']}'")
elif sub_choice == "4":
manager.list_cookies()
if manager.cookies:
idx = input("选择要删除的Cookie编号: ").strip()
if idx.isdigit():
manager.delete_cookie(int(idx))
elif sub_choice == "5":
manual_cookie = input("输入手动获取的cookie ").strip()
manager.add_cookie(manual_cookie)
elif sub_choice == "6":
break
else:
print("无效输入,请重新选择")
elif choice == "2":
# 构建完整网络
builder.build_network()
print("社交网络构建完成!")
elif choice == "3":
# 可视化完整网络
NetworkVisualizer.visualize(
builder.graph,
builder.user_profiles,
builder.interactions
)
print("网络可视化完成!")
elif choice == "4":
# 生成个人关系图
target_uin = input("请输入目标QQ号: ").strip()
depths_input = input("请选择深度列表 [1,2,3,5....]: ").strip()
depths = get_depths(depths_input)
if depths is None:
print("未能正确解析深度列表,请重新运行程序。")
return
for depth in depths:
personal_graph = PersonalNetworkGenerator.generate(builder, target_uin, depth)
output_file = NetworkVisualizer.visualize(
personal_graph,
builder.user_profiles,
builder.interactions,
is_personal=True,
use_layout='default'
)
print(f"深度 {depth} 的个人关系图已保存到: {output_file}")
elif choice == "5":
# 从群成员文件加载QQ号
group_file = input("请输入群成员文件路径 (默认: data/group_member/qq_member.json): ").strip()
group_file = group_file if group_file else "data/group_member/qq_member.json"
try:
with open(group_file, 'r', encoding='utf-8') as f:
members = json.load(f)
qq_numbers = [m["user_id"] for m in members["data"]]
for qq_number in qq_numbers:
if dm.add_target(qq_number):
print(f"{qq_number} 添加访问列表成功")
else:
print(f"{qq_number} 已经添加过了")
print(f"共加载了 {len(qq_numbers)} 个QQ号")
except Exception as e:
print(f"加载群成员文件失败: {e}")
elif choice == "6":
# 批量爬取数据
if not manager.cookies:
print("请先添加有效的Cookie!")
continue
print("开始检验cookie是否可用...ovo")
cookie = manager.get_cookie(1)
crawler = QzoneCrawler(cookie)
# 检查cookie是否有效
if not crawler.check_cookie_valid(cookie['uin'][1:]):
manager.delete_cookie(1)
print("Cookie已失效,请重新添加")
continue
print(f"使用账号: {cookie['account']} ({cookie['uin'][1:]}) 进行爬取...")
target_number = dm.list_target()
limit = input(f"共有 {len(target_number)} 个目标,请输入爬取数量 (默认10): ").strip()
limit = int(limit) if limit.isdigit() else 10
crawler.batch_crawl(target_number[:limit])
elif choice == "7":
print("退出程序")
break
else:
print("无效输入,请重新选择")
if __name__ == "__main__":
# 初始化管理器
manager = QQZoneCookieManager()
dm = MemberManager()
main()