-
Notifications
You must be signed in to change notification settings - Fork 0
/
NetMusicMVDownloader.py
53 lines (49 loc) · 1.78 KB
/
NetMusicMVDownloader.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
#coding=utf-8
import requests
import os
import re
class NetMV:
def __init__(self, url):
self.url = url
self.mv_id = []
self.mv_urls = []
def generate(self):
try:
id = re.findall(r'id=(.*)',self.url)[0]
except Exception as e:
print('歌单地址有错误')
j = requests.get('https://api.imjad.cn/cloudmusic/?type=playlist&id=%s' % id).json()
ll = len(j['privileges'])
# 获取MV ID
for i in range(0,ll):
if j['playlist']["tracks"][i]['mv'] != 0:
self.mv_id.append(j['playlist']["tracks"][i]['mv'])
# 获取MV的URL
for id in self.mv_id:
self.mv_urls.append('https://api.imjad.cn/cloudmusic/?type=mv&id=%s' % id)
def download(self):
pwd = os.path.abspath('.')
directory = pwd +'/'+'MV'
if not os.path.exists(directory):
os.makedirs(directory)
for mv_url in self.mv_urls:
name = requests.get(mv_url).json()['data']['name']
if os.path.exists(directory+'/'+'%s.mkv' % name) == True:
print('已经下载过这首歌曲了,跳过......')
else:
try:
url = requests.get(mv_url).json()['data']['brs']['1080']
r = requests.get(url)
print('开始下载%s到当前的文件夹' % name)
with open(directory+'/'+'%s.mkv' % name, 'wb') as f:
f.write(r.content)
except Exception as e:
print('1080')
print('下载结束,看看去吧')
def main():
print('输入歌单所在的网址')
nv = NetMV(url = input())
nv.generate()
nv.download()
if __name__ == '__main__':
main()