diff --git a/.gitignore b/.gitignore index 8a77e1f..e598775 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,6 @@ __pycache__/ .env .pytest_cache/ -cookie_storage \ No newline at end of file +cookie_storage +/情非得已 (童声版) - 群星.mp3 +/cache/ diff --git a/package/NeteaseCloudMusic.egg-info/PKG-INFO b/package/NeteaseCloudMusic.egg-info/PKG-INFO index 608a06e..16ff9db 100644 --- a/package/NeteaseCloudMusic.egg-info/PKG-INFO +++ b/package/NeteaseCloudMusic.egg-info/PKG-INFO @@ -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: 盧瞳 diff --git a/package/NeteaseCloudMusic/main.py b/package/NeteaseCloudMusic/main.py index 7f973ef..d26f7d0 100644 --- a/package/NeteaseCloudMusic/main.py +++ b/package/NeteaseCloudMusic/main.py @@ -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 found,please 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 diff --git a/package/dist/NeteaseCloudMusic-0.1.6.tar.gz b/package/dist/NeteaseCloudMusic-0.1.6.tar.gz deleted file mode 100644 index 3093806..0000000 Binary files a/package/dist/NeteaseCloudMusic-0.1.6.tar.gz and /dev/null differ diff --git a/package/dist/NeteaseCloudMusic-0.1.6-py2.py3-none-any.whl b/package/dist/NeteaseCloudMusic-0.1.7-py2.py3-none-any.whl similarity index 98% rename from package/dist/NeteaseCloudMusic-0.1.6-py2.py3-none-any.whl rename to package/dist/NeteaseCloudMusic-0.1.7-py2.py3-none-any.whl index a5e0b9f..c3c8999 100644 Binary files a/package/dist/NeteaseCloudMusic-0.1.6-py2.py3-none-any.whl and b/package/dist/NeteaseCloudMusic-0.1.7-py2.py3-none-any.whl differ diff --git a/package/dist/NeteaseCloudMusic-0.1.7.tar.gz b/package/dist/NeteaseCloudMusic-0.1.7.tar.gz new file mode 100644 index 0000000..d9efc1c Binary files /dev/null and b/package/dist/NeteaseCloudMusic-0.1.7.tar.gz differ diff --git a/package/setup.py b/package/setup.py index 453fe43..1931789 100644 --- a/package/setup.py +++ b/package/setup.py @@ -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', ] diff --git a/requirements.txt b/requirements.txt index 64ea9be..f5066e8 100644 Binary files a/requirements.txt and b/requirements.txt differ diff --git a/test.py b/test.py index a8317f7..a335c14 100644 --- a/test.py +++ b/test.py @@ -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()