99 lines
3.6 KiB
Python
99 lines
3.6 KiB
Python
import asyncio
|
|
import re
|
|
import time
|
|
|
|
import httpx
|
|
import pymysql
|
|
|
|
base_url = "https://dm.video.qq.com/barrage/base/"
|
|
dm_url = "https://dm.video.qq.com/barrage/segment/p0047eb164c/t/v1/0/30000"
|
|
|
|
|
|
async def getTxVideoData(url, video_id):
|
|
# 使用正则表达式获取视频ID p0047eb164c https://v.qq.com/x/cover/mzc00200pppnnhl/p0047eb164c.html
|
|
global connection
|
|
global cursor
|
|
vid = re.findall(r'/([a-zA-Z0-9]+)\.html', url)[0]
|
|
print(vid)
|
|
url = f"https://dm.video.qq.com/barrage/base/{vid}" # 替换成你要请求的JSON数据的URL
|
|
json_data = await fetch_json(url)
|
|
if json_data:
|
|
# 在这里处理解析后的JSON数据
|
|
span = json_data['segment_span']
|
|
print(span)
|
|
items = json_data["segment_index"].items()
|
|
page = len(items)
|
|
# 最大数据索引
|
|
max_data_index = (page - 1) * 30000
|
|
# 页数
|
|
print(page)
|
|
# 获取的数据量
|
|
data_num = 5000
|
|
# 每页获取的数据量
|
|
number_per_page = int(data_num / page)
|
|
print(number_per_page)
|
|
total_num = 0
|
|
# 连接数据库
|
|
try:
|
|
# 连接数据库
|
|
connection = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='root', db='dm')
|
|
cursor = connection.cursor()
|
|
except pymysql.Error as e:
|
|
# 异常 连接失败
|
|
print("数据库连接失败")
|
|
# finally:
|
|
# # 关闭数据库连接
|
|
# if connection:
|
|
# connection.close()
|
|
for i in range(0, page):
|
|
data_url_suffix = json_data["segment_index"][str(i * 30000)]["segment_name"]
|
|
data_url = f"https://dm.video.qq.com/barrage/segment/{vid}/{data_url_suffix}"
|
|
print(data_url)
|
|
data_json = await fetch_json(data_url)
|
|
if data_json:
|
|
barrage_list = data_json["barrage_list"]
|
|
data_len = len(barrage_list)
|
|
timestamp = int(time.time())
|
|
# 遍历barrage_list
|
|
sql = "INSERT INTO `danmaku_list` (`id`, `type`, `text`, `color`, `size`, `videotime`, `ip`, `time`) VALUES "
|
|
data_index = 0
|
|
for barrage in barrage_list:
|
|
content = barrage["content"].replace("\\", "").replace("'", "\\'")
|
|
time_offset = float(barrage["time_offset"]) / 1000
|
|
if data_index == 0:
|
|
sql += f"('{video_id}', 'right', '{content}','#FFFFFF', '27.5px', {time_offset}, '127.0.0.1', {timestamp})"
|
|
else:
|
|
sql += f",('{video_id}', 'right', '{content}','#FFFFFF', '27.5px', {time_offset}, '127.0.0.1', {timestamp})"
|
|
data_index += 1
|
|
total_num += data_len
|
|
# print(sql)
|
|
# cursor.execute(sql)
|
|
# connection.commit()
|
|
|
|
print(f"总数据量:{total_num}")
|
|
|
|
|
|
async def fetch_json(url):
|
|
async with httpx.AsyncClient() as client:
|
|
response = await client.get(url)
|
|
return response.json()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
url = "https://v.qq.com/x/cover/mzc00200pppnnhl/j00468dc2og.html"
|
|
if "v.qq.com" in url:
|
|
# 调用 v.qq.com 对应的接口
|
|
asyncio.run(getTxVideoData(url, "j00468dc2og"))
|
|
# 检查是否存在 "rainss.cn"
|
|
elif "rainss.cn" in url:
|
|
# 调用 rainss.cn 对应的接口
|
|
print("调用 rainss.cn 的接口")
|
|
# 检查是否存在 "allms.cn"
|
|
elif "allms.cn" in url:
|
|
# 调用 allms.cn 对应的接口
|
|
print("调用 allms.cn 的接口")
|
|
|
|
# 如果都不匹配
|
|
else:
|
|
print("未匹配到任何子字符串")
|