first commit

This commit is contained in:
2061360308 2023-12-06 12:35:16 +08:00
commit 7864212756
5 changed files with 43367 additions and 0 deletions

4
.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
.idea/
__pycache__/
.env
cookie_storage

43231
NeteaseCloudMusicApi.js Normal file

File diff suppressed because it is too large Load Diff

87
main.py Normal file
View File

@ -0,0 +1,87 @@
import json
import os.path
import socket
from pprint import pprint
import requests
from py_mini_racer import py_mini_racer
class NeteaseCloudMusicApi:
def __init__(self):
with open('NeteaseCloudMusicApi.js', 'r', encoding='utf-8') as file:
js_code = file.read()
self.ctx = py_mini_racer.MiniRacer()
self.ctx.eval(js_code)
self.DEBUG = False
self.__cookie = None
self.__ip = None
@staticmethod
def get_local_ip():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
# doesn't even have to be reachable
s.connect(('10.255.255.255', 1))
IP = s.getsockname()[0]
except Exception:
print("get local ip error")
IP = "116.25.146.177"
finally:
s.close()
return IP
@property
def cookie(self):
if self.__cookie is None:
if os.path.isfile("cookie_storage"):
with open("cookie_storage", "r", encoding='utf-8') as f:
self.__cookie = f.read()
else:
raise Exception("cookie not found")
else:
return self.__cookie
@cookie.setter
def cookie(self, cookie):
self.__cookie = cookie
with open("cookie_storage", "w+", encoding='utf-8') as f:
f.write(cookie)
@property
def ip(self):
if self.__ip is None:
self.__ip = self.get_local_ip()
return self.__ip
def getRequestParam(self, name, query):
result = self.ctx.call('NeteaseCloudMusicApi', name, query) # 拿到请求头和请求参数
if result.get("error"):
raise Exception(result.get("error"))
if self.DEBUG:
print("RequestParam:")
for item in result.keys():
print(f" - {item}:{result[item]}")
# pprint(json.dumps(result))
with open('result.json', 'w', encoding='utf-8') as file:
file.write(json.dumps(result))
return result
def api(self, name, query=None):
if query is None:
query = {}
if query.get("cookie") is None:
query["cookie"] = self.cookie
if query.get("realIP") is None:
query["ip"] = self.ip
else:
query["ip"] = query.get("realIP")
result = self.getRequestParam(name, query)
response = requests.post(result["url"], data=result["data"], headers=result["headers"])
return response

1
result.json Normal file
View File

@ -0,0 +1 @@
{"method": "POST", "url": "https://music.163.com/weapi/nuser/account/get", "headers": {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36 Edg/116.0.1938.69", "Content-Type": "application/x-www-form-urlencoded", "Referer": "https://music.163.com", "Cookie": "MUSIC_R_T=1579622000495; %20Max-Age=2147483647; %20Expires=Sun%2C%2023%20Dec%202091%2019%3A11%3A46%20GMT; %20Path=%2Fweapi%2Ffeedback; %20HTTPOnly=; MUSIC_A_T=1579621885297; __csrf=f2479af05431c9fe000004d74b9eb43c; =; MUSIC_U=00F5809E26613FA34E24E56B87BB2D71C716ECA62F1D492FDDB5915792654A9C101BE0AFDDFB7A79A405394645D6A0BCCEF96AF99CD68BB226B3AD52F1A990A41E4D4E359E8FBE202B246A605453D0BA8EA9370E2CF943DC198B5ADF131DBD233495C69C0BA6B144FD3EB4A3D356B95B15FF8FA61D19A34DA56A980797377346706BD36CB666B2BA320AFF4E140BB208BBC6CBA15716BF7B98CB0A92EF1032735321FD39465B9E8B34E8C241A1BD5DA0C77501BF407AF6AA382C7B589AB18B92B856600EF1D81FC7A3E5B80339CC9DA04A0D32E85563B018CFDBEDC86F8972B6D78F6704AA10CEDBB7E43F3FC654D0D3EC885D09A503057B92912B28763893F0DDCE9995439D4E047867F8BDEB01C4B3C4C3717D3049C5A13BC16EF182455B02F3F9CA81185292BED1FD7493A07FCAB3B5676A7FD58F8BF4F5B09F752C6F235C034D297711FBE960A3CAF5504E62EF6547; MUSIC_SNS=; __remember_me=; _ntes_nuid=9743f172c9fbf0df0cd5bea497f762a5; NMTID=c6fb4ebb7bed565569b8bfa999a6af17"}, "data": "params=zK+rehRY4wqhBJAW+jl2F/w/2ohUVNlDEzSC9mthy399eXXDw5NB8rscOLoSkz6bXMSSO9wLSLY8978bQ4gdKso3KMtol4oLDrW8Lgw1V8YKYfOXcponbBPa84BUZOwp&encSecKey=4e50790bdfb1043fc3d2295dc65a517184851020ece86d7be5890cb2d7a51a06a429bacb42101137585e79fd6246f5bd5d45d0a35a611c1baee61b56e2f99ba00932294b32fd061b37b7bdd25bfba2179497ac17312c26a2aa67f0286e0e84cf4cdd8a83f698f1338803514768cffa18f22aa8a16a6a53d7358dd09adf40164f", "keepAlive": true, "proxy": false, "httpAgent": null, "httpsAgent": null}

44
test.py Normal file
View File

@ -0,0 +1,44 @@
import json
import os
from pprint import pprint
import dotenv
from main import NeteaseCloudMusicApi
dotenv.load_dotenv()
netease_cloud_music_api = NeteaseCloudMusicApi()
netease_cloud_music_api.cookie = os.getenv("COOKIE")
netease_cloud_music_api.DEBUG = True
def songv1_test():
# 获取歌曲详情
response = netease_cloud_music_api.api("song_url_v1", {"id": 33894312, "level": "exhigh"})
pprint(json.loads(response.text))
def search_test():
# 搜索
response = netease_cloud_music_api.api("search", {"keywords": "海阔天空"})
print("|", response.text, "|")
# pprint(json.loads(response.text))
def search_default_test():
# 搜索
response = netease_cloud_music_api.api("search_default")
pprint(json.loads(response.text))
def user_account_test():
# 获取用户账号信息
response = netease_cloud_music_api.api("user_account")
pprint(response.text)
if __name__ == '__main__':
# songv1_test()
# search_test()
# search_default_test()
user_account_test()