2 from __future__ 
import unicode_literals
 
   4 from .common 
import InfoExtractor
 
   5 from ..compat 
import compat_urllib_parse_unquote
 
   6 from ..utils 
import int_or_none
 
   9 class XiamiBaseIE(InfoExtractor
): 
  10     _API_BASE_URL 
= 'http://www.xiami.com/song/playlist/cat/json/id' 
  12     def _download_webpage(self
, *args
, **kwargs
): 
  13         webpage 
= super(XiamiBaseIE
, self
)._download
_webpage
(*args
, **kwargs
) 
  14         if '>Xiami is currently not available in your country.<' in webpage
: 
  15             self
.raise_geo_restricted('Xiami is currently not available in your country') 
  18     def _extract_track(self
, track
, track_id
=None): 
  19         track_name 
= track
.get('songName') or track
.get('name') or track
['subName'] 
  20         artist 
= track
.get('artist') or track
.get('artist_name') or track
.get('singers') 
  21         title 
= '%s - %s' % (artist
, track_name
) if artist 
else track_name
 
  22         track_url 
= self
._decrypt
(track
['location']) 
  25         lyrics_url 
= track
.get('lyric_url') or track
.get('lyric') 
  26         if lyrics_url 
and lyrics_url
.startswith('http'): 
  27             subtitles
['origin'] = [{'url': lyrics_url
}] 
  30             'id': track
.get('song_id') or track_id
, 
  33             'thumbnail': track
.get('pic') or track
.get('album_pic'), 
  34             'duration': int_or_none(track
.get('length')), 
  35             'creator': track
.get('artist', '').split(';')[0], 
  37             'track_number': int_or_none(track
.get('track')), 
  38             'album': track
.get('album_name') or track
.get('title'), 
  40             'subtitles': subtitles
, 
  43     def _extract_tracks(self
, item_id
, typ
=None): 
  44         playlist 
= self
._download
_json
( 
  45             '%s/%s%s' % (self
._API
_BASE
_URL
, item_id
, '/type/%s' % typ 
if typ 
else ''), item_id
) 
  47             self
._extract
_track
(track
, item_id
) 
  48             for track 
in playlist
['data']['trackList']] 
  54         short_lenth 
= len(origin
) // n
 
  55         long_num 
= len(origin
) - short_lenth 
* n
 
  61             l 
+= (origin
[0:length
], ) 
  62             origin 
= origin
[length
:] 
  64         for i 
in range(0, short_lenth 
+ 1): 
  68         return compat_urllib_parse_unquote(ans
).replace('^', '0') 
  71 class XiamiSongIE(XiamiBaseIE
): 
  72     IE_NAME 
= 'xiami:song' 
  74     _VALID_URL 
= r
'https?://(?:www\.)?xiami\.com/song/(?P<id>[^/?#&]+)' 
  76         'url': 'http://www.xiami.com/song/1775610518', 
  77         'md5': '521dd6bea40fd5c9c69f913c232cb57e', 
  81             'title': 'HONNE - Woman', 
  82             'thumbnail': r
're:http://img\.xiami\.net/images/album/.*\.jpg', 
  94         'skip': 'Georestricted', 
  96         'url': 'http://www.xiami.com/song/1775256504', 
  97         'md5': '932a3abd45c6aa2b1fdbe028fcb4c4fc', 
 102             'thumbnail': r
're:http://img\.xiami\.net/images/album/.*\.jpg', 
 114         'skip': 'Georestricted', 
 116         'url': 'http://www.xiami.com/song/1775953850', 
 120             'title': 'До Скону - Чума Пожирает Землю', 
 121             'thumbnail': r
're:http://img\.xiami\.net/images/album/.*\.jpg', 
 123             'creator': 'До Скону', 
 124             'track': 'Чума Пожирает Землю', 
 127             'artist': 'До Скону', 
 130             'skip_download': True, 
 133         'url': 'http://www.xiami.com/song/xLHGwgd07a1', 
 134         'only_matching': True, 
 137     def _real_extract(self
, url
): 
 138         return self
._extract
_tracks
(self
._match
_id
(url
))[0] 
 141 class XiamiPlaylistBaseIE(XiamiBaseIE
): 
 142     def _real_extract(self
, url
): 
 143         item_id 
= self
._match
_id
(url
) 
 144         return self
.playlist_result(self
._extract
_tracks
(item_id
, self
._TYPE
), item_id
) 
 147 class XiamiAlbumIE(XiamiPlaylistBaseIE
): 
 148     IE_NAME 
= 'xiami:album' 
 149     IE_DESC 
= '虾米音乐 - 专辑' 
 150     _VALID_URL 
= r
'https?://(?:www\.)?xiami\.com/album/(?P<id>[^/?#&]+)' 
 153         'url': 'http://www.xiami.com/album/2100300444', 
 157         'playlist_count': 10, 
 158         'skip': 'Georestricted', 
 160         'url': 'http://www.xiami.com/album/512288?spm=a1z1s.6843761.1110925389.6.hhE9p9', 
 161         'only_matching': True, 
 163         'url': 'http://www.xiami.com/album/URVDji2a506', 
 164         'only_matching': True, 
 168 class XiamiArtistIE(XiamiPlaylistBaseIE
): 
 169     IE_NAME 
= 'xiami:artist' 
 170     IE_DESC 
= '虾米音乐 - 歌手' 
 171     _VALID_URL 
= r
'https?://(?:www\.)?xiami\.com/artist/(?P<id>[^/?#&]+)' 
 174         'url': 'http://www.xiami.com/artist/2132?spm=0.0.0.0.dKaScp', 
 178         'playlist_count': 20, 
 179         'skip': 'Georestricted', 
 181         'url': 'http://www.xiami.com/artist/bC5Tk2K6eb99', 
 182         'only_matching': True, 
 186 class XiamiCollectionIE(XiamiPlaylistBaseIE
): 
 187     IE_NAME 
= 'xiami:collection' 
 188     IE_DESC 
= '虾米音乐 - 精选集' 
 189     _VALID_URL 
= r
'https?://(?:www\.)?xiami\.com/collect/(?P<id>[^/?#&]+)' 
 192         'url': 'http://www.xiami.com/collect/156527391?spm=a1z1s.2943601.6856193.12.4jpBnr', 
 196         'playlist_mincount': 29, 
 197         'skip': 'Georestricted',