2012-12-22 20:46:22 +04:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
__all__ = ['xiami_download']
|
|
|
|
|
|
|
|
from ..common import *
|
|
|
|
|
|
|
|
from xml.dom.minidom import parseString
|
|
|
|
from urllib import parse
|
|
|
|
|
|
|
|
def location_dec(str):
|
|
|
|
head = int(str[0])
|
|
|
|
str = str[1:]
|
|
|
|
rows = head
|
|
|
|
cols = int(len(str)/rows) + 1
|
2017-02-02 07:43:57 +03:00
|
|
|
|
2012-12-22 20:46:22 +04:00
|
|
|
out = ""
|
|
|
|
full_row = len(str) % head
|
|
|
|
for c in range(cols):
|
|
|
|
for r in range(rows):
|
|
|
|
if c == (cols - 1) and r >= full_row:
|
|
|
|
continue
|
|
|
|
if r < full_row:
|
|
|
|
char = str[r*cols+c]
|
|
|
|
else:
|
|
|
|
char = str[cols*full_row+(r-full_row)*(cols-1)+c]
|
|
|
|
out += char
|
|
|
|
return parse.unquote(out).replace("^", "0")
|
|
|
|
|
2013-03-26 16:53:39 +04:00
|
|
|
def xiami_download_lyric(lrc_url, file_name, output_dir):
|
2017-08-08 10:00:51 +03:00
|
|
|
lrc = get_content(lrc_url, headers=fake_headers)
|
2014-04-11 15:51:07 +04:00
|
|
|
filename = get_filename(file_name)
|
2013-03-26 16:53:39 +04:00
|
|
|
if len(lrc) > 0:
|
2014-04-11 15:51:07 +04:00
|
|
|
with open(output_dir + "/" + filename + '.lrc', 'w', encoding='utf-8') as x:
|
2013-03-26 16:53:39 +04:00
|
|
|
x.write(lrc)
|
|
|
|
|
2013-04-29 05:53:13 +04:00
|
|
|
def xiami_download_pic(pic_url, file_name, output_dir):
|
2017-08-08 10:00:51 +03:00
|
|
|
from ..util.strings import get_filename
|
2013-04-29 05:53:13 +04:00
|
|
|
pic_url = pic_url.replace('_1', '')
|
|
|
|
pos = pic_url.rfind('.')
|
|
|
|
ext = pic_url[pos:]
|
2017-08-08 10:00:51 +03:00
|
|
|
pic = get_content(pic_url, headers=fake_headers, decoded=False)
|
2013-04-29 05:53:13 +04:00
|
|
|
if len(pic) > 0:
|
|
|
|
with open(output_dir + "/" + file_name.replace('/', '-') + ext, 'wb') as x:
|
|
|
|
x.write(pic)
|
|
|
|
|
2017-08-08 10:00:51 +03:00
|
|
|
def xiami_download_song(sid, output_dir = '.', info_only = False):
|
|
|
|
xml = get_content('http://www.xiami.com/song/playlist/id/%s/object_name/default/object_id/0' % sid, headers=fake_headers)
|
2012-12-22 20:46:22 +04:00
|
|
|
doc = parseString(xml)
|
|
|
|
i = doc.getElementsByTagName("track")[0]
|
|
|
|
artist = i.getElementsByTagName("artist")[0].firstChild.nodeValue
|
|
|
|
album_name = i.getElementsByTagName("album_name")[0].firstChild.nodeValue
|
2017-03-11 16:59:45 +03:00
|
|
|
song_title = i.getElementsByTagName("name")[0].firstChild.nodeValue
|
2012-12-22 20:46:22 +04:00
|
|
|
url = location_dec(i.getElementsByTagName("location")[0].firstChild.nodeValue)
|
2014-02-22 20:22:36 +04:00
|
|
|
try:
|
|
|
|
lrc_url = i.getElementsByTagName("lyric")[0].firstChild.nodeValue
|
|
|
|
except:
|
|
|
|
pass
|
2017-08-08 10:00:51 +03:00
|
|
|
type_, ext, size = url_info(url, headers=fake_headers)
|
2013-07-23 05:41:41 +04:00
|
|
|
if not ext:
|
|
|
|
ext = 'mp3'
|
2017-02-02 07:43:57 +03:00
|
|
|
|
2013-07-23 05:41:41 +04:00
|
|
|
print_info(site_info, song_title, ext, size)
|
2012-12-27 07:00:08 +04:00
|
|
|
if not info_only:
|
2015-03-11 14:38:21 +03:00
|
|
|
file_name = "%s - %s - %s" % (song_title, artist, album_name)
|
2017-08-08 10:00:51 +03:00
|
|
|
download_urls([url], file_name, ext, size, output_dir, headers=fake_headers)
|
2013-08-10 12:57:09 +04:00
|
|
|
try:
|
|
|
|
xiami_download_lyric(lrc_url, file_name, output_dir)
|
|
|
|
except:
|
|
|
|
pass
|
2013-03-26 16:53:39 +04:00
|
|
|
|
2017-08-08 10:00:51 +03:00
|
|
|
def xiami_download_showcollect(cid, output_dir = '.', info_only = False):
|
|
|
|
html = get_content('http://www.xiami.com/song/showcollect/id/' + cid, headers=fake_headers)
|
2013-03-26 16:53:39 +04:00
|
|
|
collect_name = r1(r'<title>(.*)</title>', html)
|
2012-12-22 20:46:22 +04:00
|
|
|
|
2017-08-08 10:00:51 +03:00
|
|
|
xml = get_content('http://www.xiami.com/song/playlist/id/%s/type/3' % cid, headers=fake_headers)
|
2012-12-22 20:46:22 +04:00
|
|
|
doc = parseString(xml)
|
2013-03-26 16:53:39 +04:00
|
|
|
output_dir = output_dir + "/" + "[" + collect_name + "]"
|
2012-12-22 20:46:22 +04:00
|
|
|
tracks = doc.getElementsByTagName("track")
|
|
|
|
track_nr = 1
|
|
|
|
for i in tracks:
|
2015-03-10 14:00:20 +03:00
|
|
|
artist=album_name=song_title=url=""
|
|
|
|
try:
|
|
|
|
song_id = i.getElementsByTagName("song_id")[0].firstChild.nodeValue
|
|
|
|
artist = i.getElementsByTagName("artist")[0].firstChild.nodeValue
|
|
|
|
album_name = i.getElementsByTagName("album_name")[0].firstChild.nodeValue
|
|
|
|
song_title = i.getElementsByTagName("title")[0].firstChild.nodeValue
|
|
|
|
url = location_dec(i.getElementsByTagName("location")[0].firstChild.nodeValue)
|
|
|
|
except:
|
|
|
|
log.e("Song %s failed. [Info Missing] artist:%s, album:%s, title:%s, url:%s" % (song_id, artist, album_name, song_title, url))
|
|
|
|
continue
|
2014-02-22 20:49:43 +04:00
|
|
|
try:
|
|
|
|
lrc_url = i.getElementsByTagName("lyric")[0].firstChild.nodeValue
|
|
|
|
except:
|
|
|
|
pass
|
2017-08-08 10:00:51 +03:00
|
|
|
type_, ext, size = url_info(url, headers=fake_headers)
|
2013-04-12 22:15:18 +04:00
|
|
|
if not ext:
|
|
|
|
ext = 'mp3'
|
2017-02-02 07:43:57 +03:00
|
|
|
|
2017-08-08 10:00:51 +03:00
|
|
|
print_info(site_info, song_title, ext, size)
|
2012-12-27 07:00:08 +04:00
|
|
|
if not info_only:
|
2013-03-26 16:53:39 +04:00
|
|
|
file_name = "%02d.%s - %s - %s" % (track_nr, song_title, artist, album_name)
|
2017-08-08 10:00:51 +03:00
|
|
|
download_urls([url], file_name, ext, size, output_dir, headers=fake_headers)
|
2013-08-10 12:57:09 +04:00
|
|
|
try:
|
|
|
|
xiami_download_lyric(lrc_url, file_name, output_dir)
|
|
|
|
except:
|
|
|
|
pass
|
2017-02-02 07:43:57 +03:00
|
|
|
|
2012-12-22 20:46:22 +04:00
|
|
|
track_nr += 1
|
|
|
|
|
2017-08-08 10:00:51 +03:00
|
|
|
def xiami_download_album(aid, output_dir='.', info_only=False):
|
|
|
|
xml = get_content('http://www.xiami.com/song/playlist/id/%s/type/1' % aid, headers=fake_headers)
|
2012-12-22 20:46:22 +04:00
|
|
|
album_name = r1(r'<album_name><!\[CDATA\[(.*)\]\]>', xml)
|
2013-03-26 16:53:39 +04:00
|
|
|
artist = r1(r'<artist><!\[CDATA\[(.*)\]\]>', xml)
|
2012-12-22 20:46:22 +04:00
|
|
|
doc = parseString(xml)
|
2013-03-26 16:53:39 +04:00
|
|
|
output_dir = output_dir + "/%s - %s" % (artist, album_name)
|
2017-08-08 10:00:51 +03:00
|
|
|
track_list = doc.getElementsByTagName('trackList')[0]
|
|
|
|
tracks = track_list.getElementsByTagName("track")
|
2012-12-22 20:46:22 +04:00
|
|
|
track_nr = 1
|
2013-04-29 05:53:13 +04:00
|
|
|
pic_exist = False
|
2012-12-22 20:46:22 +04:00
|
|
|
for i in tracks:
|
2017-08-08 10:00:51 +03:00
|
|
|
#in this xml track tag is used for both "track in a trackList" and track no
|
|
|
|
#dirty here
|
|
|
|
if i.firstChild.nodeValue is not None:
|
|
|
|
continue
|
|
|
|
song_title = i.getElementsByTagName("songName")[0].firstChild.nodeValue
|
2012-12-22 20:46:22 +04:00
|
|
|
url = location_dec(i.getElementsByTagName("location")[0].firstChild.nodeValue)
|
2014-02-22 20:49:43 +04:00
|
|
|
try:
|
|
|
|
lrc_url = i.getElementsByTagName("lyric")[0].firstChild.nodeValue
|
|
|
|
except:
|
|
|
|
pass
|
2013-04-29 05:53:13 +04:00
|
|
|
if not pic_exist:
|
|
|
|
pic_url = i.getElementsByTagName("pic")[0].firstChild.nodeValue
|
2017-08-08 10:00:51 +03:00
|
|
|
type_, ext, size = url_info(url, headers=fake_headers)
|
2013-04-12 22:15:18 +04:00
|
|
|
if not ext:
|
|
|
|
ext = 'mp3'
|
|
|
|
|
2017-08-08 10:00:51 +03:00
|
|
|
print_info(site_info, song_title, ext, size)
|
2012-12-27 07:00:08 +04:00
|
|
|
if not info_only:
|
2013-03-26 16:53:39 +04:00
|
|
|
file_name = "%02d.%s" % (track_nr, song_title)
|
2017-08-08 10:00:51 +03:00
|
|
|
download_urls([url], file_name, ext, size, output_dir, headers=fake_headers)
|
2013-08-10 12:57:09 +04:00
|
|
|
try:
|
|
|
|
xiami_download_lyric(lrc_url, file_name, output_dir)
|
|
|
|
except:
|
|
|
|
pass
|
2013-04-29 05:53:13 +04:00
|
|
|
if not pic_exist:
|
|
|
|
xiami_download_pic(pic_url, 'cover', output_dir)
|
|
|
|
pic_exist = True
|
2017-02-02 07:43:57 +03:00
|
|
|
|
2012-12-22 20:46:22 +04:00
|
|
|
track_nr += 1
|
|
|
|
|
2017-08-08 10:00:51 +03:00
|
|
|
def xiami_download(url, output_dir='.', info_only=False, **kwargs):
|
|
|
|
#albums
|
2012-12-22 20:46:22 +04:00
|
|
|
if re.match(r'http://www.xiami.com/album/\d+', url):
|
|
|
|
id = r1(r'http://www.xiami.com/album/(\d+)', url)
|
2017-08-08 10:00:51 +03:00
|
|
|
xiami_download_album(id, output_dir, info_only)
|
|
|
|
elif re.match(r'http://www.xiami.com/album/\w+', url):
|
|
|
|
page = get_content(url, headers=fake_headers)
|
|
|
|
album_id = re.search(r'rel="canonical"\s+href="http://www.xiami.com/album/([^"]+)"', page).group(1)
|
|
|
|
xiami_download_album(album_id, output_dir, info_only)
|
2017-02-02 07:43:57 +03:00
|
|
|
|
2017-08-08 10:00:51 +03:00
|
|
|
#collections
|
2015-03-09 08:50:09 +03:00
|
|
|
if re.match(r'http://www.xiami.com/collect/\d+', url):
|
|
|
|
id = r1(r'http://www.xiami.com/collect/(\d+)', url)
|
2017-08-08 10:00:51 +03:00
|
|
|
xiami_download_showcollect(id, output_dir, info_only)
|
2017-02-02 07:43:57 +03:00
|
|
|
|
2017-08-08 10:00:51 +03:00
|
|
|
#single track
|
2017-03-11 16:56:30 +03:00
|
|
|
if re.match(r'http://www.xiami.com/song/\d+\b', url):
|
|
|
|
id = r1(r'http://www.xiami.com/song/(\d+)', url)
|
2017-08-08 10:00:51 +03:00
|
|
|
xiami_download_song(id, output_dir, info_only)
|
2017-03-11 16:56:30 +03:00
|
|
|
elif re.match(r'http://www.xiami.com/song/\w+', url):
|
2017-08-08 10:00:51 +03:00
|
|
|
html = get_content(url, headers=fake_headers)
|
2017-02-02 07:43:57 +03:00
|
|
|
id = r1(r'rel="canonical" href="http://www.xiami.com/song/([^"]+)"', html)
|
2017-08-08 10:00:51 +03:00
|
|
|
xiami_download_song(id, output_dir, info_only)
|
2017-02-02 07:43:57 +03:00
|
|
|
|
2013-07-23 05:45:07 +04:00
|
|
|
if re.match('http://www.xiami.com/song/detail/id/\d+', url):
|
|
|
|
id = r1(r'http://www.xiami.com/song/detail/id/(\d+)', url)
|
2017-08-08 10:00:51 +03:00
|
|
|
xiami_download_song(id, output_dir, info_only)
|
2012-12-22 20:46:22 +04:00
|
|
|
|
|
|
|
site_info = "Xiami.com"
|
|
|
|
download = xiami_download
|
|
|
|
download_playlist = playlist_not_supported("xiami")
|