测试json返回
This commit is contained in:
parent
60ba47ac32
commit
d1487ffb1e
42
api.py
42
api.py
@ -5,6 +5,7 @@ import uvicorn
|
||||
from fastapi import FastAPI
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from service.userservice import *
|
||||
from service.rank_data import *
|
||||
|
||||
env = os.environ
|
||||
app = FastAPI()
|
||||
@ -67,7 +68,7 @@ async def login(username: str = fastapi.Form(..., description="用户名"),
|
||||
|
||||
|
||||
# 榜单获取
|
||||
@app.get("/get_rank")
|
||||
@app.get("/get_rank_test")
|
||||
async def get_rank(
|
||||
rank_id: str = fastapi.Query(..., description="榜单类型")):
|
||||
"""
|
||||
@ -78,13 +79,50 @@ async def get_rank(
|
||||
2884035 云音乐原创榜
|
||||
:return:
|
||||
"""
|
||||
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)
|
||||
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: 榜单数据
|
||||
"""
|
||||
# 检查是否有缓存数据
|
||||
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}
|
||||
|
||||
|
||||
# 根据歌曲名称搜索歌曲
|
||||
@app.get("/search_song_by_name")
|
||||
async def search_song_by_name(
|
||||
|
23
service/rank_data.py
Normal file
23
service/rank_data.py
Normal file
@ -0,0 +1,23 @@
|
||||
import datetime
|
||||
import json
|
||||
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"
|
||||
|
||||
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:
|
||||
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:
|
||||
json.dump(data, file)
|
Loading…
Reference in New Issue
Block a user