forked from atlas-comstock/NeteaseCloudMusicFlac
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
62 lines (54 loc) · 1.69 KB
/
main.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
62
import re
import requests
import json
import urllib2
import os
import sys
print "fetching msg from " + sys.argv[1] + "\n"
url = sys.argv[1]
r = requests.get(url)
contents = r.text
res = r'<ul class="f-hide">(.*?)</ul>'
mm = re.findall(res, contents, re.S|re.M)
if(mm):
contents = mm[0]
else:
print 'Can not fetch information form URL. Please make sure the URL is right.\n'
os._exit(0)
res = r'<li><a .*?>(.*?)</a></li>'
mm = re.findall(res, contents, re.S|re.M)
for value in mm:
url = 'http://sug.music.baidu.com/info/suggestion'
payload = {'word': value, 'version': '2', 'from': '0'}
print value
r = requests.get(url, params=payload)
contents = r.text
d = json.loads(contents, encoding="utf-8")
if('data' not in d):
continue
songid = d["data"]["song"][0]["songid"]
print "find songid: "
print songid
url = "http://music.baidu.com/data/music/fmlink"
payload = {'songIds': songid, 'type': 'flac'}
r = requests.get(url, params=payload)
contents = r.text
d = json.loads(contents, encoding="utf-8")
if('data' not in d):
continue
songlink = d["data"]["songList"][0]["songLink"]
print "find songlink: "
if(len(songlink) < 10):
print "\tdo not have flac\n"
continue
print songlink
songdir = "songs_dir"
if not os.path.exists(songdir):
os.makedirs(songdir)
songname = d["data"]["songList"][0]["songName"]
artistName = d["data"]["songList"][0]["artistName"]
filename = "./" + songdir + "/"+songname+"-"+artistName+".flac"
print filename + " is downloading now ......\n\n"
f = urllib2.urlopen(songlink)
with open(filename, "wb") as code:
code.write(f.read())