Repository navigation
Expand file tree
/
Copy pathcore_api.py
More file actions
116 lines (101 loc) · 4.08 KB
/
Copy pathcore_api.py
File metadata and controls
116 lines (101 loc) · 4.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import requests
import time
import json
import os
class NeteaseCloudMatchAPI:
def __init__(self):
self.session = requests.Session()
self.session.headers.update({
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
'Referer': 'https://music.163.com/',
'Accept': 'application/json, text/plain, */*'
})
self.session.trust_env = False # 禁用系统代理
self.cookie_file = 'cookies.json'
self.user_profile = None
def load_cookies(self):
"""从本地文件加载 Cookie"""
if os.path.exists(self.cookie_file):
try:
with open(self.cookie_file, 'r') as f:
cookies = json.load(f)
self.session.cookies.update(cookies)
return True
except:
return False
return False
def save_cookies(self):
"""保存当前会话的 Cookie 到本地文件"""
with open(self.cookie_file, 'w') as f:
json.dump(self.session.cookies.get_dict(), f)
def set_music_u(self, music_u):
"""手动设置 MUSIC_U Cookie"""
self.session.cookies.set('MUSIC_U', music_u, domain='.music.163.com')
self.session.cookies.set('__csrf', '', domain='.music.163.com') # 补全 CSRF
self.save_cookies()
def check_login(self):
"""
验证登录状态
返回: (bool, profile_data)
"""
try:
# 接口 1: nuser/account/get
url = f"https://music.163.com/api/nuser/account/get?timestamp={int(time.time()*1000)}"
resp = self.session.post(url)
data = resp.json()
if data['code'] == 200 and data.get('profile'):
self.user_profile = data['profile']
return True, self.user_profile
# 接口 2: login/status (备用)
url2 = f"https://music.163.com/api/login/status?timestamp={int(time.time()*1000)}"
resp = self.session.get(url2)
data = resp.json()
if data['code'] == 200 and data.get('profile'):
self.user_profile = data['profile']
return True, self.user_profile
except Exception as e:
print(f"Check login error: {e}")
self.user_profile = None
return False, None
def get_cloud_list(self):
"""获取云盘全量歌曲列表 (自动处理分页)"""
all_songs = []
offset = 0
limit = 200
try:
while True:
url = f"https://music.163.com/api/v1/cloud/get?limit={limit}&offset={offset}"
resp = self.session.post(url)
data = resp.json()
if data['code'] == 200:
songs = data.get('data', [])
all_songs.extend(songs)
# 判断是否还有更多内容
if data.get('hasMore') == True and len(songs) > 0:
offset += limit
else:
break
else:
break
return all_songs
except Exception as e:
print(f"Get cloud list error: {e}")
return all_songs
def match_song(self, cloud_song_id, target_song_id):
"""
执行匹配纠正
返回: (bool, message)
"""
if not self.user_profile:
return False, "请先登录"
user_id = self.user_profile.get('userId')
url = f"https://music.163.com/api/cloud/user/song/match?userId={user_id}&songId={cloud_song_id}&adjustSongId={target_song_id}"
try:
resp = self.session.post(url)
res_text = resp.text
if '"code":200' in res_text:
return True, "匹配纠正成功!"
else:
return False, f"服务器响应: {res_text}"
except Exception as e:
return False, f"请求异常: {e}"