|
1 | 1 | import aiohttp
|
2 |
| -import time |
3 | 2 | import re
|
4 | 3 | from .util import *
|
5 | 4 | from .zsync import ZingMp3
|
6 | 5 | from .sobj import *
|
7 | 6 |
|
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 |
| - |
47 | 7 | class Stream(Stream):
|
48 | 8 | async def download(self, *args, **kwargs):
|
49 | 9 | if not self.isVIP:
|
@@ -78,31 +38,75 @@ def songs(self):
|
78 | 38 | return [Song(song, self.client) for song in self.indata['song']["items"]]
|
79 | 39 |
|
80 | 40 | 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 | + |
81 | 85 | 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}) |
83 | 87 | return Playlist(data["data"], client=self)
|
84 | 88 |
|
85 | 89 | 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) |
87 | 91 | return Artist(data["data"])
|
88 | 92 |
|
89 | 93 | 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}) |
91 | 95 | return LiveRadio(data["data"])
|
92 | 96 |
|
93 | 97 | 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}) |
95 | 99 | return Song(data["data"], client=self)
|
96 | 100 |
|
97 | 101 | 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}) |
99 | 103 | return [Stream(i, c) for i, c in data["data"].items()]
|
100 | 104 |
|
101 | 105 | 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) |
103 | 107 | dat = data["data"]
|
104 | 108 | return [Playlist(j, client=self) for i in dat for j in i["items"]]
|
105 | 109 |
|
106 | 110 | 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) |
108 | 112 | return Search(data["data"], client=self)
|
0 commit comments