Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions src/nonebot_plugin_parser/download/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,13 +224,14 @@ async def _get_m3u8_slices(self, m3u8_url: str):
return slices


DOWNLOADER: StreamDownloader = StreamDownloader()
downloader: StreamDownloader = StreamDownloader()
"""全局下载器实例,提供下载功能"""
yt_dlp_downloader = None
"""yt-dlp 下载器实例,提供下载视频功能,若 yt-dlp 未安装则为 None"""

try:
import yt_dlp as yt_dlp
from ..utils import is_module_available

if is_module_available("yt_dlp"):
from .ytdlp import YtdlpDownloader

YTDLP_DOWNLOADER = YtdlpDownloader()
except ImportError:
YTDLP_DOWNLOADER = None
yt_dlp_downloader = YtdlpDownloader()
6 changes: 3 additions & 3 deletions src/nonebot_plugin_parser/matchers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@ async def _(message: Message = CommandArg()):
await UniMessage(UniHelper.file_seg(audio_path)).send()


from ..download import YTDLP_DOWNLOADER
from ..download import yt_dlp_downloader

if YTDLP_DOWNLOADER is not None:
if yt_dlp_downloader is not None:
from ..parsers import YouTubeParser

@on_command("ym", priority=3, block=True).handle()
Expand All @@ -128,7 +128,7 @@ async def _(message: Message = CommandArg()):

url = matched.group(0)

audio_path = await YTDLP_DOWNLOADER.download_audio(url)
audio_path = await yt_dlp_downloader.download_audio(url)
await UniMessage(UniHelper.record_seg(audio_path)).send()

if pconfig.need_upload:
Expand Down
4 changes: 2 additions & 2 deletions src/nonebot_plugin_parser/parsers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
from .twitter import TwitterParser as TwitterParser
from .bilibili import BilibiliParser as BilibiliParser
from .kuaishou import KuaiShouParser as KuaiShouParser
from ..download import YTDLP_DOWNLOADER
from ..download import yt_dlp_downloader as yt_dlp_downloader
from .xiaohongshu import XiaoHongShuParser as XiaoHongShuParser

if YTDLP_DOWNLOADER is not None:
if yt_dlp_downloader is not None:
from .tiktok import TikTokParser as TikTokParser
from .youtube import YouTubeParser as YouTubeParser

Expand Down
16 changes: 8 additions & 8 deletions src/nonebot_plugin_parser/parsers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from .data import Platform, ParseResult, ImageContent, ParseResultKwargs
from .task import PathTask
from ..config import pconfig as pconfig
from ..download import DOWNLOADER
from ..download import downloader
from ..constants import IOS_HEADER, COMMON_HEADER, ANDROID_HEADER, COMMON_TIMEOUT
from ..constants import DOWNLOAD_TIMEOUT as DOWNLOAD_TIMEOUT
from ..constants import PlatformEnum as PlatformEnum
Expand Down Expand Up @@ -170,7 +170,7 @@ def create_author(
author = Author(name=name, description=description)

if avatar_url:
author.avatar = PathTask(DOWNLOADER.download_img(avatar_url, ext_headers=self.headers))
author.avatar = PathTask(downloader.download_img(avatar_url, ext_headers=self.headers))

return author

Expand All @@ -185,12 +185,12 @@ def create_video(
from ..utils import extract_video_cover

if isinstance(url_or_task, str):
path_task = DOWNLOADER.download_video(url_or_task, ext_headers=self.headers)
path_task = downloader.download_video(url_or_task, ext_headers=self.headers)
elif isinstance(url_or_task, Task):
path_task = url_or_task

if cover_url:
cover_task = DOWNLOADER.download_img(cover_url, ext_headers=self.headers)
cover_task = downloader.download_img(cover_url, ext_headers=self.headers)
else:
# 如果没有封面 URL,尝试从视频中提取封面
async def extract_cover():
Expand All @@ -212,7 +212,7 @@ def create_images(
"""创建图片内容列表"""
contents: list[ImageContent] = []
for url in image_urls:
task = DOWNLOADER.download_img(url, ext_headers=self.headers)
task = downloader.download_img(url, ext_headers=self.headers)
contents.append(ImageContent(PathTask(task)))
return contents

Expand All @@ -223,7 +223,7 @@ def create_image(
):
"""创建单个图片内容"""
if isinstance(url_or_task, str):
path_task = DOWNLOADER.download_img(url_or_task, ext_headers=self.headers)
path_task = downloader.download_img(url_or_task, ext_headers=self.headers)
elif isinstance(url_or_task, Task):
path_task = url_or_task

Expand All @@ -238,12 +238,12 @@ def create_audio(
from .data import AudioContent

if isinstance(url_or_task, str):
path_task = DOWNLOADER.download_audio(url_or_task, ext_headers=self.headers)
path_task = downloader.download_audio(url_or_task, ext_headers=self.headers)
elif isinstance(url_or_task, Task):
path_task = url_or_task

return AudioContent(PathTask(path_task), duration)

@property
def downloader(self):
return DOWNLOADER
return downloader
6 changes: 3 additions & 3 deletions src/nonebot_plugin_parser/parsers/tiktok.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from .base import BaseParser, PlatformEnum, handle
from .data import Author, Platform
from ..download import YTDLP_DOWNLOADER
from ..download import yt_dlp_downloader


class TikTokParser(BaseParser):
Expand All @@ -18,10 +18,10 @@ async def _parse(self, searched: re.Match[str]):
url = await self.get_redirect_url(url)

# 获取视频信息
video_info = await YTDLP_DOWNLOADER.extract_video_info(url)
video_info = await yt_dlp_downloader.extract_video_info(url)

# 下载封面和视频
video = YTDLP_DOWNLOADER.download_video(url)
video = yt_dlp_downloader.download_video(url)
video_content = self.create_video(
video,
video_info.thumbnail,
Expand Down
6 changes: 3 additions & 3 deletions src/nonebot_plugin_parser/parsers/youtube/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from ..base import Platform, BaseParser, PlatformEnum, handle, pconfig
from ..cookie import save_cookies_with_netscape
from ...download import YTDLP_DOWNLOADER
from ...download import yt_dlp_downloader


class YouTubeParser(BaseParser):
Expand All @@ -28,7 +28,7 @@ async def _parse_video(self, searched: re.Match[str]):
return await self.parse_video(url)

async def parse_video(self, url: str):
video_info = await YTDLP_DOWNLOADER.extract_video_info(url, self.cookies_file)
video_info = await yt_dlp_downloader.extract_video_info(url, self.cookies_file)
author = await self._fetch_author_info(video_info.channel_id)

result = self.result(
Expand All @@ -38,7 +38,7 @@ async def parse_video(self, url: str):
)

if video_info.duration <= pconfig.duration_maximum:
video = YTDLP_DOWNLOADER.download_video(url, self.cookies_file)
video = yt_dlp_downloader.download_video(url, self.cookies_file)
result.video = self.create_video(
video,
video_info.thumbnail,
Expand Down
11 changes: 2 additions & 9 deletions src/nonebot_plugin_parser/renders/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,12 @@
from .common import CommonRenderer
from .default import DefaultRenderer

RENDERER: type[BaseRenderer] | None = None

def is_module_available(module_name: str) -> bool:
"""检查模块是否可用"""
import importlib.util

return importlib.util.find_spec(module_name) is not None


from ..utils import is_module_available
from ..config import pconfig
from ..constants import RenderType

RENDERER: type[BaseRenderer] | None = None

match pconfig.render_type:
case RenderType.common:
RENDERER = CommonRenderer
Expand Down
7 changes: 7 additions & 0 deletions src/nonebot_plugin_parser/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,10 @@ def write_json_to_data(data: dict[str, Any] | str, file_name: str):
with open(path, "w") as f:
json.dump(data, f, ensure_ascii=False, indent=4)
logger.success(f"数据写入 {path} 成功")


def is_module_available(module_name: str) -> bool:
"""检查模块是否可用"""
import importlib.util

return importlib.util.find_spec(module_name) is not None
4 changes: 2 additions & 2 deletions tests/parsers/test_bilibili_need_ck.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ async def test_video():
@pytest.mark.asyncio
async def test_max_size_video():
from nonebot_plugin_parser.parsers import BilibiliParser
from nonebot_plugin_parser.download import DOWNLOADER
from nonebot_plugin_parser.download import downloader
from nonebot_plugin_parser.exception import IgnoreException

parser = BilibiliParser()
Expand All @@ -61,7 +61,7 @@ async def test_max_size_video():

assert audio_url is not None
try:
await DOWNLOADER.download_audio(audio_url, ext_headers=parser.headers)
await downloader.download_audio(audio_url, ext_headers=parser.headers)
except IgnoreException:
pass

Expand Down
12 changes: 6 additions & 6 deletions tests/parsers/test_ytdlp.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,27 @@

@pytest.mark.asyncio
async def test_extract_video_info():
from nonebot_plugin_parser.download import YTDLP_DOWNLOADER
from nonebot_plugin_parser.download import yt_dlp_downloader

await YTDLP_DOWNLOADER.extract_video_info(TIKTOK_URL)
await yt_dlp_downloader.extract_video_info(TIKTOK_URL)


@pytest.mark.asyncio
async def test_download_video():
from nonebot_plugin_parser.download import YTDLP_DOWNLOADER
from nonebot_plugin_parser.download import yt_dlp_downloader

video_path = await YTDLP_DOWNLOADER.download_video(TIKTOK_URL)
video_path = await yt_dlp_downloader.download_video(TIKTOK_URL)

assert video_path.exists()


@pytest.mark.asyncio
async def test_download_audio():
from nonebot_plugin_parser.download import YTDLP_DOWNLOADER
from nonebot_plugin_parser.download import yt_dlp_downloader

url = "https://www.tiktok.com/@fdznews/video/7575810064078884116?is_from_webapp=1&sender_device=pc"

audio_path = await YTDLP_DOWNLOADER.download_audio(url)
audio_path = await yt_dlp_downloader.download_audio(url)

assert audio_path.exists()

Expand Down
Loading