-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.py
More file actions
64 lines (58 loc) · 2.85 KB
/
Copy pathmain.py
File metadata and controls
64 lines (58 loc) · 2.85 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
'''
Author: 未来可欺 2513502304@qq.com
Date: 2025-01-14 02:27:22
LastEditors: 未来可欺 2513502304@qq.com
LastEditTime: 2025-08-03 13:31:00
Description: 支持任意历史时间筛选的 bilibili 弹幕爬虫
'''
import time
import settings
from crawl import get_history_danmaku
from storage import dump_history_danmaku
from utils import logger
def main():
try: # 尝试获取记录信息
with open(f'{settings.save_dir}/{settings.task}.seen', mode='r', encoding='utf-8') as f:
seen: list[str] = f.read().split('\n')[:-1:] # ignore the last empty string: ''
except FileNotFoundError as e: # 初始化记录信息
seen: list[str] = []
# aid 不为空
if settings.aids:
for aid, name in zip(settings.aids, settings.save_name):
# 当前 aid 在记录中,跳过爬取
if aid in seen:
logger.info(f'{aid} 已在当前记录中,将跳过该记录')
continue
time.sleep(settings.delay) # 反爬
# 从给定的 aid/bvid 与时间段中获取历史弹幕
danmaku = get_history_danmaku(aid=aid, page=settings.page, cookies=settings.cookies, start=settings.start, end=settings.end, delay=settings.delay)
# 若弹幕记录不为 0,则将其转存
if danmaku:
# 转存为指定格式
dump_history_danmaku(danmaku, save_name=name, save_dir=settings.save_dir, file_format=settings.file_format)
# 记录当前信息
seen.append(aid)
with open(f'{settings.save_dir}/{settings.task}.seen', mode='a', encoding='utf-8') as f:
f.write(aid + '\n')
# bvid 不为空
elif settings.bvids:
for bvid, name in zip(settings.bvids, settings.save_name):
# 当前 bvid 在记录中,跳过爬取
if bvid in seen:
logger.info(f'{bvid} 已在当前记录中,将跳过该记录')
continue
time.sleep(settings.delay) # 反爬
# 从给定的 aid/bvid 与时间段中获取历史弹幕
danmaku = get_history_danmaku(bvid=bvid, page=settings.page, cookies=settings.cookies, start=settings.start, end=settings.end, delay=settings.delay)
# 若弹幕记录不为 0,则将其转存
if danmaku:
# 转存为指定格式
dump_history_danmaku(danmaku, save_name=name, save_dir=settings.save_dir, file_format=settings.file_format)
# 记录当前信息
seen.append(bvid)
with open(f'{settings.save_dir}/{settings.task}.seen', mode='a', encoding='utf-8') as f:
f.write(bvid + '\n')
else:
raise ValueError('请配置 settings 中视频的 aids/bvids 参数')
if __name__ == "__main__":
main()