Skip to content
This repository was archived by the owner on Sep 12, 2024. It is now read-only.

Commit 5319d50

Browse files
committed
fix code base
1 parent 0e84c59 commit 5319d50

File tree

5 files changed

+103
-99
lines changed

5 files changed

+103
-99
lines changed

README.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ async def main():
4444
await zi.getTop100()
4545
await zi.search("rick roll")
4646

47-
loop = asyncio.get_event_loop()
48-
loop.run_until_complete(main())
47+
asyncio.run(main())
4948
```
5049

5150
## Get Type And ID

test/zasync_test.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,4 @@ async def main():
1010
await zi.getTop100()
1111
await zi.search("rick roll")
1212

13-
loop = asyncio.get_event_loop()
14-
loop.run_until_complete(main())
13+
asyncio.run(main())

test/zsync_test.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
getUrlTypeAndID("https://zingmp3.vn/liveradio/IWZ979CW.html")
44
zi = ZingMp3()
5-
print(zi.getDetailPlaylist("ZB08FIBW").songs)
5+
zi.getDetailPlaylist("ZB08FIBW").songs
66
zi.getDetailArtist("Cammie")
7-
zi.getRadioInfo("IWZ979CW")
7+
zi.getRadioInfo("IWZ979UB")
88
zi.getSongInfo("ZWAF6UFD")
99
zi.getSongStreaming("ZWAF6UFD")
1010
zi.getTop100()

zingmp3py/zasync.py

+51-47
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,9 @@
11
import aiohttp
2-
import time
32
import re
43
from .util import *
54
from .zsync import ZingMp3
65
from .sobj import *
76

8-
cooke = {"cookies": {}, "last_updated": 0}
9-
apikey = {}
10-
async def get_ck(request: aiohttp.ClientSession):
11-
if int(cooke["last_updated"] + 60) < int(time.time()):
12-
async with request.get("https://zingmp3.vn") as r:
13-
cooke["cookies"] = r.cookies
14-
cooke["last_updated"] = int(time.time())
15-
return cooke["cookies"]
16-
else:
17-
return cooke["cookies"]
18-
19-
async def get_key():
20-
if not apikey:
21-
async with aiohttp.ClientSession() as s:
22-
async with s.get("https://zjs.zmdcdn.me/zmp3-desktop/releases/v1.7.34/static/js/main.min.js") as r:
23-
data = await r.text()
24-
key = re.findall(r',h="(.*?)",p=\["ctime","id","type","page","count","version"\]', data)[0]
25-
skey = re.findall(r"return d\(\)\(t\+r,\"(.*?)\"\)", data)[0]
26-
apikey.update({"data": [key, skey]})
27-
return [key, skey]
28-
else:
29-
return apikey["data"]
30-
31-
async def requestZing(path, qs={}, haveParam=0):
32-
apikey, skey = await get_key()
33-
param = "&".join([f"{i}={k}" for i,k in qs.items()])
34-
sig = hashParam(skey, path, param, haveParam)
35-
qs.update({"apiKey": apikey})
36-
qs.update({"ctime": sig[1]})
37-
qs.update({"sig": sig[0]})
38-
url = "https://zingmp3.vn" + path
39-
async with aiohttp.ClientSession() as s:
40-
ck = await get_ck(s)
41-
async with s.get(url, params=qs, cookies=ck) as r:
42-
data = await r.json()
43-
if data['err'] != 0:
44-
raise ZingMp3Error(data)
45-
return data
46-
477
class Stream(Stream):
488
async def download(self, *args, **kwargs):
499
if not self.isVIP:
@@ -78,31 +38,75 @@ def songs(self):
7838
return [Song(song, self.client) for song in self.indata['song']["items"]]
7939

8040
class ZingMp3Async(ZingMp3):
41+
42+
async def get_ck(self, request: aiohttp.ClientSession):
43+
if int(self.cooke["last_updated"] + 60) < int(time.time()):
44+
async with request.get("https://zingmp3.vn") as r:
45+
self.cooke["cookies"] = r.cookies
46+
self.cooke["last_updated"] = int(time.time())
47+
return self.cooke["cookies"]
48+
else:
49+
return self.cooke["cookies"]
50+
51+
async def get_key(self):
52+
if not self.apikey:
53+
async with aiohttp.ClientSession() as s:
54+
async with s.get("https://zingmp3.vn/") as r:
55+
data = await r.text()
56+
outs = re.findall(r"<script type=\"text/javascript\" src=\"(https://zjs.zmdcdn.me/zmp3-desktop/releases/.*?/static/js/main\.min\.js)\"></script>", data)
57+
async with s.get(outs[0]) as r:
58+
data = await r.text()
59+
outs = re.findall(r"\"([a-zA-Z0-9]{32})\"", data)
60+
key = outs[0]
61+
skey = outs[1]
62+
self.apikey.update({"data": [key, skey]})
63+
return [key, skey]
64+
else:
65+
return self.apikey["data"]
66+
67+
async def requestZing(self, path, qs=None, haveParam=0):
68+
if qs is None:
69+
qs = {}
70+
apikey, skey = await self.get_key()
71+
param = "&".join([f"{i}={k}" for i, k in qs.items()])
72+
sig = hashParam(skey, path, param, haveParam)
73+
qs.update({"apiKey": apikey})
74+
qs.update({"ctime": sig[1]})
75+
qs.update({"sig": sig[0]})
76+
url = "https://zingmp3.vn" + path
77+
async with aiohttp.ClientSession() as s:
78+
ck = await self.get_ck(s)
79+
async with s.get(url, params=qs, cookies=ck) as r:
80+
data = await r.json()
81+
if data['err'] != 0:
82+
raise ZingMp3Error(data)
83+
return data
84+
8185
async def getDetailPlaylist(self, id):
82-
data = await requestZing("/api/v2/page/get/playlist", {"id": id})
86+
data = await self.requestZing("/api/v2/page/get/playlist", {"id": id})
8387
return Playlist(data["data"], client=self)
8488

8589
async def getDetailArtist(self, alias):
86-
data = await requestZing("/api/v2/page/get/artist", {"alias": alias}, 1)
90+
data = await self.requestZing("/api/v2/page/get/artist", {"alias": alias}, 1)
8791
return Artist(data["data"])
8892

8993
async def getRadioInfo(self, id):
90-
data = await requestZing("/api/v2/livestream/get/info", {"id": id})
94+
data = await self.requestZing("/api/v2/livestream/get/info", {"id": id})
9195
return LiveRadio(data["data"])
9296

9397
async def getSongInfo(self, id):
94-
data = await requestZing("/api/v2/song/get/info", {"id": id})
98+
data = await self.requestZing("/api/v2/song/get/info", {"id": id})
9599
return Song(data["data"], client=self)
96100

97101
async def getSongStreaming(self, id):
98-
data = await requestZing("/api/v2/song/get/streaming", {"id": id})
102+
data = await self.requestZing("/api/v2/song/get/streaming", {"id": id})
99103
return [Stream(i, c) for i, c in data["data"].items()]
100104

101105
async def getTop100(self):
102-
data = await requestZing("/api/v2/page/get/top-100", haveParam=1)
106+
data = await self.requestZing("/api/v2/page/get/top-100", haveParam=1)
103107
dat = data["data"]
104108
return [Playlist(j, client=self) for i in dat for j in i["items"]]
105109

106110
async def search(self, search):
107-
data = await requestZing("/api/v2/search/multi", {"q": search}, 1)
111+
data = await self.requestZing("/api/v2/search/multi", {"q": search}, 1)
108112
return Search(data["data"], client=self)

zingmp3py/zsync.py

+48-46
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,80 @@
1-
import requests
21
import re
32
from .util import *
43
from .sobj import *
54

6-
cooke = {"cookies": {}, "last_updated": 0}
7-
apikey = {}
85

9-
def get_ck(request: requests.Session):
10-
if int(cooke["last_updated"] - 60) < int(time.time()):
11-
with request.get("https://zingmp3.vn") as r:
12-
cooke["cookies"] = r.cookies
13-
cooke["last_updated"] = int(time.time())
14-
return cooke["cookies"]
15-
else:
16-
return cooke["cookies"]
6+
class ZingMp3:
7+
def __init__(self):
8+
self.apikey = {}
9+
self.cooke = {"cookies": {}, "last_updated": 0}
1710

18-
def get_key():
19-
if not apikey:
20-
with requests.Session() as s:
21-
with s.get("https://zjs.zmdcdn.me/zmp3-desktop/releases/v1.7.34/static/js/main.min.js") as r:
22-
data = r.text
23-
key = re.findall(r',h="(.*?)",p=\["ctime","id","type","page","count","version"\]', data)[0]
24-
skey = re.findall(r"return d\(\)\(t\+r,\"(.*?)\"\)", data)[0]
25-
apikey.update({"data": [key, skey]})
26-
return [key, skey]
27-
else:
28-
return apikey["data"]
11+
def get_ck(self, request: requests.Session):
12+
if int(self.cooke["last_updated"] - 60) < int(time.time()):
13+
with request.get("https://zingmp3.vn") as r:
14+
self.cooke["cookies"] = r.cookies
15+
self.cooke["last_updated"] = int(time.time())
16+
return self.cooke["cookies"]
17+
else:
18+
return self.cooke["cookies"]
2919

30-
def requestZing(path, qs={}, haveParam=0):
31-
apikey, skey = get_key()
32-
param = "&".join([f"{i}={k}" for i,k in qs.items()])
33-
sig = hashParam(skey, path, param, haveParam)
34-
qs.update({"apiKey": apikey})
35-
qs.update({"ctime": sig[1]})
36-
qs.update({"sig": sig[0]})
37-
url = "https://zingmp3.vn" + path
38-
with requests.Session() as s:
39-
with s.get(url, params=qs, cookies=get_ck(s)) as r:
40-
data = r.json()
41-
if data['err'] != 0:
42-
raise ZingMp3Error(data)
43-
return data
20+
def get_key(self):
21+
if not self.apikey:
22+
with requests.Session() as s:
23+
with s.get("https://zingmp3.vn/") as r:
24+
data = r.text
25+
outs = re.findall(r"<script type=\"text/javascript\" src=\"(https://zjs.zmdcdn.me/zmp3-desktop/releases/.*?/static/js/main\.min\.js)\"></script>", data)
26+
with s.get(outs[0]) as r:
27+
data = r.text
28+
outs = re.findall(r"\"([a-zA-Z0-9]{32})\"", data)
29+
key = outs[0]
30+
skey = outs[1]
31+
self.apikey.update({"data": [key, skey]})
32+
return [key, skey]
33+
else:
34+
return self.apikey["data"]
4435

45-
class ZingMp3:
46-
def __init__(self):
47-
pass
36+
def requestZing(self, path, qs={}, haveParam=0):
37+
apikey, skey = self.get_key()
38+
param = "&".join([f"{i}={k}" for i, k in qs.items()])
39+
sig = hashParam(skey, path, param, haveParam)
40+
qs.update({"apiKey": apikey})
41+
qs.update({"ctime": sig[1]})
42+
qs.update({"sig": sig[0]})
43+
url = "https://zingmp3.vn" + path
44+
with requests.Session() as s:
45+
with s.get(url, params=qs, cookies=self.get_ck(s)) as r:
46+
data = r.json()
47+
if data['err'] != 0:
48+
raise ZingMp3Error(data)
49+
return data
4850

4951
def getDetailPlaylist(self, id):
50-
data = requestZing("/api/v2/page/get/playlist", {"id": id})
52+
data = self.requestZing("/api/v2/page/get/playlist", {"id": id})
5153
return Playlist(data["data"], client=self)
5254

5355
def getDetailArtist(self, alias):
54-
data = requestZing("/api/v2/page/get/artist", {"alias": alias}, 1)
56+
data = self.requestZing("/api/v2/page/get/artist", {"alias": alias}, 1)
5557
return Artist(data["data"])
5658

5759
def getRadioInfo(self, id):
58-
data = requestZing("/api/v2/livestream/get/info", {"id": id})
60+
data = self.requestZing("/api/v2/livestream/get/info", {"id": id})
5961
return LiveRadio(data["data"])
6062

6163
def getSongInfo(self, id):
62-
data = requestZing("/api/v2/song/get/info", {"id": id})
64+
data = self.requestZing("/api/v2/song/get/info", {"id": id})
6365
return Song(data["data"], client=self)
6466

6567
def getSongStreaming(self, id):
66-
data = requestZing("/api/v2/song/get/streaming", {"id": id})
68+
data = self.requestZing("/api/v2/song/get/streaming", {"id": id})
6769
return [Stream(i, c) for i, c in data["data"].items()]
6870

6971
def getTop100(self):
70-
data = requestZing("/api/v2/page/get/top-100", haveParam=1)
72+
data = self.requestZing("/api/v2/page/get/top-100", haveParam=1)
7173
dat = data["data"]
7274
return [Playlist(j, client=self) for i in dat for j in i["items"]]
7375

7476
def search(self, search):
75-
data = requestZing("/api/v2/search/multi", {"q": search}, 1)
77+
data = self.requestZing("/api/v2/search/multi", {"q": search}, 1)
7678
return Search(data["data"], client=self)
7779

7880

0 commit comments

Comments
 (0)