-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxiami.py
94 lines (85 loc) · 2.56 KB
/
xiami.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env python
#-*- coding:utf-8 -*-
import os
import math
import urllib
import xml.etree.ElementTree as etree
import sys
import common
_trackUrl = 'http://www.xiami.com/song/playlist/id/%s'
_albumUrl = 'http://www.xiami.com/song/playlist/id/%s/type/1'
_collectUrl = 'http://www.xiami.com/song/playlist/id/%s/type/3'
TARGET = common.os.path.join(os.path.expanduser('~'), 'Music','xiami')
if common.os.sys.platform.find('win') != -1:
TARGET = 'E:\\Music\\xiami'
def parse_url(loc):
'''Parse the uri of the mp3'''
line = int(loc[0])
length = len(loc) - 1
numz = int(math.ceil(length*1.0/line))
numd = int(math.floor(length*1.0/line))
mod = length%line
npl = [0] + [numz]*mod + [numd]*(line-mod-1)
uri = ""
for i in range(1,numz+1):
cnt = i
if i != numz:
for n in npl:
cnt += n
uri += loc[cnt]
else:
if mod == 0: mod=line
for j in range(0,mod):
cnt += npl[j]
uri += loc[cnt]
uri = urllib.unquote(uri).replace('^','0')
return uri
def extract(html):
''' extract the xml'''
if not html:
return ''
xns = '{http://xspf.org/ns/0/}%s'
playlist = etree.fromstring(html)
tracklist = playlist.find(xns % 'trackList')
res = []
for track in tracklist:
title = track.find(xns % 'title')
if title is None:
break
title = title.text.strip()
uri = parse_url(track.find(xns % 'location').text.strip())
lrc = track.find(xns % 'lyric').text.strip()
if not lrc.endswith('lrc'):
lrc = ''
res.append((title,uri,lrc))
return res
def main():
if len(sys.argv) < 3 or (sys.argv[1] != '-t' and len(sys.argv) > 3):
help_info()
return
if sys.argv[1] == '-a':
url = _albumUrl % sys.argv[2]
elif sys.argv[1] == '-c':
url = _collectUrl % sys.argv[2]
elif sys.argv[1] == '-t':
url = _trackUrl % ','.join(sys.argv[2:])
else :
help_info()
return
content = common.open_url(url)
if not content:
return
res = extract(content)
for title,uri,lrc in res:
common.download(uri,TARGET,title,'mp3')
if lrc:
#download the lyric
def help_info():
print '.................USAGE....................'
print '>xiami -a album_id'
print '>xiami -c collection_id'
print '>xiami -t track_id1 track_id2 track_id3...'
print '>xiami -h'
print '..........................................'
if __name__ == '__main__':
main()