mirror of
https://github.com/2061360308/NeteaseCloudMusic_PythonSDK.git
synced 2025-01-30 15:08:37 +00:00
feat:add caching function
This commit is contained in:
parent
dcbd290d78
commit
cc1b12dc36
4
.gitignore
vendored
4
.gitignore
vendored
@ -2,4 +2,6 @@
|
||||
__pycache__/
|
||||
.env
|
||||
.pytest_cache/
|
||||
cookie_storage
|
||||
cookie_storage
|
||||
/情非得已 (童声版) - 群星.mp3
|
||||
/cache/
|
||||
|
@ -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: 盧瞳
|
||||
|
@ -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
|
||||
|
||||
|
BIN
package/dist/NeteaseCloudMusic-0.1.6.tar.gz
vendored
BIN
package/dist/NeteaseCloudMusic-0.1.6.tar.gz
vendored
Binary file not shown.
Binary file not shown.
BIN
package/dist/NeteaseCloudMusic-0.1.7.tar.gz
vendored
Normal file
BIN
package/dist/NeteaseCloudMusic-0.1.7.tar.gz
vendored
Normal file
Binary file not shown.
@ -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',
|
||||
]
|
||||
|
||||
|
BIN
requirements.txt
BIN
requirements.txt
Binary file not shown.
4
test.py
4
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()
|
||||
|
Loading…
Reference in New Issue
Block a user