Skip to content

Commit

Permalink
Merge pull request #53 from katagomo/main
Browse files Browse the repository at this point in the history
更新本地封面接口
  • Loading branch information
HisAtri authored Jun 3, 2024
2 parents e4df59b + 2b8cc46 commit 388e8e6
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
20 changes: 19 additions & 1 deletion api/cover.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import requests

from flask import request, abort, redirect
from urllib.parse import unquote_plus

from mod import searchx


# 跟踪重定向
Expand All @@ -18,19 +21,34 @@ def follow_redirects(url, max_redirects=10):
abort(404) # 达到最大重定向次数仍未获得 200 状态码,放弃


def local_cover_search(title: str, artist: str, album: str):
result: list = searchx.search_all(title=title, artist=artist, album=album, timeout=30)
for item in result:
if cover_url := item.get('cover'):
res = requests.get(cover_url,
headers={"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/"})
if res.status_code == 200:
return res.content, 200, {"Content-Type": res.headers['Content-Type']}


@app.route('/cover', methods=['GET'])
@cache.cached(timeout=86400, key_prefix=make_cache_key)
def cover_api():
title = unquote_plus(request.args.get('title'))
artist = unquote_plus(request.args.get('artist', ''))
album = unquote_plus(request.args.get('album', ''))
req_args = {key: request.args.get(key) for key in request.args}
# 构建目标URL
target_url = 'http://api.lrc.cx/cover?' + '&'.join([f"{key}={req_args[key]}" for key in req_args])
result = requests.get(target_url, headers={"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/"})
if result.status_code == 200:
return result.content, 200, {"Content-Type": result.headers['Content-Type']}
elif res := local_cover_search(title, artist, album):
return res
elif result.status_code == 404:
abort(404)
else:
abort(500)
abort(500, '后端存在错误,暂时无法查询')


@v1_bp.route('/cover/<path:s_type>', methods=['GET'])
Expand Down
2 changes: 1 addition & 1 deletion api/lyrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from flask import request, abort, jsonify, render_template_string
from urllib.parse import unquote_plus

from mod import search, lrc
from mod import lrc
from mod import searchx
from mod import tools
from mod import tag
Expand Down
6 changes: 4 additions & 2 deletions mod/searchx/kugou.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import time
import logging

from functools import lru_cache

from mod import textcompare
from mod import tools

Expand Down Expand Up @@ -49,7 +51,6 @@ async def a_search(title='', artist='', album=''):
return None
result_list = []
limit = 3

async with aiohttp.ClientSession() as session:
async with session.get(
f"http://mobilecdn.kugou.com/api/v3/search/song?format=json&keyword={' '.join([item for item in [title, artist, album] if item])}&page=1&pagesize=2&showtype=1",
Expand Down Expand Up @@ -106,11 +107,12 @@ async def a_search(title='', artist='', album=''):
return [i.get('data') for i in sort_li]


@lru_cache(maxsize=64)
@no_error(throw=logger.info,
exceptions=(aiohttp.ClientError, asyncio.TimeoutError, KeyError, IndexError, AttributeError))
def search(title='', artist='', album=''):
return asyncio.run(a_search(title=title, artist=artist, album=album))


if __name__ == "__main__":
print(search(album="光辉岁月"))
print(search(album="十年"))

0 comments on commit 388e8e6

Please sign in to comment.