Skip to content

Commit

Permalink
完善
Browse files Browse the repository at this point in the history
  • Loading branch information
lgc2333 committed Oct 28, 2023
1 parent 0e16da1 commit e882af1
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 20 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,10 @@ Telegram:[@lgc2333](https://t.me/lgc2333)

## 📝 更新日志

### 0.4.1

- 支持了 `163.tv` 短链(Thanks to [@XieXiLin2](https://github.com/XieXiLin2)

### 0.4.0

- 项目部分重构
Expand Down
2 changes: 1 addition & 1 deletion nonebot_plugin_multincm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from . import __main__ as __main__ # noqa: E402
from .config import ConfigModel, config # noqa: E402

__version__ = "0.4.0"
__version__ = "0.4.1"
__plugin_meta__ = PluginMetadata(
name="MultiNCM",
description="网易云多选点歌",
Expand Down
59 changes: 41 additions & 18 deletions nonebot_plugin_multincm/__main__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import asyncio
import httpx
import random
import re
from pathlib import Path
Expand All @@ -15,6 +14,7 @@
cast,
)

from httpx import AsyncClient
from nonebot import logger, on_command, on_regex
from nonebot.adapters import Event
from nonebot.adapters.onebot.v11 import (
Expand All @@ -27,6 +27,7 @@
NetworkError,
PrivateMessageEvent,
)
from nonebot.consts import REGEX_MATCHED
from nonebot.matcher import Matcher, current_bot, current_event, current_matcher
from nonebot.params import ArgPlainText, CommandArg
from nonebot.rule import Rule
Expand All @@ -52,7 +53,6 @@
KEY_SEND_LINK = "send_link"
KEY_UPLOAD_FILE = "upload_file"
KEY_IS_AUTO_RESOLVE = "is_auto_resolve"
KEY_IS_SHORT_LINK = "is_short_link"

EXIT_COMMAND = ("退出", "tc", "取消", "qx", "quit", "q", "exit", "e", "cancel", "c", "0")
PREVIOUS_COMMAND = ("上一页", "syy", "previous", "p")
Expand All @@ -65,9 +65,9 @@
SONG_URL_REGEX = (
rf"music\.163\.com/(.*?)(?P<type>{_link_types_reg})(/?\?id=|/)(?P<id>[0-9]+)&?"
)
SONG_SHORT_URL_REGEX = (
"163cn\.tv/.*"
)
SHORT_URL_BASE = "https://163cn.tv"
SHORT_URL_REGEX = r"163cn\.tv/(?P<suffix>[a-zA-Z0-9]+)"


# region util funcs

Expand Down Expand Up @@ -230,7 +230,7 @@ async def rule(bot: Bot, event: Event, state: T_State):
async def resolve_music_from_msg(
message: Message,
auto_resolve: bool = False,
is_short_link: bool = False,
matched: Optional[re.Match[str]] = None,
) -> Optional[SongCache]:
msg_str = None

Expand All @@ -242,30 +242,53 @@ async def resolve_music_from_msg(
or config.ncm_resolve_playable_card
or ('"musicUrl"' not in data)
):
matched = None
msg_str = data

if not msg_str:
msg_str = message.extract_plain_text()

if is_short_link:
async with httpx.AsyncClient() as client:
req = await client.get(url=msg_str, follow_redirects=False)
msg_str = req.headers.get("Location")
short_suffix = None
if matched and (short_suffix := matched.group("suffix")):
pass
elif not matched:
short_matched = re.match(SHORT_URL_REGEX, msg_str, re.I)
if short_matched:
short_suffix = short_matched.group("suffix")

if short_suffix:
async with AsyncClient(base_url=SHORT_URL_BASE) as client:
resp = await client.get(short_suffix, follow_redirects=False)

res = re.search(SONG_URL_REGEX, msg_str, re.I)
if not res:
if resp.status_code // 100 != 3:
logger.warning(
f"Short url {short_suffix} "
f"returned invalid status code {resp.status_code}",
)
return None

resp_location = resp.headers.get("Location")
if not resp_location:
logger.warning(f"Short url {short_suffix} returned no location header")
return None

msg_str = resp_location

if not matched:
matched = re.match(SONG_URL_REGEX, msg_str, re.I)
if not matched:
return None

song_type = await get_song_from_type(res.group("type"))
song_id = int(res.group("id"))
song_type = await get_song_from_type(matched.group("type"))
song_id = int(matched.group("id"))
return SongCache(song_type, song_id)


async def rule_music_msg(event: MessageEvent, state: T_State) -> bool:
if song := await resolve_music_from_msg(
event.message,
KEY_IS_AUTO_RESOLVE in state,
KEY_IS_SHORT_LINK in state,
state.get(REGEX_MATCHED),
):
state[KEY_SONG_CACHE] = song
return bool(song)
Expand All @@ -278,7 +301,7 @@ async def rule_reply_music_msg(event: MessageEvent, state: T_State) -> bool:
if song := await resolve_music_from_msg(
event.reply.message,
KEY_IS_AUTO_RESOLVE in state,
KEY_IS_SHORT_LINK in state,
state.get(REGEX_MATCHED),
):
state[KEY_SONG_CACHE] = song

Expand Down Expand Up @@ -440,10 +463,10 @@ def register_search_handlers():
state={KEY_IS_AUTO_RESOLVE: True},
)
reg_short_resolve = on_regex(
SONG_SHORT_URL_REGEX,
SHORT_URL_REGEX,
re.I,
rule=Rule(rule_auto_resolve) & rule_has_music_msg,
state={KEY_IS_AUTO_RESOLVE: True, KEY_IS_SHORT_LINK: True},
state={KEY_IS_AUTO_RESOLVE: True},
)


Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "nonebot-plugin-multincm"
version = "0.4.0.post1"
version = "0.4.1"
description = "NCM Song Searcher"
authors = [{ name = "student_2333", email = "[email protected]" }]
dependencies = [
Expand Down

0 comments on commit e882af1

Please sign in to comment.