You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
1.6 KiB

import os
import threading
from getdm import get_videos_url, get_danmu, get_web_url
# 定义全局锁
lock = threading.Lock()
def getalldm(videos_url, savepath):
with open(savepath, 'w', encoding='utf-8') as file:
for item in videos_url:
res = get_danmu(item)
with lock: # 确保写入操作是互斥的
for result in res:
file.write(f"{result}\n ")
file.write("\n")
def get_danmu_thread(item, savepath):
res = get_danmu(item)
with lock:
with open(savepath, 'a', encoding='utf-8') as file: # 以追加模式打开文件
for result in res:
file.write(f"{result}\n ")
file.write("\n")
def main():
# 获取所有页面的 URL
pages = 10
keyword = '2024巴黎奥运会'
webs_url = get_web_url(keyword, pages)
# 获取所有视频的 URL
videos_url = []
for item in webs_url:
now = get_videos_url(item)
videos_url.extend(now)
# 定义保存弹幕的文件路径
res_dir = os.path.join(os.getcwd(), 'res')
if not os.path.exists(res_dir):
os.makedirs(res_dir)
file_name = "total_final_fast.txt"
savepath = os.path.join(res_dir, file_name)
# 创建并启动线程
threads = []
for item in videos_url:
thread = threading.Thread(target=get_danmu_thread, args=(item, savepath))
threads.append(thread)
thread.start()
# 等待所有线程完成
for thread in threads:
thread.join()
print("所有视频弹幕已处理完成。")
if __name__ == '__main__':
main()