Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
HisAtri committed Nov 29, 2023
2 parents e5e2710 + 6a95bb0 commit 9960bd5
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
A Flask API For [StreamMusic](https://github.com/gitbobobo/StreamMusic)

<p align="center">
<img src="https://img.shields.io/badge/Python-3.10|3.11|3.12-blue.svg" alt="">
<a href="https://www.python.org/downloads/"><img src="https://img.shields.io/badge/Python-3.10|3.11|3.12-blue.svg" alt=""></a>
<a href="https://hub.docker.com/r/hisatri/lyricapi"><img src="https://img.shields.io/badge/Docker-Quick%20Start-0077ED.svg" alt=""></a>
<br>
<img src="https://img.shields.io/github/license/HisAtri/LrcApi?color=%23f280bf" alt="">
<img src="https://img.shields.io/github/commit-activity/m/HisAtri/LrcApi?color=%23bf4215" alt="">
<img src="https://img.shields.io/github/stars/HisAtri/LrcApi?style=social" alt="">
Expand All @@ -19,6 +21,8 @@ A Flask API For [StreamMusic](https://github.com/gitbobobo/StreamMusic)

支持text/json API

支持获取音乐/专辑/艺术家封面

默认监听28883端口,API地址 `http://0.0.0.0:28883/lyrics` ;新版API地址 `http://0.0.0.0:28883/jsonapi`

### 启动参数
Expand All @@ -38,7 +42,9 @@ A Flask API For [StreamMusic](https://github.com/gitbobobo/StreamMusic)

如果无法私有部署,可以先尝试使用公开API。注意:公开API通过酷狗等接口获取歌词,可能响应较慢且并不完全准确。

API地址:`https://lrc.xms.mx/lyrics`
歌词API地址:`https://lrc.xms.mx/lyrics`

封面API地址: `https://lrc.xms.mx/cover`

### 二进制文件

Expand Down Expand Up @@ -78,6 +84,10 @@ docker run -d -p 28883:28883 -v /home/user/music:/music hisatri/lyricapi:alpine-

然后访问 `http://0.0.0.0:28883/lyrics` 或新版API `http://0.0.0.0:28883/jsonapi`

图片API地址为 `http://0.0.0.0:28883/cover`

注意:图片返回目前采用反向代理策略,可能存在一定的上下行流量消耗和延迟。

支持使用Nginx或Apache进行反向代理与SSL。

## 常见状态码及可能含义
Expand Down
43 changes: 42 additions & 1 deletion app.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from flask import Flask, request, abort, redirect, send_from_directory
import requests
from flask import Flask, request, abort, redirect, send_from_directory, Response
from flask_caching import Cache
import os
import hashlib
Expand Down Expand Up @@ -62,6 +63,19 @@ def calculate_md5(string):
return md5_hex


# 跟踪重定向
def follow_redirects(url, max_redirects=10):
for _ in range(max_redirects):
response = requests.head(url, allow_redirects=False)
if response.status_code == 200:
return url
elif 300 <= response.status_code < 400:
url = response.headers['Location']
else:
abort(404) # 或者根据需求选择其他状态码
abort(404) # 达到最大重定向次数仍未获得 200 状态码,放弃


def read_file_with_encoding(file_path, encodings):
for encoding in encodings:
try:
Expand All @@ -87,6 +101,16 @@ def lyrics():
file_content = read_file_with_encoding(lrc_path, ['utf-8', 'gbk'])
if file_content is not None:
return lrc.standard(file_content)
try:
audio = EasyID3(path)
lyrics_key = 'lyrics'
if lyrics_key in audio:
# 获取歌词内容
lyrics_content = audio[lyrics_key][0]
# 返回歌词文本
return lyrics_content
except:
pass
try:
# 通过request参数获取音乐Tag
title = unquote_plus(request.args.get('title'))
Expand Down Expand Up @@ -138,6 +162,23 @@ def lrc_json():
return response


@app.route('/cover', methods=['GET'])
@cache.cached(timeout=86400, key_prefix=make_cache_key)
def cover_api():
req_args = {key: request.args.get(key) for key in request.args}
# 构建目标URL
target_url = 'https://lrc.xms.mx/cover?' + '&'.join([f"{key}={req_args[key]}" for key in req_args])
# 跟踪重定向并获取最终URL
final_url = follow_redirects(target_url)
# 获取最终URL的内容或响应
response = requests.get(final_url)
if response.status_code == 200:
content_type = response.headers.get('Content-Type', 'application/octet-stream')
return Response(response.content, content_type=content_type)
else:
abort(404)


def validate_json_structure(data):
if not isinstance(data, dict):
return False
Expand Down

0 comments on commit 9960bd5

Please sign in to comment.