mirror of
https://github.com/ClovertaTheTrilobita/SanYeCao-Nonebot.git
synced 2026-04-01 22:04:51 +00:00
新增B站搜索视频详情的功能
修复音乐名被qq识别为url的问题
This commit is contained in:
parent
6f0d35584d
commit
cfe0567210
5 changed files with 103 additions and 1 deletions
|
|
@ -5,6 +5,7 @@ nonebot2[httpx]
|
||||||
nonebot2[websockets]
|
nonebot2[websockets]
|
||||||
|
|
||||||
sqlalchemy
|
sqlalchemy
|
||||||
|
selenium
|
||||||
autopep8
|
autopep8
|
||||||
graiax-silkcoder[ffmpeg]
|
graiax-silkcoder[ffmpeg]
|
||||||
Crypto
|
Crypto
|
||||||
|
|
|
||||||
39
src/qq_plugins/bili_vid_search.py
Normal file
39
src/qq_plugins/bili_vid_search.py
Normal file
|
|
@ -0,0 +1,39 @@
|
||||||
|
# https://api.bilibili.com/x/web-interface/search/type?keyword=av28465342&search_type=video&page=1
|
||||||
|
|
||||||
|
import time
|
||||||
|
from nonebot import on_command
|
||||||
|
from nonebot.rule import to_me
|
||||||
|
from nonebot.adapters.qq import MessageSegment,MessageEvent, Message
|
||||||
|
import src.videos.billibili.biliVideos as biliVideos
|
||||||
|
|
||||||
|
bili_vid = on_command("B站搜索",rule=to_me(), priority=10, block=True)
|
||||||
|
@bili_vid.handle()
|
||||||
|
async def get_bili_vid_info(message: MessageEvent):
|
||||||
|
content = message.get_plaintext().replace("/B站搜索", "").strip()
|
||||||
|
response = biliVideos.get(content)
|
||||||
|
if response['code'] != 0:
|
||||||
|
bili_vid.finish(response['message'])
|
||||||
|
search_result = response['data']['result']
|
||||||
|
|
||||||
|
i = 0
|
||||||
|
for vid_info in search_result:
|
||||||
|
i += 1
|
||||||
|
if i >= 4:
|
||||||
|
break
|
||||||
|
pic = "https:" + str(vid_info['pic'])
|
||||||
|
# print(pic)
|
||||||
|
if vid_info['description'].strip() == "":
|
||||||
|
dis = "无"
|
||||||
|
else:
|
||||||
|
dis = vid_info['description']
|
||||||
|
description = ("\n标题: " + str(vid_info['title']).replace('<em class="keyword">', "").replace('</em>', "") +
|
||||||
|
"\nup主: " + vid_info['author'] +
|
||||||
|
"\n" + vid_info['bvid'])
|
||||||
|
msg = Message([
|
||||||
|
MessageSegment.image(pic),
|
||||||
|
MessageSegment.text(description),
|
||||||
|
])
|
||||||
|
await bili_vid.send(msg)
|
||||||
|
time.sleep(0.5)
|
||||||
|
|
||||||
|
await bili_vid.finish(f"展示{len(search_result)}条结果中的前3条。")
|
||||||
|
|
@ -20,7 +20,7 @@ with open(os.getcwd() + '/src/ai_chat/config/chat_ai.yaml', 'r', encoding='utf-8
|
||||||
|
|
||||||
|
|
||||||
menu = ['/今日运势','/图','/点歌','/摸摸头','/群老婆','/今日老婆', "/开启ai","/关闭ai","/角色列表","/添加人设", "/更新人设", "/删除人设", "/切换人设", "/管理员注册",
|
menu = ['/今日运势','/图','/点歌','/摸摸头','/群老婆','/今日老婆', "/开启ai","/关闭ai","/角色列表","/添加人设", "/更新人设", "/删除人设", "/切换人设", "/管理员注册",
|
||||||
'/待办', '/test','/天气','我喜欢你', "❤", "/待办查询", "/新建待办", "/删除待办" ,"/cf", "/奶龙", "/repo", "/info"]
|
'/待办', '/test','/天气','我喜欢你', "❤", "/待办查询", "/新建待办", "/删除待办" ,"/cf", "/奶龙", "/repo", "/info", "/B站搜索"]
|
||||||
|
|
||||||
|
|
||||||
async def check_value_in_menu(message: MessageEvent) -> bool:
|
async def check_value_in_menu(message: MessageEvent) -> bool:
|
||||||
|
|
|
||||||
|
|
@ -48,6 +48,7 @@ async def handle_function(msg: MessageEvent):
|
||||||
|
|
||||||
#搜索歌曲
|
#搜索歌曲
|
||||||
song_id,song_name,singer,song_url = netease_music_search(keyword,session)
|
song_id,song_name,singer,song_url = netease_music_search(keyword,session)
|
||||||
|
song_name = str(song_name).replace(".", "·")
|
||||||
if song_id is None:
|
if song_id is None:
|
||||||
await music.finish("\n没有找到歌曲,或检索到的歌曲均为付费喔qwq\n这绝对不是我的错,绝对不是!")
|
await music.finish("\n没有找到歌曲,或检索到的歌曲均为付费喔qwq\n这绝对不是我的错,绝对不是!")
|
||||||
else:
|
else:
|
||||||
|
|
|
||||||
61
src/videos/billibili/biliVideos.py
Normal file
61
src/videos/billibili/biliVideos.py
Normal file
|
|
@ -0,0 +1,61 @@
|
||||||
|
import os
|
||||||
|
import pickle
|
||||||
|
|
||||||
|
import requests
|
||||||
|
import hashlib
|
||||||
|
import urllib.parse
|
||||||
|
from selenium import webdriver
|
||||||
|
from selenium.webdriver.chrome.options import Options
|
||||||
|
|
||||||
|
chrome_options = Options()
|
||||||
|
chrome_options.add_argument("--headless")
|
||||||
|
|
||||||
|
if not os.path.exists('bili.cookie'):
|
||||||
|
# 使用Selenium模拟浏览器获取Cookie
|
||||||
|
driver = webdriver.Chrome(options=chrome_options)
|
||||||
|
driver.get("https://www.bilibili.com")
|
||||||
|
# cookies = driver.get_cookies()
|
||||||
|
with open('bili.cookie', 'wb') as f:
|
||||||
|
pickle.dump(driver.get_cookies(), f)
|
||||||
|
driver.quit()
|
||||||
|
|
||||||
|
cookies = pickle.load(open('bili.cookie', 'rb'))
|
||||||
|
|
||||||
|
headers = {
|
||||||
|
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36",
|
||||||
|
"referer": "https://www.bilibili.com/",
|
||||||
|
"Cookie": "; ".join([f"{c['name']}={c['value']}" for c in cookies])
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
appkey = '1d8b6e7d45233436'
|
||||||
|
appsec = '560c52ccd288fed045859ed18bffd973'
|
||||||
|
params = {
|
||||||
|
'search_type':'video'
|
||||||
|
}
|
||||||
|
|
||||||
|
def appsign(params, appkey, appsec):
|
||||||
|
"""为请求参数进行 APP 签名"""
|
||||||
|
params.update({'appkey': appkey})
|
||||||
|
params = dict(sorted(params.items())) # 按照 key 重排参数
|
||||||
|
query = urllib.parse.urlencode(params) # 序列化参数
|
||||||
|
sign = hashlib.md5((query+appsec).encode()).hexdigest() # 计算 api 签名
|
||||||
|
params.update({'sign':sign})
|
||||||
|
return params
|
||||||
|
|
||||||
|
|
||||||
|
def get(keyword):
|
||||||
|
signed_params = appsign(params, appkey, appsec)
|
||||||
|
query = urllib.parse.urlencode(signed_params)
|
||||||
|
url = f"https://api.bilibili.com/x/web-interface/search/type?keyword={keyword}&"
|
||||||
|
session = requests.session()
|
||||||
|
session.get("https://www.bilibili.com/", headers=headers)
|
||||||
|
response = session.get(url + query, headers=headers).json()
|
||||||
|
# print(response['code'])
|
||||||
|
return response
|
||||||
|
# print(signed_params)
|
||||||
|
# print(query)
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
print(get('海南某211台风过后现状111'))
|
||||||
Loading…
Reference in a new issue