-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMusic_download.py
56 lines (42 loc) · 1.73 KB
/
Music_download.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
import requests
import re
import json
def get_music_resource(songid):
search_url = 'http://musicapi.taihe.com/v1/restserver/ting?method=baidu.ting.song.playAAC&format=jsonp&callback=' \
'jQuery172047648654448286276_1545221906467&songid={}'.format(songid)
response = requests.get(search_url).text
res = re.findall(r'\((.*)\)', response)[0]
res_json = json.loads(res)
return res_json
def get_music_info(jsondata):
songinfo = jsondata['songinfo']
music_title = songinfo['title']
print('歌名: ' + music_title)
music_compose = songinfo['compose']
print('作者: ' + music_compose)
album_title = songinfo['album_title']
print('专辑: ' + album_title)
avatar = songinfo['artist_list'][0]['avatar_s300'] if songinfo['artist_list'][0]['avatar_s300'] else ''
print('头像: ' + avatar)
music_language = songinfo['language'] if songinfo['language'] else ''
print('语种: ' + music_language)
music_country = songinfo['country'] if songinfo['country'] else ''
print('国家: ' + music_country)
music_url = jsondata['bitrate']['file_link']
print(music_url)
return music_title, music_url
def music_download(filename, url):
with open(filename + '.mp3', 'wb') as f:
f.write(requests.get(url).content)
if __name__ == '__main__':
songid = input('请输入歌曲的id: ')
data = get_music_resource(songid)
music_title, music_url = get_music_info(data)
is_download = input('是否下载(y/n): ')
if is_download.lower() == 'y':
music_download(music_title, music_url)
print('下载完成,真刺激!!!!')
elif is_download.lower() == 'n':
print('Just for fun...')
else:
print('emmm ,exiting...')