This commit is contained in:
parent
4ee2e545a9
commit
3c96772a56
78
api.py
78
api.py
@ -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(
|
async def get_rank(
|
||||||
rank_id: str = fastapi.Query(..., description="榜单类型")):
|
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"
|
" 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}"
|
url = f"https://music.163.com/api/playlist/detail?id={rank_id}"
|
||||||
|
|
||||||
|
# 缓存文件不存在,从网络获取榜单数据
|
||||||
response = requests.get(url, headers=headers)
|
response = requests.get(url, headers=headers)
|
||||||
|
# 如果请求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": "本地JSON数据", "data": cached_data}
|
||||||
|
|
||||||
|
# 将榜单数据写入缓存文件
|
||||||
|
write_cache(rank_id, response.json())
|
||||||
|
print(response.json())
|
||||||
data = response.json()
|
data = response.json()
|
||||||
|
|
||||||
return {"message": "success", "data": data}
|
return {"message": "请求网易云并缓存本地JSON成功", "data": data}
|
||||||
|
|
||||||
|
|
||||||
@app.get("/get_rank")
|
# @app.get("/get_rank")
|
||||||
async def get_rank(rank_id: str = fastapi.Query(..., description="榜单类型")):
|
# async def get_rank(rank_id: str = fastapi.Query(..., description="榜单类型")):
|
||||||
"""
|
# """
|
||||||
获取音乐榜单数据
|
# 获取音乐榜单数据
|
||||||
:param rank_id: 榜单ID
|
# :param rank_id: 榜单ID
|
||||||
19723756 云音乐飙升榜
|
# 19723756 云音乐飙升榜
|
||||||
3779629 云音乐新歌榜
|
# 3779629 云音乐新歌榜
|
||||||
3778678 云音乐热歌榜
|
# 3778678 云音乐热歌榜
|
||||||
2884035 云音乐原创榜
|
# 2884035 云音乐原创榜
|
||||||
:return: 榜单数据
|
# :return: 榜单数据
|
||||||
"""
|
# """
|
||||||
# 检查是否有缓存数据
|
# # 检查是否有缓存数据
|
||||||
cached_data = read_cache(rank_id)
|
# cached_data = read_cache(rank_id)
|
||||||
if cached_data:
|
# if cached_data:
|
||||||
return {"message": "success", "data": cached_data}
|
# return {"message": "success", "data": cached_data}
|
||||||
|
#
|
||||||
headers = {
|
# headers = {
|
||||||
"Referer": "https://music.163.com/",
|
# "Referer": "https://music.163.com/",
|
||||||
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64)"
|
# "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"
|
# " 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}"
|
# url = f"https://music.163.com/api/playlist/detail?id={rank_id}"
|
||||||
response = requests.get(url, headers=headers)
|
# response = requests.get(url, headers=headers)
|
||||||
data = response.json()
|
# data = response.json()
|
||||||
|
#
|
||||||
# 将数据写入缓存文件
|
# # 将数据写入缓存文件
|
||||||
write_cache(rank_id, data)
|
# write_cache(rank_id, data)
|
||||||
|
#
|
||||||
return {"message": "success", "data": data}
|
# return {"message": "success", "data": data}
|
||||||
|
|
||||||
|
|
||||||
# 根据歌曲名称搜索歌曲
|
# 根据歌曲名称搜索歌曲
|
||||||
|
1
data/19723756.json
Normal file
1
data/19723756.json
Normal file
File diff suppressed because one or more lines are too long
1
data/2884035 .json
Normal file
1
data/2884035 .json
Normal file
File diff suppressed because one or more lines are too long
1
data/3778678.json
Normal file
1
data/3778678.json
Normal file
File diff suppressed because one or more lines are too long
1
data/3779629.json
Normal file
1
data/3779629.json
Normal file
File diff suppressed because one or more lines are too long
@ -4,20 +4,26 @@ import os
|
|||||||
|
|
||||||
|
|
||||||
def get_cache_filename(rank_id):
|
def get_cache_filename(rank_id):
|
||||||
# 生成缓存文件名,以年月日+rank_id命名
|
# 生成缓存文件名,以rank_id命名
|
||||||
date_str = datetime.datetime.now().strftime("%Y%m%d")
|
# date_str = datetime.datetime.now().strftime("%Y%m%d")
|
||||||
return f"{date_str}_{rank_id}.json"
|
return f"{rank_id}.json"
|
||||||
|
|
||||||
|
|
||||||
def read_cache(rank_id):
|
def read_cache(rank_id):
|
||||||
# 从缓存文件中读取数据
|
# 从缓存文件中读取数据
|
||||||
cache_filename = get_cache_filename(rank_id)
|
cache_filename = get_cache_filename(rank_id)
|
||||||
if os.path.exists(cache_filename):
|
if os.path.exists(f"data/{cache_filename}"):
|
||||||
with open(cache_filename, "r") as file:
|
with open(f"data/{cache_filename}", "r", encoding="utf8") as file:
|
||||||
return json.load(file)
|
return json.load(file)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def write_cache(rank_id, data):
|
def write_cache(rank_id, data):
|
||||||
# 将数据写入缓存文件
|
# 将数据写入缓存文件
|
||||||
cache_filename = get_cache_filename(rank_id)
|
cache_filename = get_cache_filename(rank_id)
|
||||||
with open(cache_filename, "w") as file:
|
# 创建文件
|
||||||
json.dump(data, 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)
|
||||||
|
Loading…
Reference in New Issue
Block a user