forked from handoing/get-163-music
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetMusic.py
72 lines (61 loc) · 2.16 KB
/
getMusic.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
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/python
#coding:utf-8
import re
import requests
import json
import urllib2
import os
import sys
minimumsize = 1
print "ID: " + sys.argv[1] + "\n";
url = "http://music.163.com/playlist?id=" + 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)
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 "Song Name: " + value
r = requests.get(url, params=payload)
contents = r.text
d = json.loads(contents, encoding="utf-8")
if('data' not in d):
print "do not have flac\n"
continue
if('song' not in d["data"]):
print "do not have flac\n"
continue
songid = d["data"]["song"][0]["songid"]
print "Song ID: " + songid
url = "http://music.baidu.com/data/music/fmlink"
payload = {'songIds': songid, 'type': 'mp3'}
r = requests.get(url, params=payload)
contents = r.text
d = json.loads(contents, encoding="utf-8")
if d is not None and 'data' not in d or d['data'] == '':
continue
songlink = d["data"]["songList"][0]["songLink"]
if(len(songlink) < 10):
print "do not have flac\n"
continue
print "Song Source: " + songlink + "\n";
songdir = "songs"
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"
f = urllib2.urlopen(songlink)
headers = requests.head(songlink).headers
size = int(headers['Content-Length']) / (1024 ** 2)
if not os.path.isfile(filename) or os.path.getsize(filename) < minimumsize:
print "%s is downloading now ......\n" % songname
with open(filename, "wb") as code:
code.write(f.read())
else:
print "%s is already downloaded. Finding next song...\n\n" % songname
print "\n================================================================\n"
print "Download finish!"