-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmigu.py
61 lines (61 loc) · 2.59 KB
/
migu.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
import requests
import os
class Migu():
def __init__(self, key):
self.key = key
self.names = []
self.singers = []
self.albums = []
self.download_urls = []
self.exts = []
def get_urls(self):
search_url = 'http://pd.musicapp.migu.cn/MIGUM3.0/v1.0/content/search_all.do'
player_url = 'https://app.pd.nf.migu.cn/MIGUM3.0/v1.0/content/sub/listenSong.do?channel=mx©rightId={copyrightId}&contentId={contentId}&toneFlag={toneFlag}&resourceType={resourceType}&userId=15548614588710179085069&netType=00'
params = {
'ua': 'Android_migu',
'version': '5.0.1',
'text': self.key,
'pageNo': '1',
'pageSize': '31',
'searchSwitch': '{"song":1,"album":0,"singer":0,"tagSong":0,"mvSong":0,"songlist":0,"bestShow":1}',
}
headers = {
'Referer': 'https://m.music.migu.cn/',
'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 Edg/84.0.522.44'
}
s = requests.Session()
response = s.get(search_url, headers=headers, params=params)
items = response.json()['songResultData']['result']
for item in items:
for rate in sorted(item.get('rateFormats', []), key=lambda x: int(x['size']), reverse=True):
ext = '.flac' if rate.get('formatType') == 'SQ' else '.mp3'
self.exts.append(ext)
self.download_urls.append(player_url.format(copyrightId=item['copyrightId'], contentId=item['contentId'], toneFlag=rate['formatType'], resourceType=rate['resourceType']))
break
self.names.append(item['name'])
t = ''
k = ''
for i in item['singers']:
t += i['name']
t = t + '/'
self.singers.append(t)
try:
for j in item['albums']:
k += j['name']
k = k + '/'
self.albums.append(k)
except:
self.albums.append('')
def open_url(img_url):
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 Edg/84.0.522.44'
}
response = requests.get(img_url, headers=headers)
if response.status_code == 200:
return response.content
return None
def download(key, url, folder, ext):
os.chdir(folder)
with open(key + ext, 'wb') as f:
data = open_url(url)
f.write(data)