1 # -*- coding: utf-8 -*-
3 from __future__ import unicode_literals
5 from .common import InfoExtractor
13 from ..compat import compat_urllib_parse_unquote
16 class XiamiBaseIE(InfoExtractor):
18 _XML_BASE_URL = 'http://www.xiami.com/song/playlist/id'
19 _NS_MAP = {'xm': 'http://xspf.org/ns/0/'}
21 def _extract_track(self, track):
22 artist = xpath_text(track, xpath_with_ns('xm:artist', self._NS_MAP), default='')
23 artist = artist.split(';')
26 'id': xpath_text(track, xpath_with_ns('xm:song_id', self._NS_MAP)),
27 'title': xpath_text(track, xpath_with_ns('xm:title', self._NS_MAP)),
28 'album': xpath_text(track, xpath_with_ns('xm:album_name', self._NS_MAP)),
29 'artist': ';'.join(artist) if artist else None,
30 'creator': artist[0] if artist else None,
31 'url': self._decrypt(xpath_text(track, xpath_with_ns('xm:location', self._NS_MAP))),
32 'thumbnail': xpath_text(track, xpath_with_ns('xm:pic', self._NS_MAP), default=None),
33 'duration': int_or_none(xpath_text(track, xpath_with_ns('xm:length', self._NS_MAP))),
36 lyrics_url = xpath_text(track, xpath_with_ns('xm:lyric', self._NS_MAP))
37 if lyrics_url and lyrics_url.endswith('.lrc'):
38 ret['description'] = self._download_webpage(lyrics_url, ret['id'])
41 def _extract_xml(self, _id, typ=''):
42 playlist = self._download_xml('%s/%s%s' % (self._XML_BASE_URL, _id, typ), _id)
43 tracklist = xpath_element(playlist, xpath_with_ns('./xm:trackList', self._NS_MAP))
45 if not len(tracklist):
46 raise ExtractorError('No track found')
47 return [self._extract_track(track) for track in tracklist]
53 short_lenth = len(origin) // n
54 long_num = len(origin) - short_lenth * n
60 l += (origin[0:length], )
61 origin = origin[length:]
63 for i in range(0, short_lenth + 1):
67 return compat_urllib_parse_unquote(ans).replace('^', '0')
70 class XiamiIE(XiamiBaseIE):
71 IE_NAME = 'xiami:song'
73 _VALID_URL = r'http://www\.xiami\.com/song/(?P<id>[0-9]+)'
76 'url': 'http://www.xiami.com/song/1775610518',
77 'md5': '521dd6bea40fd5c9c69f913c232cb57e',
84 'thumbnail': r're:http://img\.xiami\.net/images/album/.*\.jpg',
85 'description': 'md5:052ec7de41ca19f67e7fd70a1bfc4e0b',
89 'url': 'http://www.xiami.com/song/1775256504',
90 'md5': '932a3abd45c6aa2b1fdbe028fcb4c4fc',
97 'description': 'md5:206e67e84f9bed1d473d04196a00b990',
102 def _real_extract(self, url):
103 _id = self._match_id(url)
104 return self._extract_xml(_id)[0]
107 class XiamiAlbumIE(XiamiBaseIE):
108 IE_NAME = 'xiami:album'
109 IE_DESC = '虾米音乐 - 专辑'
110 _VALID_URL = r'http://www\.xiami\.com/album/(?P<id>[0-9]+)'
113 'url': 'http://www.xiami.com/album/2100300444',
117 'playlist_count': 10,
120 'url': 'http://www.xiami.com/album/512288?spm=a1z1s.6843761.1110925389.6.hhE9p9',
121 'only_matching': True,
125 def _real_extract(self, url):
126 _id = self._match_id(url)
127 return self.playlist_result(self._extract_xml(_id, '/type/1'), _id)
130 class XiamiArtistIE(XiamiBaseIE):
131 IE_NAME = 'xiami:artist'
132 IE_DESC = '虾米音乐 - 歌手'
133 _VALID_URL = r'http://www\.xiami\.com/artist/(?P<id>[0-9]+)'
135 'url': 'http://www.xiami.com/artist/2132?spm=0.0.0.0.dKaScp',
139 'playlist_count': 20,
142 def _real_extract(self, url):
143 _id = self._match_id(url)
144 return self.playlist_result(self._extract_xml(_id, '/type/2'), _id)
147 class XiamiCollectionIE(XiamiBaseIE):
148 IE_NAME = 'xiami:collection'
149 IE_DESC = '虾米音乐 - 精选集'
150 _VALID_URL = r'http://www\.xiami\.com/collect/(?P<id>[0-9]+)'
152 'url': 'http://www.xiami.com/collect/156527391?spm=a1z1s.2943601.6856193.12.4jpBnr',
156 'playlist_count': 26,
159 def _real_extract(self, url):
160 _id = self._match_id(url)
161 return self.playlist_result(self._extract_xml(_id, '/type/3'), _id)