Skip to content

Commit

Permalink
chore: 移除未使用的导入,优化代码结构
Browse files Browse the repository at this point in the history
  • Loading branch information
fllesser committed Jan 16, 2025
1 parent 6b8467e commit 8c87147
Show file tree
Hide file tree
Showing 19 changed files with 38 additions and 136 deletions.
10 changes: 8 additions & 2 deletions nonebot_plugin_resolver2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@

from nonebot.plugin import PluginMetadata
from .matchers import resolvers
from .config import *
from .cookie import *
from .config import (
Config,
rconfig,
scheduler,
plugin_cache_dir,
ytb_cookies_file,
)
from .cookie import save_cookies_to_netscape


__plugin_meta__ = PluginMetadata(
Expand Down
5 changes: 2 additions & 3 deletions nonebot_plugin_resolver2/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@
)
from pydantic import BaseModel
from pathlib import Path
from typing import List, Literal, Optional
from typing import List, Optional

from .constant import MatcherNames

require("nonebot_plugin_localstore")
require("nonebot_plugin_apscheduler")
from nonebot_plugin_apscheduler import scheduler
import nonebot_plugin_localstore as store
import nonebot_plugin_localstore as store # noqa: E402

class Config(BaseModel):
r_xhs_ck: str = ''
Expand Down
8 changes: 4 additions & 4 deletions nonebot_plugin_resolver2/cookie.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import http.cookiejar
from nonebot import logger
from http import cookiejar
from pathlib import Path
from typing import Dict
from nonebot.log import logger

def save_cookies_to_netscape(cookies_str: str, file: Path, domain: str):
# 创建 MozillaCookieJar 对象
cj = http.cookiejar.MozillaCookieJar(file)
cj = cookiejar.MozillaCookieJar(file)

# 从字符串创建 cookies 并添加到 MozillaCookieJar 对象
for cookie in cookies_str.split(';'):
name, value = cookie.strip().split('=', 1)
cj.set_cookie(http.cookiejar.Cookie(
cj.set_cookie(cookiejar.Cookie(
version=0, name=name, value=value, port=None, port_specified=False,
domain="." + domain, domain_specified=True, domain_initial_dot=False,
path="/", path_specified=True, secure=True,
Expand Down
2 changes: 0 additions & 2 deletions nonebot_plugin_resolver2/download/ytdlp.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
import yt_dlp

from pathlib import Path
from nonebot import get_bot, get_driver
from nonebot.log import logger

from .common import delete_boring_characters
from ..config import (
Expand Down
5 changes: 3 additions & 2 deletions nonebot_plugin_resolver2/matchers/acfun.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import re
import json
import httpx
import asyncio
import aiofiles
import subprocess
import httpx
import json


from nonebot import on_keyword
from nonebot.rule import Rule
Expand Down
2 changes: 0 additions & 2 deletions nonebot_plugin_resolver2/matchers/douyin.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import re
import httpx
import asyncio

from nonebot import on_keyword
from nonebot.log import logger
from nonebot.rule import Rule
from nonebot.adapters.onebot.v11 import (
Message,
Event,
Bot,
MessageSegment
Expand Down
1 change: 0 additions & 1 deletion nonebot_plugin_resolver2/matchers/filter.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import json

from nonebot.log import logger
from nonebot.rule import to_me
from nonebot.plugin import on_command
from nonebot.permission import SUPERUSER
Expand Down
8 changes: 1 addition & 7 deletions nonebot_plugin_resolver2/matchers/kugou.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
import os
import re
import json
import httpx
import asyncio

from nonebot.log import logger
from nonebot.typing import T_State
from nonebot.plugin import on_message
from nonebot.plugin.on import on_message
from nonebot.adapters.onebot.v11 import (
Message,
MessageEvent,
Bot,
MessageSegment
)
from .utils import get_file_seg
from .filter import is_not_in_disable_group
from .preprocess import (
r_keywords,
R_KEYWORD_KEY,
R_EXTRACT_KEY
)
from ..download.common import download_audio
Expand Down
6 changes: 2 additions & 4 deletions nonebot_plugin_resolver2/matchers/ncm.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import re
import httpx
import asyncio

from nonebot.plugin import on_message
from nonebot.typing import T_State
from nonebot.adapters.onebot.v11 import (
Message,
Bot,
MessageSegment
)
Expand All @@ -19,7 +17,7 @@
)
from ..constant import COMMON_HEADER
from ..download.common import download_audio
from ..config import *
from ..config import NICKNAME

# NCM获取歌曲信息链接
NETEASE_API_CN = 'https://www.markingchen.ink'
Expand Down Expand Up @@ -63,7 +61,7 @@ async def _(bot: Bot, state: T_State):
try:
audio_path = await download_audio(ncm_music_url)
except Exception as e:
await ncm.finish(f'音频下载失败')
await ncm.finish(f'音频下载失败 {e}')
# 发送语音
await ncm.send(MessageSegment.record(audio_path))
# 发送群文件
Expand Down
3 changes: 1 addition & 2 deletions nonebot_plugin_resolver2/matchers/preprocess.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import json

from typing import Literal
from nonebot.log import logger
from nonebot.rule import Rule
from nonebot.message import event_preprocessor
from nonebot.typing import T_State
from nonebot.rule import Rule
from nonebot.adapters.onebot.v11 import MessageEvent


Expand Down
10 changes: 4 additions & 6 deletions nonebot_plugin_resolver2/matchers/tiktok.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import re, httpx, asyncio
import re
import httpx

from nonebot import on_keyword
from nonebot.rule import Rule
from nonebot.adapters.onebot.v11 import (
MessageEvent,
Message
)
from nonebot.adapters.onebot.v11 import MessageEvent
from nonebot.exception import ActionFailed

from .filter import is_not_in_disable_group
from .utils import get_video_seg
from ..download.ytdlp import get_video_info, ytdlp_download_video
from ..config import *
from ..config import PROXY, NICKNAME


tiktok = on_keyword(
Expand Down
3 changes: 0 additions & 3 deletions nonebot_plugin_resolver2/matchers/twitter.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import re
import httpx
import asyncio

from nonebot.log import logger
from nonebot.rule import Rule
from nonebot.plugin.on import on_keyword
from nonebot.adapters.onebot.v11 import (
Message,
MessageEvent,
Bot,
MessageSegment
Expand Down
1 change: 0 additions & 1 deletion nonebot_plugin_resolver2/matchers/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from pathlib import Path
from nonebot.log import logger
from nonebot.adapters.onebot.v11 import (
Message,
MessageSegment
Expand Down
9 changes: 3 additions & 6 deletions nonebot_plugin_resolver2/matchers/weibo.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
import os
import re
import json
import math
import httpx
import asyncio

from nonebot import on_keyword
from nonebot.rule import Rule
from nonebot.log import logger
from nonebot.plugin.on import on_keyword
from nonebot.adapters.onebot.v11 import (
Message,
MessageEvent,
Bot,
MessageSegment
)

from .filter import is_not_in_disable_group
from .utils import construct_nodes, get_video_seg
from ..constant import COMMON_HEADER

from ..download.common import download_img, download_video
from ..parsers.weibo import WeiBo
from ..constant import COMMON_HEADER
from ..config import NICKNAME

# WEIBO_SINGLE_INFO
Expand Down
7 changes: 2 additions & 5 deletions nonebot_plugin_resolver2/matchers/xiaohongshu.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
from nonebot.plugin.on import on_message
from nonebot.adapters.onebot.v11 import (
Bot,
Message,
MessageEvent,
MessageSegment
)
from urllib.parse import parse_qs, urlparse
Expand All @@ -18,13 +16,12 @@
from .utils import get_video_seg, construct_nodes
from .preprocess import (
r_keywords,
R_KEYWORD_KEY,
R_EXTRACT_KEY
)

from ..constant import COMMON_HEADER
from ..download.common import download_video, download_img
from ..config import *
from ..download.common import download_img
from ..config import rconfig, NICKNAME

# 小红书下载链接
XHS_REQ_LINK = "https://www.xiaohongshu.com/explore/"
Expand Down
83 changes: 4 additions & 79 deletions nonebot_plugin_resolver2/matchers/ytb.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
import re
import asyncio

from nonebot.log import logger
from nonebot.typing import T_State
from nonebot.params import Arg
from nonebot.rule import Rule
from nonebot.exception import ActionFailed
from nonebot.permission import SUPERUSER
from nonebot.plugin.on import on_keyword, on_command
from nonebot.plugin.on import on_keyword
from nonebot.adapters.onebot.v11 import (
MessageEvent,
Bot,
Message,
MessageSegment,
Bot
MessageEvent,
MessageSegment
)

from .filter import is_not_in_disable_group
Expand All @@ -25,18 +22,13 @@
from ..config import (
NICKNAME,
ytb_cookies_file,
scheduler
)

ytb = on_keyword(
keywords = {"youtube.com", "youtu.be"},
rule = Rule(is_not_in_disable_group)
)

# update_yt = on_command(
# cmd="update yt",
# permission=SUPERUSER
# )

@ytb.handle()
async def _(event: MessageEvent, state: T_State):
Expand Down Expand Up @@ -70,70 +62,3 @@ async def _(bot: Bot, event: MessageEvent, state: T_State, type: Message = Arg()
if not isinstance(e, ActionFailed):
await ytb.send(f"下载失败 | {e}")

# @update_yt.handle()
# async def _(event: MessageEvent):
# get_video_info, ytdlp_download_video, ytdlp_download_audio = await update_ytdlp()
# version = await get_yt_dlp_version()
# await update_yt.finish(f"Successfully updated {version}")

# @scheduler.scheduled_job(
# "cron",
# hour=3,
# minute=0,
# )
# async def _():
# get_video_info, ytdlp_download_video, ytdlp_download_audio = await update_ytdlp()
# version = await get_yt_dlp_version()
# success_info = f"Successfully updated {version}"
# try:
# bot = get_bot()
# if superuser_id := int(next(iter(get_driver().config.superusers), None)):
# await bot.send_private_msg(user_id = superuser_id, message = success_info)
# except Exception:
# pass


# async def update_ytdlp() -> str:
# import subprocess
# import importlib
# import sys
# process = await asyncio.create_subprocess_exec(
# 'pip', 'install', '--upgrade', 'yt-dlp',
# stdout=subprocess.PIPE,
# stderr=subprocess.PIPE
# )
# stdout, stderr = await process.communicate()
# if process.returncode == 0:
# if 'yt_dlp' in sys.modules:
# del sys.modules['yt_dlp']
# logger.warning('delete cache of yt_dlp')
# import yt_dlp
# if 'nonebot_plugin_resolver2.download.ytdlp' in sys.modules:
# del sys.modules['nonebot_plugin_resolver2.download.ytdlp']
# logger.warning('delete cache of nonebot_plugin_resolver2.download.ytdlp')
# from ..download import ytdlp
# importlib.reload(yt_dlp)
# importlib.reload(ytdlp)
# from ..download.ytdlp import (
# get_video_info,
# ytdlp_download_audio,
# ytdlp_download_video
# )
# return get_video_info, ytdlp_download_audio, ytdlp_download_video
# else:
# logger.error(f"Failed to update yt-dlp: {stderr.decode()}")

# async def get_yt_dlp_version():
# import subprocess
# process = await asyncio.create_subprocess_exec(
# 'yt-dlp', '--version',
# stdout=subprocess.PIPE,
# stderr=subprocess.PIPE
# )
# stdout, stderr = await process.communicate()
# if process.returncode == 0:
# version = stdout.decode().strip()
# return f"yt-dlp {version}"
# else:
# error_message = stderr.decode().strip()
# return f"Failed to get yt-dlp version: {error_message}"
3 changes: 1 addition & 2 deletions nonebot_plugin_resolver2/parsers/base.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import dataclasses
import fake_useragent
from abc import ABC, abstractmethod
from enum import Enum
from typing import Dict, List

import fake_useragent

@dataclasses.dataclass
class VideoAuthor:
Expand Down
5 changes: 2 additions & 3 deletions nonebot_plugin_resolver2/parsers/douyin.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import re
import json
import httpx
import asyncio

from tenacity import (
retry,
stop_after_attempt,
wait_fixed,
AsyncRetrying
stop_after_attempt
)

from .base import BaseParser, VideoAuthor, VideoInfo
Expand Down
Loading

0 comments on commit 8c87147

Please sign in to comment.