-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweibo_tool.py
More file actions
540 lines (426 loc) · 14.9 KB
/
weibo_tool.py
File metadata and controls
540 lines (426 loc) · 14.9 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
"""
微博批量操作 CLI 工具
功能:
1. 批量将微博设为仅自己可见
作者:WeiBoHideTool
"""
import requests
import time
import math
import threading
from concurrent.futures import ThreadPoolExecutor, as_completed
# ======================
# 全局配置
# ======================
# 请求间隔(秒),避免请求过于频繁被限流
SLEEP = 1.5
# 并发线程数(用于多线程处理)
THREADS = 5
# 线程安全的计数器
class ThreadSafeCounter:
"""线程安全的计数器"""
def __init__(self):
self._value = 0
self._lock = threading.Lock()
def increment(self):
with self._lock:
self._value += 1
@property
def value(self):
with self._lock:
return self._value
# ======================
# 初始化配置
# ======================
def get_user_config():
"""
获取用户配置(UID 和 Cookie)
Returns:
tuple: (uid, cookie_string)
"""
uid = input("输入微博UID: ").strip()
cookie_string = input("粘贴完整Cookie: ").strip()
return uid, cookie_string
def parse_cookie(cookie_str):
"""
将 Cookie 字符串解析为字典
Args:
cookie_str: Cookie 字符串,格式为 "key1=value1; key2=value2"
Returns:
dict: Cookie 字典
"""
cookies = {}
for item in cookie_str.split(";"):
if "=" in item:
k, v = item.strip().split("=", 1)
cookies[k] = v
return cookies
def build_headers(uid, cookie):
"""
构建请求头
Args:
uid: 微博用户 ID
cookie: Cookie 字典
Returns:
dict: 请求头字典
"""
return {
"accept": "application/json, text/plain, */*",
"accept-language": "zh-CN,zh;q=0.9",
"client-version": "3.0.0",
"content-type": "application/x-www-form-urlencoded",
"origin": "https://weibo.com",
"referer": f"https://weibo.com/u/{uid}",
"sec-ch-ua": '"Chromium";v="146", "Not-A.Brand";v="24", "Google Chrome";v="146"',
"sec-ch-ua-mobile": "?0",
"sec-ch-ua-platform": '"macOS"',
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "same-origin",
"server-version": "v2026.03.13.2",
"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/146.0.0.0 Safari/537.36",
"x-requested-with": "XMLHttpRequest",
"x-xsrf-token": cookie.get("XSRF-TOKEN", "")
}
def validate_config(uid, cookie, headers):
"""
校验 UID 和 Cookie 是否有效
通过发送一个简单的请求来验证配置是否正确
Args:
uid: 微博用户 ID
cookie: Cookie 字典
headers: 请求头
Returns:
tuple: (is_valid, error_message)
"""
print("\n校验配置中...")
# 1. 校验 UID 格式
if not uid:
return False, "UID 不能为空"
if not uid.isdigit():
return False, f"UID 格式错误: 应为纯数字,当前为 '{uid}'"
# 2. 校验 Cookie 必要字段
required_cookies = ["SUBP"]
missing = [k for k in required_cookies if k not in cookie or not cookie[k]]
if missing:
return False, f"Cookie 缺少必要字段: {', '.join(missing)}"
# 3. 发送请求验证
try:
url = "https://weibo.com/ajax/statuses/mymblog"
params = {"uid": uid, "page": 1, "feature": 0}
r = requests.get(url, headers=headers, cookies=cookie, params=params, timeout=10)
if r.status_code == 403:
return False, "Cookie 已过期或无效,请重新获取"
elif r.status_code == 400:
return False, f"请求参数错误,请检查 UID 是否正确"
elif r.status_code != 200:
return False, f"请求失败: HTTP {r.status_code}"
data = r.json()
# 检查响应内容
if data.get("ok") == 0:
msg = data.get("msg", "未知错误")
return False, f"API 返回错误: {msg}"
if "data" not in data:
return False, "响应数据格式异常,可能是 Cookie 无效"
print("✓ 配置校验通过")
return True, None
except requests.exceptions.Timeout:
return False, "请求超时,请检查网络连接"
except requests.exceptions.ConnectionError:
return False, "网络连接失败,请检查网络"
except Exception as e:
return False, f"校验失败: {e}"
# ======================
# 获取微博列表
# ======================
def fetch_page(uid, headers, cookie, page, since_id=None, retry=3):
"""
获取单页微博列表
API: https://weibo.com/ajax/statuses/mymblog
Args:
uid: 微博用户 ID
headers: 请求头
cookie: Cookie 字典
page: 页码(从 1 开始)
since_id: 分页参数,上一页返回的 since_id
retry: 重试次数
Returns:
dict | None: API 响应数据,失败返回 None
"""
url = "https://weibo.com/ajax/statuses/mymblog"
params = {
"uid": uid,
"page": page,
"feature": 0
}
if since_id:
params["since_id"] = since_id
for attempt in range(retry):
try:
r = requests.get(url, headers=headers, cookies=cookie, params=params)
if r.status_code != 200:
print(f" 请求失败 [{attempt+1}/{retry}]: HTTP {r.status_code}")
if attempt < retry - 1:
time.sleep(3)
continue
return None
return r.json()
except Exception as e:
print(f" 解析错误 [{attempt+1}/{retry}]: {e}")
if attempt < retry - 1:
time.sleep(3)
continue
return None
return None
def get_all_weibo_ids(uid, headers, cookie):
"""
获取所有微博 ID 列表
Args:
uid: 微博用户 ID
headers: 请求头
cookie: Cookie 字典
Returns:
list: 微博 ID 列表
"""
page = 1
since_id = None
ids = []
while True:
print(f"读取第 {page} 页...")
data = fetch_page(uid, headers, cookie, page, since_id)
if not data or "data" not in data:
break
blogs = data["data"].get("list", [])
if not blogs:
break
for blog in blogs:
ids.append(blog["id"])
since_id = data["data"].get("since_id")
page += 1
time.sleep(SLEEP)
return ids
# ======================
# 修改微博可见性
# ======================
def set_private(wid, headers, cookie, retry=3, silent=False):
"""
将单条微博设为仅自己可见
API: https://weibo.com/ajax/statuses/modifyVisible
Args:
wid: 微博 ID
headers: 请求头
cookie: Cookie 字典
retry: 重试次数
silent: 是否静默模式(不打印日志)
Returns:
tuple: (success: bool, wid: int)
"""
url = "https://weibo.com/ajax/statuses/modifyVisible"
data = {"ids": wid, "visible": 1}
for attempt in range(retry):
try:
r = requests.post(url, headers=headers, cookies=cookie, data=data)
if r.status_code != 200:
if not silent:
print(f" [{wid}] 请求失败: HTTP {r.status_code}")
if attempt < retry - 1:
time.sleep(2)
continue
return False, wid
result = r.json()
if result.get("ok") == 1:
if not silent:
print(f" [{wid}] ✓ 已设为仅自己可见")
return True, wid
else:
if not silent:
print(f" [{wid}] ✗ 设置失败: {result}")
return False, wid
except Exception as e:
if not silent:
print(f" [{wid}] 异常: {e}")
if attempt < retry - 1:
time.sleep(2)
continue
return False, wid
return False, wid
def set_private_batch_task(wid, headers, cookie, success_counter, fail_counter):
"""
多线程任务:设置单条微博可见性并更新计数器
Args:
wid: 微博 ID
headers: 请求头
cookie: Cookie 字典
success_counter: 成功计数器
fail_counter: 失败计数器
Returns:
bool: 是否成功
"""
success, _ = set_private(wid, headers, cookie, silent=True)
if success:
success_counter.increment()
print(f" [{wid}] ✓")
else:
fail_counter.increment()
print(f" [{wid}] ✗")
time.sleep(SLEEP)
return success
def get_weibo_stats(uid, headers, cookie, max_pages=None):
"""
获取微博统计信息(总数和总页数)
从第一页 API 响应中获取 total 字段,计算总页数
Args:
uid: 微博用户 ID
headers: 请求头
cookie: Cookie 字典
max_pages: 最大处理页数限制,None 表示不限制
Returns:
tuple: (total_count, total_pages, actual_pages)
actual_pages 是实际要处理的页数(考虑 max_pages 限制)
"""
print("\n获取微博统计信息...")
data = fetch_page(uid, headers, cookie, 1)
if not data or "data" not in data:
return 0, 0, 0
# 从 API 响应获取总数
total_count = data["data"].get("total", 0)
# 每页 20 条,计算总页数
PAGE_SIZE = 20
total_pages = math.ceil(total_count / PAGE_SIZE) if total_count > 0 else 0
# 计算实际要处理的页数
actual_pages = min(total_pages, max_pages) if max_pages else total_pages
return total_count, total_pages, actual_pages
def batch_set_private(uid, headers, cookie, max_pages=None, use_threads=True):
"""
批量将微博设为仅自己可见
每获取一页就处理一页,自动跳过已是仅自己可见的微博
Args:
uid: 微博用户 ID
headers: 请求头
cookie: Cookie 字典
max_pages: 最大处理页数,None 表示不限制
use_threads: 是否使用多线程处理
"""
# 获取微博统计信息
total_count, total_pages, actual_pages = get_weibo_stats(uid, headers, cookie, max_pages)
if total_count == 0:
print("\n没有找到任何微博")
return
print("\n" + "=" * 50)
print("微博统计")
print(f" 总微博数: {total_count}")
print(f" 总页数: {total_pages}")
if max_pages:
print(f" 实际处理: {actual_pages} 页(已限制)")
print(f" 并发线程: {THREADS}" if use_threads else " 模式: 单线程")
print("=" * 50)
# 确认是否继续
confirm = input("\n是否继续执行批量设置? (y/n): ").strip().lower()
if confirm != "y":
print("已取消操作")
return
page = 1
since_id = None
# 使用线程安全的计数器
success_counter = ThreadSafeCounter()
skip_counter = ThreadSafeCounter()
fail_counter = ThreadSafeCounter()
print("\n" + "=" * 50)
print("开始批量设置微博为仅自己可见")
print("=" * 50)
while True:
print(f"\n[第 {page} 页 / {actual_pages} 页]")
data = fetch_page(uid, headers, cookie, page, since_id)
if not data or "data" not in data:
print(" 获取数据失败或无更多数据")
break
blogs = data["data"].get("list", [])
if not blogs:
print(" 无更多微博")
break
# 筛选需要处理的微博
to_process = []
for blog in blogs:
visible_type = blog.get("visible", {}).get("type", 0)
if visible_type == 1:
skip_counter.increment()
print(f" [{blog['id']}] ⊘ 跳过(已仅自己可见)")
else:
to_process.append(blog["id"])
# 处理当前页
if to_process:
if use_threads and len(to_process) > 1:
# 多线程处理
print(f" 使用 {THREADS} 个线程并行处理 {len(to_process)} 条微博...")
with ThreadPoolExecutor(max_workers=THREADS) as executor:
futures = [
executor.submit(
set_private_batch_task,
wid, headers, cookie,
success_counter, fail_counter
)
for wid in to_process
]
# 等待所有任务完成
for future in as_completed(futures):
pass
else:
# 单线程处理
for wid in to_process:
success, _ = set_private(wid, headers, cookie)
if success:
success_counter.increment()
else:
fail_counter.increment()
time.sleep(SLEEP)
since_id = data["data"].get("since_id")
page += 1
if page > actual_pages:
print(f"\n已完成所有页数处理")
break
# 页间间隔
time.sleep(SLEEP)
# 打印统计
print("\n" + "=" * 50)
print("处理完成")
print(f" ✓ 成功: {success_counter.value}")
print(f" ⊘ 跳过: {skip_counter.value}")
print(f" ✗ 失败: {fail_counter.value}")
print("=" * 50)
# ======================
# 主菜单
# ======================
def main():
"""主程序入口"""
print("""
╔══════════════════════════════════════╗
║ 微博批量操作工具 v1.0 ║
╠══════════════════════════════════════╣
║ 1. 批量设为仅自己可见 ║
║ 2. 退出 ║
╚══════════════════════════════════════╝
""")
# 获取用户配置
uid, cookie_string = get_user_config()
cookie = parse_cookie(cookie_string)
headers = build_headers(uid, cookie)
# 校验配置
is_valid, error_msg = validate_config(uid, cookie, headers)
if not is_valid:
print(f"\n✗ 配置校验失败: {error_msg}")
print("请检查 UID 和 Cookie 后重试")
return
while True:
print()
choice = input("请选择功能 [1-2]: ").strip()
if choice == "1":
max_pages = input("最大处理页数(留空不限制): ").strip()
max_pages = int(max_pages) if max_pages else None
batch_set_private(uid, headers, cookie, max_pages)
elif choice == "2":
print("再见!")
break
else:
print("无效选择,请重试")
if __name__ == "__main__":
main()