Skip to content

Commit

Permalink
🎈 pref: 提升健壮性 & 添加自定义名称
Browse files Browse the repository at this point in the history
1. 删除不必要的逻辑
2. try-catch一些容易出问题的地方,并保证删除垃圾文件
3. 现在可以更改`极速版`
  • Loading branch information
zhiyu1998 committed Jul 13, 2024
1 parent 258283e commit 6908e2d
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 70 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ apt-get install atomicparsley
在 nonebot2 项目的`.env`文件中添加下表中的可选配置

```
DOUYIN_CK='' # douyin's cookie,最佳实践教程:https://thumbsnap.com/rKxUGKqp ,格式:odin_tt=xxx;passport_fe_beating_status=xxx;sid_guard=xxx;uid_tt=xxx;uid_tt_ss=xxx;sid_tt=xxx;sessionid=xxx;sessionid_ss=xxx;sid_ucp_v1=xxx;ssid_ucp_v1=xxx;passport_assist_user=xxx;ttwid=xxx;
IS_OVERSEA=False # True/False,是否是海外服务器部署
IS_LAGRANGE=False # True/False,是否是拉格朗日(https://github.com/KonataDev/Lagrange.Core)
resolver_proxy = "http://127.0.0.1:7890" # 代理
VIDEO_DURATION_MAXIMUM = 480 # 哔哩哔哩限制的最大视频时长(默认8分钟),单位:秒
DOUYIN_CK='odin_tt=4365b7fc5681ac8702a0adfcbe9733bcd7c5c1ed61993a02640bbe4f2cc56f419ccbeda80d3560686d9a64f9ea5d587f;passport_fe_beating_status=true;sid_guard=1796d8581585653a050884d09e26008b%7C1715415372%7C5183998%7CWed%2C+10-Jul-2024+08%3A16%3A10+GMT;uid_tt=7199d412b0d7cee4430070a8cfc8320f;uid_tt_ss=7199d412b0d7cee4430070a8cfc8320f;sid_tt=1796d8581585653a050884d09e26008b;sessionid=1796d8581585653a050884d09e26008b;sessionid_ss=1796d8581585653a050884d09e26008b;sid_ucp_v1=1.0.0-KGQ3NDhkNzRlYjU5YWI0YmQ4YmExZmVkOGFhOGRiNDg1NDNkMzkxNTAKGwi_5uDl4oy6AxDM0vyxBhjvMSAMOAJA8QdIBBoCaGwiIDE3OTZkODU4MTU4NTY1M2EwNTA4ODRkMDllMjYwMDhi;ssid_ucp_v1=1.0.0-KGQ3NDhkNzRlYjU5YWI0YmQ4YmExZmVkOGFhOGRiNDg1NDNkMzkxNTAKGwi_5uDl4oy6AxDM0vyxBhjvMSAMOAJA8QdIBBoCaGwiIDE3OTZkODU4MTU4NTY1M2EwNTA4ODRkMDllMjYwMDhi;passport_assist_user=CkGuJEZPpgFQWNMCABb2GaOC-Ti8e0UuW4QgdpP--GoBlq1KrfwWwP_QaJejxEtNd77iqpXX0czej9pjLoexN_qcyxpKCjxwi9cIHJwQc4xpNFKIDoabUFW0O9dmh5yta1sCQR0WbZTaov2ldfGGIoELE5m002kdncRlUiDU_hh0mqAQtIDRDRiJr9ZUIAEiAQNAdRnC;ttwid=1%7C1oSBGQv3H2RaIGU0T5qWdSIbXmqLC8DQ2l_8cXcvoaE%7C1715415325%7C790ecd495fe12b8c3056a6562f14888ca137d14aa01b2534e3f205fb6c8d82a1;' # douyin's cookie, 格式:odin_tt=xxx;passport_fe_beating_status=xxx;sid_guard=xxx;uid_tt=xxx;uid_tt_ss=xxx;sid_tt=xxx;sessionid=xxx;sessionid_ss=xxx;sid_ucp_v1=xxx;ssid_ucp_v1=xxx;passport_assist_user=xxx;ttwid=xxx;
IS_OVERSEA=False # 是否是海外服务器部署
IS_LAGRANGE=False # 是否是拉格朗日(https://github.com/KonataDev/Lagrange.Core)
RESOLVER_PROXY = "http://127.0.0.1:7890" # 代理
R_GLOBAL_NICKNAME = "R插件极速版" # 解析前缀名
```

## 🤺 交流群
Expand Down
85 changes: 44 additions & 41 deletions nonebot-plugin-resolver/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import asyncio
import json
import os.path
from typing import cast

from nonebot import on_regex, get_driver, logger
Expand All @@ -11,12 +12,12 @@

from .common_utils import *
from .config import Config
from .bili23_utils import getDownloadUrl, downloadBFile, mergeFileToMp4, get_dynamic, extra_bili_info
from .tiktok_utills import get_id_video, generate_x_bogus_url
from .bili23_utils import get_download_url, download_b_file, merge_file_to_mp4, get_dynamic, extra_bili_info
from .tiktok_utills import generate_x_bogus_url
from .acfun_utils import parse_url, download_m3u8_videos, parse_m3u8, merge_ac_file_to_mp4
from .ytdlp_utils import get_video_title, download_ytb_video
from .freyrjs import parse_freyr_log, execute_freyr_command
from .constants import URL_TYPE_CODE_DICT, BILI_VIDEO_INFO, DOUYIN_VIDEO, TIKTOK_VIDEO, GENERAL_REQ_LINK, XHS_REQ_LINK
from .constants import URL_TYPE_CODE_DICT, BILI_VIDEO_INFO, DOUYIN_VIDEO, GENERAL_REQ_LINK, XHS_REQ_LINK

__plugin_meta__ = PluginMetadata(
name="链接分享解析器",
Expand Down Expand Up @@ -143,23 +144,23 @@ async def bilibili(event: Event) -> None:
Message(MessageSegment.image(video_cover)) + Message(
f"\n{GLOBAL_NICKNAME}识别:B站,{video_title}\n{extra_bili_info(video_info)}\n简介:{video_desc}\n---------\n⚠️ 当前视频时长 {video_duration // 60} 分钟,超过管理员设置的最长时间 {VIDEO_DURATION_MAXIMUM // 60} 分钟!"))
# 获取下载链接
video_url, audio_url = getDownloadUrl(url)
video_url, audio_url = get_download_url(url)
# 下载视频和音频
path = os.getcwd() + "/" + video_id
await asyncio.gather(
downloadBFile(video_url, f"{path}-video.m4s", logger.info),
downloadBFile(audio_url, f"{path}-audio.m4s", logger.info))
mergeFileToMp4(f"{video_id}-video.m4s", f"{video_id}-audio.m4s", f"{path}-res.mp4")
try:
await asyncio.gather(
download_b_file(video_url, f"{path}-video.m4s", logger.info),
download_b_file(audio_url, f"{path}-audio.m4s", logger.info))
merge_file_to_mp4(f"{video_id}-video.m4s", f"{video_id}-audio.m4s", f"{path}-res.mp4")
finally:
remove_res = remove_files([f"{video_id}-video.m4s", f"{video_id}-audio.m4s"])
logger.info(remove_res)
# logger.info(os.getcwd())
# 发送出去
# logger.info(path)
# await bili23.send(Message(MessageSegment.video(f"{path}-res.mp4")))
await auto_video_send(event, f"{path}-res.mp4", IS_LAGRANGE)
# logger.info(f'{path}-res.mp4')
# 清理文件
if os.path.exists(f"{path}-res.mp4.jpg"):
os.unlink(f"{path}-res.mp4.jpg")


@douyin.handle()
async def dy(bot: Bot, event: Event) -> None:
Expand Down Expand Up @@ -301,9 +302,6 @@ async def ac(event: Event) -> None:
merge_ac_file_to_mp4(ts_names, output_file_name)
# await acfun.send(Message(MessageSegment.video(f"{os.getcwd()}/{output_file_name}")))
await auto_video_send(event, f"{os.getcwd()}/{output_file_name}", IS_LAGRANGE)
os.unlink(output_file_name)
os.unlink(output_file_name + ".jpg")


@twit.handle()
async def twitter(bot: Bot, event: Event):
Expand Down Expand Up @@ -351,7 +349,7 @@ async def twitter(bot: Bot, event: Event):


@xhs.handle()
async def redbook(bot: Bot, event: Event):
async def xiaohongshu(bot: Bot, event: Event):
"""
小红书解析
:param event:
Expand Down Expand Up @@ -396,7 +394,6 @@ async def redbook(bot: Bot, event: Event):
path = await download_video(video_url)
# await xhs.send(Message(MessageSegment.video(path)))
await auto_video_send(event, path, IS_LAGRANGE)
os.unlink(path)
return
# 发送图片
links = [MessageSegment.node_custom(user_id=int(bot.self_id), nickname=GLOBAL_NICKNAME,
Expand Down Expand Up @@ -474,18 +471,18 @@ def auto_determine_send_type(user_id: int, task: str):
content=Message(MessageSegment.video(task)))


async def upload_data_file(bot: Bot, event: Event, data: str):
async def upload_data_file(bot: Bot, event: Event, data_path: str):
"""
上传群文件
:param bot:
:param event:
:param data:
:param data_path:
:return:
"""
if isinstance(event, GroupMessageEvent):
await upload_group_file(bot, group_id=event.group_id, file=data)
await upload_group_file(bot, group_id=event.group_id, file=data_path)
elif isinstance(event, PrivateMessageEvent):
await upload_private_file(bot, user_id=event.user_id, file=data)
await upload_private_file(bot, user_id=event.user_id, file=data_path)


async def upload_group_file(bot: Bot, group_id: int, file: str):
Expand Down Expand Up @@ -514,29 +511,35 @@ async def upload_private_file(bot: Bot, user_id: int, file: str):
"[ERROR] 文件上传失败\r\n[原因] 上传超时(一般来说还在传,建议等待五分钟)")))


async def auto_video_send(event: Event, data: str, is_lagrange: bool = False):
async def auto_video_send(event: Event, data_path: str, is_lagrange: bool = False):
"""
拉格朗日自动转换成上传
:param event:
:param data:
:param data_path:
:param is_lagrange:
:return:
"""
bot: Bot = cast(Bot, current_bot.get())

# 如果data以"http"开头,先下载视频
if data.startswith("http"):
data = await download_video(data)

# 如果是Lagrange,则上传数据文件
if is_lagrange:
await upload_data_file(bot=bot, event=event, data=data)
else:
# 根据事件类型发送不同的消息
if isinstance(event, GroupMessageEvent):
await bot.send_group_msg(group_id=event.group_id, message=Message(MessageSegment.video(f'file://{data}')))
elif isinstance(event, PrivateMessageEvent):
await bot.send_private_msg(user_id=event.user_id, message=Message(MessageSegment.video(f'file://{data}')))

# 删除临时文件
os.unlink(data)
try:
bot: Bot = cast(Bot, current_bot.get())

# 如果data以"http"开头,先下载视频
if data_path.startswith("http"):
data_path = await download_video(data_path)

# 如果是Lagrange,则上传数据文件
if is_lagrange:
await upload_data_file(bot=bot, event=event, data=data_path)
else:
# 根据事件类型发送不同的消息
if isinstance(event, GroupMessageEvent):
await bot.send_group_msg(group_id=event.group_id, message=Message(MessageSegment.video(f'file://{data_path}')))
elif isinstance(event, PrivateMessageEvent):
await bot.send_private_msg(user_id=event.user_id, message=Message(MessageSegment.video(f'file://{data_path}')))
except Exception as e:
logger.error(f"下载出现错误,具体为\n{e}")
finally:
# 删除临时文件
if os.path.exists(data_path):
os.unlink(data_path)
if os.path.exists(data_path + '.jpg'):
os.unlink(data_path + '.jpg')
38 changes: 17 additions & 21 deletions nonebot-plugin-resolver/bili23_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
}


def getDownloadUrl(url: str):
def get_download_url(url: str):
"""
爬取下载链接
:param url:
Expand All @@ -29,44 +29,40 @@ def getDownloadUrl(url: str):
return videoUrl, audioUrl


async def downloadBFile(url, fullFileName, progressCallback):
async def download_b_file(url, full_file_name, progress_callback):
"""
下载视频文件和音频文件
:param url:
:param fullFileName:
:param progressCallback:
:param full_file_name:
:param progress_callback:
:return:
"""
async with httpx.AsyncClient() as client:
async with client.stream("GET", url, headers=header) as resp:
currentLen = 0
totalLen = int(resp.headers['content-length'])
print(totalLen)
with open(fullFileName, "wb") as f:
current_len = 0
total_len = int(resp.headers['content-length'])
print(total_len)
with open(full_file_name, "wb") as f:
async for chunk in resp.aiter_bytes():
currentLen += len(chunk)
current_len += len(chunk)
f.write(chunk)
progressCallback(currentLen / totalLen)
progress_callback(current_len / total_len)


def mergeFileToMp4(vFullFileName: str, aFullFileName: str, outputFileName: str, shouldDelete=True):
def merge_file_to_mp4(v_full_file_name: str, a_full_file_name: str, output_file_name: str):
"""
合并视频文件和音频文件
:param vFullFileName:
:param aFullFileName:
:param outputFileName:
:param shouldDelete:
:param v_full_file_name:
:param a_full_file_name:
:param output_file_name:
:return:
"""
# 调用ffmpeg
subprocess.call(f'ffmpeg -y -i "{vFullFileName}" -i "{aFullFileName}" -c copy "{outputFileName}"', shell=True,
subprocess.call(f'ffmpeg -y -i "{v_full_file_name}" -i "{a_full_file_name}" -c copy "{output_file_name}"',
shell=True,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
# 删除临时文件
if shouldDelete:
os.unlink(vFullFileName)
os.unlink(aFullFileName)


def get_dynamic(dynamic_id: str):
Expand Down Expand Up @@ -117,4 +113,4 @@ def extra_bili_info(video_info):
formatted_value = value
video_info_result += f"{key}: {formatted_value} | "

return video_info_result
return video_info_result
33 changes: 31 additions & 2 deletions nonebot-plugin-resolver/common_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
import re
import time

from typing import List, Dict

header = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (HTML, like Gecko) Chrome/101.0.4951.64 Safari/537.36'
}


async def download_video(url, proxy: str = None):
"""
异步下载(httpx)视频,并支持通过代理下载。
Expand Down Expand Up @@ -42,7 +45,7 @@ async def download_video(url, proxy: str = None):
return None


async def download_img(url: str, path: str = '', proxy: str = None, session = None) -> str:
async def download_img(url: str, path: str = '', proxy: str = None, session=None) -> str:
"""
异步下载(aiohttp)网络图片,并支持通过代理下载。
如果未指定path,则图片将保存在当前工作目录并以图片的文件名命名。
Expand Down Expand Up @@ -72,10 +75,36 @@ async def download_img(url: str, path: str = '', proxy: str = None, session = No
f.write(data)
return path


def delete_boring_characters(sentence):
"""
去除标题的特殊字符
:param sentence:
:return:
"""
return re.sub('[0-9’!"∀〃#$%&\'()*+,-./:;<=>?@,。?★、…【】《》?“”‘’![\\]^_`{|}~~\s]+', "", sentence)
return re.sub('[0-9’!"∀〃#$%&\'()*+,-./:;<=>?@,。?★、…【】《》?“”‘’![\\]^_`{|}~~\s]+', "", sentence)


def remove_files(file_paths: List[str]) -> Dict[str, str]:
"""
根据路径删除文件
Parameters:
*file_paths (str): 要删除的一个或多个文件路径
Returns:
dict: 一个以文件路径为键、删除状态为值的字典
"""
results = {}

for file_path in file_paths:
if os.path.exists(file_path):
try:
os.remove(file_path)
results[file_path] = 'remove'
except Exception as e:
results[file_path] = f'error: {e}'
else:
results[file_path] = 'don\'t exist'

return results
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "nonebot-plugin-resolver"
version = "1.1.9"
version = "1.1.10"
description = "NoneBot2链接分享解析器插件。解析视频、图片链接/小程序插件,tiktok、bilibili、twitter等实时发送!"
authors = ["zhiyu1998 <[email protected]>"]
readme = "README.md"
Expand Down

0 comments on commit 6908e2d

Please sign in to comment.