forked from atlas-comstock/NeteaseCloudMusicFlac
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython3_main.py
More file actions
79 lines (69 loc) · 2.54 KB
/
python3_main.py
File metadata and controls
79 lines (69 loc) · 2.54 KB
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
73
74
75
76
77
78
79
import re
import requests
import json
import urllib.request
import urllib.error
import os
import sys
minimumsize = 10
print("fetching msg from %s \n" % sys.argv[1])
url = re.sub("#/", "", 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)
CURRENT_PATH = os.path.dirname(os.path.realpath(__file__))
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 d is not None and 'data' not in d:
continue
songid = d["data"]["song"][0]["songid"]
print("find songid: %s" % 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) or d['data'] == '':
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 = ("%s/%s/%s-%s.flac" %
(CURRENT_PATH, songdir, songname, artistName))
f = urllib.request.urlopen(songlink)
headers = requests.head(songlink).headers
size = round(int(headers['Content-Length']) / (1024 ** 2), 2)
#Download unfinished Flacs again.
if not os.path.isfile(filename) or os.path.getsize(filename) < minimumsize: #Delete useless flacs
print("%s is downloading now ......\n\n" % songname)
if size >= minimumsize:
with open(filename, "wb") as code:
code.write(f.read())
else:
print("the size of %s (%r Mb) is less than 10 Mb, skipping" %
(filename, size))
else:
print("%s is already downloaded. Finding next song...\n\n" % songname)
print("\n================================================================\n")
print("Download finish!\nSongs' directory is %s/songs_dir" % os.getcwd())