feat:add caching function

This commit is contained in:
2061360308 2023-12-16 23:18:26 +08:00
parent dcbd290d78
commit cc1b12dc36
9 changed files with 27 additions and 7 deletions

4
.gitignore vendored
View File

@ -2,4 +2,6 @@
__pycache__/
.env
.pytest_cache/
cookie_storage
cookie_storage
/情非得已 (童声版) - 群星.mp3
/cache/

View File

@ -1,6 +1,6 @@
Metadata-Version: 2.1
Name: NeteaseCloudMusic
Version: 0.1.6
Version: 0.1.7
Summary: 网易云音乐API NeteaseCloudMusicApi项目的 Python SDK
Home-page: https://github.com/2061360308/NeteaseCloudMusic_PythonSDK
Author: 盧瞳

View File

@ -4,6 +4,7 @@ import socket
from pprint import pprint
import http.cookies
import datetime
from diskcache import Cache
import pkg_resources
import requests
@ -15,6 +16,10 @@ class NeteaseCloudMusicApi:
__cookie = None
__ip = None
cache = Cache('cache', timeout=120) # 设置缓存目录和过期时间
# cache = TTLCache(maxsize=100, ttl=120) # 设置缓存大小为100缓存项的生存时间为120秒
def __init__(self, debug=False):
self.DEBUG = debug # 是否开启调试模式
@ -66,8 +71,20 @@ class NeteaseCloudMusicApi:
if name not in yubei_special.values():
raise Exception(f"apiName: {name} not foundplease use ”api_list()“ to view the interface list")
# 生成一个唯一的键,用于在缓存中查找结果
cache_key = (name, frozenset(query.items()) if query else None)
# 检查缓存中是否已经有了结果
if self.cache.get(cache_key):
return self.cache.get(cache_key)
if query is None:
query = {}
else:
# 如果存在timestamp参数那么删除它
if query.get("timestamp"):
del query["timestamp"]
if query.get("cookie") is None:
query["cookie"] = self.cookie
@ -82,7 +99,8 @@ class NeteaseCloudMusicApi:
else:
result = self.call_api(name, query)
#
# 将结果存入缓存
self.cache.set(cache_key, result)
return result

Binary file not shown.

Binary file not shown.

View File

@ -19,14 +19,14 @@ URL = 'https://github.com/2061360308/NeteaseCloudMusic_PythonSDK'
EMAIL = '2061360308@qq.com'
AUTHOR = '盧瞳'
REQUIRES_PYTHON = '>=3.6.0'
VERSION = '0.1.6'
VERSION = '0.1.7'
UPDATA_INFO = ('修复了初次使用时没有cookie导致的一系列问题\n'
'修复了NeteaseCloudMusicApi.js没有更新的问题\n'
'添加了对于cookie的判断现在可以正常判断cookie是否过期了')
# What packages are required for this module to be executed?
REQUIRED = [
'py_mini_racer', 'requests'
'py_mini_racer', 'requests', 'diskcache'
# 'requests', 'maya', 'records',
]

Binary file not shown.

View File

@ -95,7 +95,7 @@ if __name__ == '__main__':
pass
# print(api_list())
# print(api_help())
# song_url_v1_test()
song_url_v1_test()
# top_mv_test()
# search_test()
# search_default_test()
@ -105,4 +105,4 @@ if __name__ == '__main__':
# playlist_detail_test()
# top_playlist_highquality_test()
# captcha_sent_test()
login_cellphone_test()
# login_cellphone_test()