Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

修改模块mod/search中异步请求的错误实现 #16

Merged
merged 2 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
修改模块mod/search中异步请求的错误实现
  • Loading branch information
HisAtri committed Jan 17, 2024
commit 7aa5a73f99e7b7fc75d3e2a1e618fd0b7b1a021d
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ A Flask API For [StreamMusic](https://github.com/gitbobobo/StreamMusic)

<div align="center">
<a href="README.md">中文</a> | <a href="README_EN.md">English</a>
<br>
<a href="https://docs.lrc.cx/" target="_blank">查阅文档</a>
</div>

## 功能
Expand All @@ -31,10 +33,10 @@ A Flask API For [StreamMusic](https://github.com/gitbobobo/StreamMusic)

### 启动参数

| 参数 | 类型 | 默认值 |
| -------- | -------- | -------- |
| `--port` | int | 28883 |
| `--auth` | str | |
| 参数 | 类型 | 默认值 |
|----------|-----|-------|
| `--port` | int | 28883 |
| `--auth` | str | |

`--auth`参数用于header鉴权,留空则跳过鉴权。验证header中的`Authorization`或`Authentication`字段。如果鉴权不符合,则返回403响应。

Expand Down
114 changes: 61 additions & 53 deletions mod/search.py
Original file line number Diff line number Diff line change
@@ -1,52 +1,57 @@
import base64
import requests
import json

import aiohttp
import asyncio
from mod import textcompare


ua = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36"
UA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36"


async def kugou(title, artist, album):
headers = {'User-Agent': ua, }
headers = {'User-Agent': UA, }
limit = 10
# 第一层Json,要求获得Hash值
response = requests.get(
f'http://mobilecdn.kugou.com/api/v3/search/song?format=json&keyword={title} {artist} {album}&page=1&pagesize=2&showtype=1',
headers=headers,
timeout=10)
if response.status_code == 200:
song_info = response.json()["data"]["info"]
if len(song_info) >= 1:
ratio_max = 0.2
for index in range(min(limit, len(song_info))):
song_item = song_info[index]
song_name = song_item["songname"]
singer_name = song_item.get("singername", "")
song_hash = song_item["hash"]
album_name = song_item.get("album_name", "")
title_conform_ratio = textcompare.association(title, song_name)
artist_conform_ratio = textcompare.assoc_artists(artist, singer_name)
# 计算两个指标的几何平均值;区间范围(0,1]
ratio = (title_conform_ratio * artist_conform_ratio) ** 0.5
ratio_max = max(ratio, ratio_max)
if ratio >= ratio_max:
response2 = requests.get(
f"https://krcs.kugou.com/search?ver=1&man=yes&client=mobi&keyword=&duration=&hash={song_hash}&album_audio_id=",
headers=headers,
timeout=10)
lyrics_info = response2.json()
lyrics_id = lyrics_info["candidates"][0]["id"]
lyrics_key = lyrics_info["candidates"][0]["accesskey"]
# 第三层Json,要求获得并解码Base64
response3 = requests.get(
f"http://lyrics.kugou.com/download?ver=1&client=pc&id={lyrics_id}&accesskey={lyrics_key}&fmt=lrc&charset=utf8",
headers=headers,
timeout=10)
lyrics_data = response3.json()
lyrics_encode = lyrics_data["content"] # 这里是Base64编码的数据
lrc_text = base64.b64decode(lyrics_encode).decode('utf-8') # 这里解码
return lrc_text

async with aiohttp.ClientSession() as session:
# 第一层Json,要求获得Hash值
async with session.get(
f'http://mobilecdn.kugou.com/api/v3/search/song?format=json&keyword={title} {artist} {album}&page=1&pagesize=2&showtype=1',
headers=headers,
timeout=10) as response:
if response.status == 200:
song_info_t = await response.text()
song_info = json.loads(song_info_t)
song_info = song_info["data"]["info"]
if len(song_info) >= 1:
ratio_max = 0.2
for index in range(min(limit, len(song_info))):
song_item = song_info[index]
song_name = song_item["songname"]
singer_name = song_item.get("singername", "")
song_hash = song_item["hash"]
album_name = song_item.get("album_name", "")
title_conform_ratio = textcompare.association(title, song_name)
artist_conform_ratio = textcompare.assoc_artists(artist, singer_name)
# 计算两个指标的几何平均值;范围(0,1]
ratio = (title_conform_ratio * artist_conform_ratio) ** 0.5
ratio_max = max(ratio, ratio_max)
if ratio >= ratio_max:
async with session.get(
f"https://krcs.kugou.com/search?ver=1&man=yes&client=mobi&keyword=&duration=&hash={song_hash}&album_audio_id=",
headers=headers,
timeout=10) as response2:
lyrics_info = await response2.json()
lyrics_id = lyrics_info["candidates"][0]["id"]
lyrics_key = lyrics_info["candidates"][0]["accesskey"]
# 第三层Json,要求获得并解码Base64
async with session.get(
f"http://lyrics.kugou.com/download?ver=1&client=pc&id={lyrics_id}&accesskey={lyrics_key}&fmt=lrc&charset=utf8",
headers=headers,
timeout=10) as response3:
lyrics_data = await response3.json()
lyrics_encode = lyrics_data["content"] # 这里是Base64编码的数据
lrc_text = base64.b64decode(lyrics_encode).decode('utf-8') # 这里解码
return lrc_text
await asyncio.sleep(10)
return None

Expand All @@ -56,17 +61,20 @@ async def api_2(title, artist, album):
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 LrcAPI/1.1',
}
url = f"http://api.lrc.cx/lyrics?title={title}&artist={artist}&album={album}&path=None&limit=1&api=lrcapi"
try:
res = requests.get(url, headers=headers, timeout=30)
if res.status_code < 300:
return res.text
else:
res = requests.get(url, headers=headers, timeout=30)
if res.status_code < 300:
return res.text
except:
await asyncio.sleep(10)
return None

async with aiohttp.ClientSession() as session:
try:
async with session.get(url, headers=headers, timeout=30) as response:
if response.status < 300:
return await response.text()
else:
async with session.get(url, headers=headers, timeout=30) as retry_response:
if retry_response.status < 300:
return await retry_response.text()
except Exception as e:
print(f"Request failed: {e}")
await asyncio.sleep(10)

return None


Expand Down Expand Up @@ -102,4 +110,4 @@ def allin(title, artist, album):


if __name__ == "__main__":
print(allin("醉花阴", "洛天依", ""))
print(allin("大地", "Beyond", ""))
6 changes: 6 additions & 0 deletions mod/textcompare.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
from mod.ttscn import t2s


"""
本模块算法针对常见音乐标题匹配场景应用,着重分离度和效率。
Levenshtein Distance算法实际表现不佳
目前没有好的轻量nn实现,不考虑上模型
当前数据集R~=0.8
"""
def text_convert(text: str):
patterns = [
r"(?<!^)\([^)]+?\)",
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ Flask_Caching==2.0.2
Requests==2.31.0
tinytag==1.9.0
waitress==2.1.2
mutagen==1.46.0
mutagen==1.46.0
aiohttp>=3.9.1