This repository has been archived by the owner on Nov 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
ck_music163.py
208 lines (193 loc) · 7.98 KB
/
ck_music163.py
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# -*- coding: utf-8 -*-
"""
cron: 27 0 * * *
new Env('网易云音乐');
"""
import base64
import hashlib
import json
import random
import requests
import urllib3
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from notify_mtr import send
from utils import get_data
urllib3.disable_warnings()
class Music163:
def __init__(self, check_items):
self.check_items = check_items
self.headers = {
"User-Agent":
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36",
"Referer": "http://music.163.com/",
"Accept-Encoding": "gzip, deflate"
}
@staticmethod
def _encrypt(key, text):
backend = default_backend()
cipher = Cipher(algorithms.AES(key.encode("utf8")),
modes.CBC(b"0102030405060708"),
backend=backend)
encryptor = cipher.encryptor()
length = 16
count = len(text.encode("utf-8"))
if count % length != 0:
add = length - (count % length)
else:
add = 16
pad = chr(add)
text1 = text + (pad * add)
ciphertext = encryptor.update(
text1.encode("utf-8")) + encryptor.finalize()
crypted_str = str(base64.b64encode(ciphertext), encoding="utf-8")
return crypted_str
def encrypt(self, text):
return {
"params": self._encrypt("TA3YiYCfY2dDJQgg", self._encrypt("0CoJUm6Qyw8W8jud", text)),
"encSecKey":
"84ca47bca10bad09a6b04c5c927ef077d9b9f1e37098aa3eac6ea70eb59df0aa28b691b7e75e4f1f9831754919ea784c8f74fbfadf2898b0be17849fd656060162857830e241aba44991601f137624094c114ea8d17bce815b0cd4e5b8e2fbaba978c6d1d14dc3d1faf852bdd28818031ccdaaa13a6018e1024e2aae98844210"
}
def login(self, session, phone, password):
login_url = "https://music.163.com/weapi/login/cellphone"
headers = {
"User-Agent":
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36",
"Referer": "http://music.163.com/",
"Accept-Encoding": "gzip, deflate",
"Cookie":
"os=pc; osver=Microsoft-Windows-10-Professional-build-10586-64bit; appver=2.0.3.131777; channel=netease; __remember_me=true;"
}
hl = hashlib.md5()
hl.update(password.encode(encoding="utf-8"))
md5_password = str(hl.hexdigest())
login_data = self.encrypt(
json.dumps({
"phone": phone,
"countrycode": "86",
"password": md5_password,
"rememberLogin": "true"
})
)
res = session.post(url=login_url,
data=login_data,
headers=headers,
verify=False)
ret = res.json()
if ret["code"] == 200:
csrf = requests.utils.dict_from_cookiejar(res.cookies)["__csrf"]
nickname = ret["profile"]["nickname"]
level_data = self.get_level(session=session,
csrf=csrf,
login_data=login_data)
level = level_data["level"]
now_play_count = level_data["nowPlayCount"]
next_play_count = level_data["nextPlayCount"]
now_login_count = level_data["nowLoginCount"]
next_login_count = level_data["nextLoginCount"]
return csrf, nickname, level, now_play_count, next_play_count, now_login_count, next_login_count
else:
return False, ret.get("message"), 0, 0, 0
def sign(self, session):
sign_url = "https://music.163.com/weapi/point/dailyTask"
res = session.post(url=sign_url,
data=self.encrypt('{"type":0}'),
headers=self.headers,
verify=False)
ret = res.json()
if ret["code"] == 200:
return "签到成功,经验+ " + str(ret["point"])
elif ret["code"] == -2:
return "今天已经签到过了"
else:
return "签到失败: " + ret["message"]
def task(self, session, csrf):
url = "https://music.163.com/weapi/v6/playlist/detail?csrf_token=" + csrf
recommend_url = "https://music.163.com/weapi/v1/discovery/recommend/resource"
music_lists = []
res = session.post(url=recommend_url,
data=self.encrypt('{"csrf_token":"' + csrf + '"}'),
headers=self.headers,
verify=False)
ret = res.json()
if ret["code"] != 200:
print("获取推荐歌曲失败: ", str(ret["code"]), ":", ret["message"])
else:
lists = ret["recommend"]
music_lists = [(d["id"]) for d in lists]
music_id = []
for m in music_lists:
res = session.post(
url=url,
data=self.encrypt(
json.dumps({
"id": m,
"n": 1000,
"csrf_token": csrf
})
),
headers=self.headers,
verify=False,
)
ret = json.loads(res.text)
for i in ret["playlist"]["trackIds"]:
music_id.append(i["id"])
post_data = json.dumps({
"logs":
json.dumps(
list(map(
lambda x: {
"action": "play",
"json": {
"download": 0,
"end": "playend",
"id": x,
"sourceId": "",
"time": 240,
"type": "song",
"wifi": 0
}
},
random.sample(
music_id,
420 if len(music_id) > 420 else len(music_id)),
)))
})
res = session.post(url="http://music.163.com/weapi/feedback/weblog",
data=self.encrypt(post_data))
ret = res.json()
if ret["code"] == 200:
return "刷听歌量成功"
else:
return "刷听歌量失败: " + ret["message"]
def get_level(self, session, csrf, login_data):
url = "https://music.163.com/weapi/user/level?csrf_token=" + csrf
res = session.post(url=url, data=login_data, headers=self.headers)
ret = json.loads(res.text)
return ret["data"]
def main(self):
msg_all = ""
for check_item in self.check_items:
phone = check_item.get("phone")
password = check_item.get("password")
session = requests.session()
csrf, nickname, level, now_play_count, next_play_count, now_login_count, next_login_count = self.login(
session=session, phone=phone, password=password)
res_sign = ""
res_task = ""
if csrf:
res_sign = self.sign(session=session)
res_task = self.task(session=session, csrf=csrf)
msg = (
f"帐号信息: {nickname}\n当前等级: {level}\n当前听歌数量: {now_play_count}\n"
f"升级需听歌数量: {next_play_count - now_play_count}\n升级需签到天数: {next_login_count - now_login_count}\n"
f"签到状态: {res_sign}\n刷歌状态: {res_task}"
)
msg_all += msg + "\n\n"
return msg_all
if __name__ == "__main__":
data = get_data()
_check_items = data.get("MUSIC163", [])
res = Music163(check_items=_check_items).main()
print(res)
send("网易云音乐", res)