-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathZingMp3.py
111 lines (89 loc) · 3.3 KB
/
ZingMp3.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
import hashlib
import hmac
import urllib
import requests
import datetime
class ZingMp3:
api_key = "kI44ARvPwaqL7v0KuDSM0rGORtdY1nnw"
secret_key = "882QcNXV4tUZbvAsjmFOHqNC1LpcBRKW"
url_api = "https://zingmp3.vn"
cookies = None
@classmethod
def getRecommendSongs(cls, id):
return cls.requestZing("/api/v2/recommend/get/songs", {"id": id})
@classmethod
def getDetailPlaylist(cls, id):
return cls.requestZing("/api/v2/page/get/playlist", {"id": id})
@classmethod
def getDetailArtist(cls, alias):
return cls.requestZing("/api/v2/page/get/artist", {"alias": alias})
@classmethod
def getInfoMusic(cls, id):
return cls.requestZing("/api/v2/song/get/info", {"id": id})
@classmethod
def getStreaming(cls, id):
return cls.requestZing("/api/v2/song/get/streaming", {"id": id})
@classmethod
def getFullInfo(cls, id):
data = cls.requestZing("/api/v2/song/get/info", {"id": id})
data["streaming"] = cls.requestZing("/api/v2/song/get/streaming", {"id": id})
return data
@classmethod
def getHome(cls, page=1):
return cls.requestZing("/api/v2/page/get/home", {"page": page})
@classmethod
def getChartHome(cls, page=1):
return cls.requestZing("/api/v2/page/get/chart-home", {"page": page})
@classmethod
def getWeekChart(cls, id):
return cls.requestZing("/api/v2/page/get/week-chart", {"id": id})
@classmethod
def getTop100(cls, page=1):
return cls.requestZing("/api/v2/page/get/top-100", {"page": page})
@classmethod
def getNewReleaseChart(cls, page=1):
return cls.requestZing("/api/v2/page/get/newrelease-chart", {"page": page})
@classmethod
def search(cls, keyword, stype="multi"):
if stype == "multi":
return cls.requestZing("/api/v2/search/multi", {"q": keyword})
else:
return cls.requestZing("/api/v2/search", {"q": keyword, "type": stype})
@classmethod
def getCookie(cls):
if not cls.cookies:
r = requests.get(cls.url_api)
cls.cookies = dict(r.cookies)
return cls.cookies
@classmethod
def hashParam(cls, path, param=""):
hash256 = hashlib.sha256(param.encode("utf8")).hexdigest()
sig = hmac.new(
cls.secret_key.encode("utf8"),
msg=f"{path}{hash256}".encode("utf8"),
digestmod=hashlib.sha512
).hexdigest()
return sig
@classmethod
def requestZing(cls, path, qs):
cls.getCookie()
ctime = int(datetime.datetime.now().timestamp())
param = {
"ctime": ctime
}
for k, v in qs.items():
if k not in ["q", "alias"]:
param[k] = v
param = urllib.parse.urlencode(param).replace("&", "")
sig = cls.hashParam(path, param)
params = {
"ctime": ctime,
"sig": sig,
"apiKey": cls.api_key
}
params.update(qs)
r = requests.get(cls.url_api + path, params=params, cookies=cls.cookies)
r = r.json()
if r["err"] != 0:
raise Exception(r["msg"])
return r["data"]