thief_music_api/server/rank_data.py

30 lines
795 B
Python
Raw Permalink Normal View History

2023-08-04 19:38:09 +08:00
import datetime
import json
import os
def get_cache_filename(rank_id):
2023-08-06 18:42:48 +08:00
# 生成缓存文件名以rank_id命名
# date_str = datetime.datetime.now().strftime("%Y%m%d")
return f"{rank_id}.json"
2023-08-04 19:38:09 +08:00
def read_cache(rank_id):
# 从缓存文件中读取数据
cache_filename = get_cache_filename(rank_id)
2023-08-06 18:42:48 +08:00
if os.path.exists(f"data/{cache_filename}"):
with open(f"data/{cache_filename}", "r", encoding="utf8") as file:
2023-08-04 19:38:09 +08:00
return json.load(file)
return None
2023-08-06 18:42:48 +08:00
2023-08-04 19:38:09 +08:00
def write_cache(rank_id, data):
# 将数据写入缓存文件
cache_filename = get_cache_filename(rank_id)
2023-08-06 18:42:48 +08:00
# 创建文件
if not os.path.exists("data"):
os.mkdir("data")
with open(f"data/{cache_filename}", "w", encoding="utf8") as file:
json.dump(data, file)