fix:修复本地json榜单
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
1264204425 2023-08-06 18:42:48 +08:00
parent 4ee2e545a9
commit 3c96772a56
6 changed files with 63 additions and 39 deletions

74
api.py
View File

@ -68,7 +68,7 @@ async def login(username: str = fastapi.Form(..., description="用户名"),
# 榜单获取
@app.get("/get_rank_test")
@app.get("/get_rank")
async def get_rank(
rank_id: str = fastapi.Query(..., description="榜单类型")):
"""
@ -85,42 +85,56 @@ async def get_rank(
" AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36"
}
url = f"https://music.163.com/api/playlist/detail?id={rank_id}"
# 缓存文件不存在,从网络获取榜单数据
response = requests.get(url, headers=headers)
data = response.json()
return {"message": "success", "data": data}
@app.get("/get_rank")
async def get_rank(rank_id: str = fastapi.Query(..., description="榜单类型")):
"""
获取音乐榜单数据
:param rank_id: 榜单ID
19723756 云音乐飙升榜
3779629 云音乐新歌榜
3778678 云音乐热歌榜
2884035 云音乐原创榜
:return: 榜单数据
"""
# 检查是否有缓存数据
# 如果请求code为-447 那么继续返回json
if response.status_code == -447:
return {"message": "请求code为-447传入本地JSON数据", "data": response.json()}
else:
# 检测是否含有带rank_id的缓存文件
cached_data = read_cache(rank_id)
if cached_data:
return {"message": "success", "data": cached_data}
return {"message": "本地JSON数据", "data": cached_data}
headers = {
"Referer": "https://music.163.com/",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64)"
" AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36"
}
# 缓存数据不存在,从网络获取榜单数据
url = f"https://music.163.com/api/playlist/detail?id={rank_id}"
response = requests.get(url, headers=headers)
# 将榜单数据写入缓存文件
write_cache(rank_id, response.json())
print(response.json())
data = response.json()
# 将数据写入缓存文件
write_cache(rank_id, data)
return {"message": "请求网易云并缓存本地JSON成功", "data": data}
return {"message": "success", "data": data}
# @app.get("/get_rank")
# async def get_rank(rank_id: str = fastapi.Query(..., description="榜单类型")):
# """
# 获取音乐榜单数据
# :param rank_id: 榜单ID
# 19723756 云音乐飙升榜
# 3779629 云音乐新歌榜
# 3778678 云音乐热歌榜
# 2884035 云音乐原创榜
# :return: 榜单数据
# """
# # 检查是否有缓存数据
# cached_data = read_cache(rank_id)
# if cached_data:
# return {"message": "success", "data": cached_data}
#
# headers = {
# "Referer": "https://music.163.com/",
# "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64)"
# " AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36"
# }
# # 缓存数据不存在,从网络获取榜单数据
# url = f"https://music.163.com/api/playlist/detail?id={rank_id}"
# response = requests.get(url, headers=headers)
# data = response.json()
#
# # 将数据写入缓存文件
# write_cache(rank_id, data)
#
# return {"message": "success", "data": data}
# 根据歌曲名称搜索歌曲

1
data/19723756.json Normal file

File diff suppressed because one or more lines are too long

1
data/2884035 .json Normal file

File diff suppressed because one or more lines are too long

1
data/3778678.json Normal file

File diff suppressed because one or more lines are too long

1
data/3779629.json Normal file

File diff suppressed because one or more lines are too long

View File

@ -4,20 +4,26 @@ import os
def get_cache_filename(rank_id):
# 生成缓存文件名,以年月日+rank_id命名
date_str = datetime.datetime.now().strftime("%Y%m%d")
return f"{date_str}_{rank_id}.json"
# 生成缓存文件名以rank_id命名
# date_str = datetime.datetime.now().strftime("%Y%m%d")
return f"{rank_id}.json"
def read_cache(rank_id):
# 从缓存文件中读取数据
cache_filename = get_cache_filename(rank_id)
if os.path.exists(cache_filename):
with open(cache_filename, "r") as file:
if os.path.exists(f"data/{cache_filename}"):
with open(f"data/{cache_filename}", "r", encoding="utf8") as file:
return json.load(file)
return None
def write_cache(rank_id, data):
# 将数据写入缓存文件
cache_filename = get_cache_filename(rank_id)
with open(cache_filename, "w") as file:
# 创建文件
if not os.path.exists("data"):
os.mkdir("data")
with open(f"data/{cache_filename}", "w", encoding="utf8") as file:
json.dump(data, file)