]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/xiami.py
Completely change project name to yt-dlp (#85)
[yt-dlp.git] / yt_dlp / extractor / xiami.py
CommitLineData
4e0c0c15 1# coding: utf-8
89c0dc9a
B
2from __future__ import unicode_literals
3
4from .common import InfoExtractor
89c0dc9a 5from ..compat import compat_urllib_parse_unquote
4e0c0c15 6from ..utils import int_or_none
89c0dc9a
B
7
8
9class XiamiBaseIE(InfoExtractor):
91839809 10 _API_BASE_URL = 'https://emumo.xiami.com/song/playlist/cat/json/id'
4e0c0c15 11
e5eadfa8
S
12 def _download_webpage_handle(self, *args, **kwargs):
13 webpage = super(XiamiBaseIE, self)._download_webpage_handle(*args, **kwargs)
7759be38
YCH
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')
097eba01 16 return webpage
7759be38 17
4e0c0c15 18 def _extract_track(self, track, track_id=None):
79fc8496
S
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
4e0c0c15
S
22 track_url = self._decrypt(track['location'])
23
24 subtitles = {}
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}]
28
29 return {
30 'id': track.get('song_id') or track_id,
31 'url': track_url,
32 'title': title,
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],
79fc8496
S
36 'track': track_name,
37 'track_number': int_or_none(track.get('track')),
38 'album': track.get('album_name') or track.get('title'),
39 'artist': artist,
4e0c0c15 40 'subtitles': subtitles,
89c0dc9a
B
41 }
42
1663b329 43 def _extract_tracks(self, item_id, referer, typ=None):
4e0c0c15 44 playlist = self._download_json(
1663b329
RA
45 '%s/%s%s' % (self._API_BASE_URL, item_id, '/type/%s' % typ if typ else ''),
46 item_id, headers={
47 'Referer': referer,
48 })
4e0c0c15
S
49 return [
50 self._extract_track(track, item_id)
51 for track in playlist['data']['trackList']]
89c0dc9a
B
52
53 @staticmethod
54 def _decrypt(origin):
55 n = int(origin[0])
56 origin = origin[1:]
a0566bbf 57 short_length = len(origin) // n
58 long_num = len(origin) - short_length * n
89c0dc9a
B
59 l = tuple()
60 for i in range(0, n):
a0566bbf 61 length = short_length
89c0dc9a
B
62 if i < long_num:
63 length += 1
64 l += (origin[0:length], )
65 origin = origin[length:]
66 ans = ''
a0566bbf 67 for i in range(0, short_length + 1):
89c0dc9a 68 for j in range(0, n):
4e0c0c15 69 if len(l[j]) > i:
89c0dc9a
B
70 ans += l[j][i]
71 return compat_urllib_parse_unquote(ans).replace('^', '0')
72
73
4e0c0c15 74class XiamiSongIE(XiamiBaseIE):
89c0dc9a
B
75 IE_NAME = 'xiami:song'
76 IE_DESC = '虾米音乐'
79fc8496 77 _VALID_URL = r'https?://(?:www\.)?xiami\.com/song/(?P<id>[^/?#&]+)'
4e0c0c15
S
78 _TESTS = [{
79 'url': 'http://www.xiami.com/song/1775610518',
80 'md5': '521dd6bea40fd5c9c69f913c232cb57e',
81 'info_dict': {
82 'id': '1775610518',
83 'ext': 'mp3',
79fc8496 84 'title': 'HONNE - Woman',
4e0c0c15
S
85 'thumbnail': r're:http://img\.xiami\.net/images/album/.*\.jpg',
86 'duration': 265,
87 'creator': 'HONNE',
88 'track': 'Woman',
89 'album': 'Woman',
90 'artist': 'HONNE',
91 'subtitles': {
92 'origin': [{
93 'ext': 'lrc',
94 }],
95 },
7759be38
YCH
96 },
97 'skip': 'Georestricted',
4e0c0c15
S
98 }, {
99 'url': 'http://www.xiami.com/song/1775256504',
100 'md5': '932a3abd45c6aa2b1fdbe028fcb4c4fc',
101 'info_dict': {
102 'id': '1775256504',
103 'ext': 'mp3',
79fc8496 104 'title': '戴荃 - 悟空',
4e0c0c15
S
105 'thumbnail': r're:http://img\.xiami\.net/images/album/.*\.jpg',
106 'duration': 200,
107 'creator': '戴荃',
108 'track': '悟空',
109 'album': '悟空',
110 'artist': '戴荃',
111 'subtitles': {
112 'origin': [{
113 'ext': 'lrc',
114 }],
115 },
7759be38
YCH
116 },
117 'skip': 'Georestricted',
79fc8496
S
118 }, {
119 'url': 'http://www.xiami.com/song/1775953850',
120 'info_dict': {
121 'id': '1775953850',
122 'ext': 'mp3',
123 'title': 'До Скону - Чума Пожирает Землю',
124 'thumbnail': r're:http://img\.xiami\.net/images/album/.*\.jpg',
125 'duration': 683,
126 'creator': 'До Скону',
127 'track': 'Чума Пожирает Землю',
128 'track_number': 7,
129 'album': 'Ад',
130 'artist': 'До Скону',
131 },
132 'params': {
133 'skip_download': True,
134 },
135 }, {
136 'url': 'http://www.xiami.com/song/xLHGwgd07a1',
137 'only_matching': True,
4e0c0c15 138 }]
89c0dc9a
B
139
140 def _real_extract(self, url):
1663b329 141 return self._extract_tracks(self._match_id(url), url)[0]
89c0dc9a
B
142
143
4e0c0c15
S
144class XiamiPlaylistBaseIE(XiamiBaseIE):
145 def _real_extract(self, url):
146 item_id = self._match_id(url)
1663b329 147 return self.playlist_result(self._extract_tracks(item_id, url, self._TYPE), item_id)
4e0c0c15
S
148
149
150class XiamiAlbumIE(XiamiPlaylistBaseIE):
89c0dc9a
B
151 IE_NAME = 'xiami:album'
152 IE_DESC = '虾米音乐 - 专辑'
79fc8496 153 _VALID_URL = r'https?://(?:www\.)?xiami\.com/album/(?P<id>[^/?#&]+)'
4e0c0c15
S
154 _TYPE = '1'
155 _TESTS = [{
156 'url': 'http://www.xiami.com/album/2100300444',
157 'info_dict': {
158 'id': '2100300444',
89c0dc9a 159 },
4e0c0c15 160 'playlist_count': 10,
7759be38 161 'skip': 'Georestricted',
4e0c0c15
S
162 }, {
163 'url': 'http://www.xiami.com/album/512288?spm=a1z1s.6843761.1110925389.6.hhE9p9',
164 'only_matching': True,
79fc8496
S
165 }, {
166 'url': 'http://www.xiami.com/album/URVDji2a506',
167 'only_matching': True,
4e0c0c15 168 }]
89c0dc9a
B
169
170
4e0c0c15 171class XiamiArtistIE(XiamiPlaylistBaseIE):
89c0dc9a
B
172 IE_NAME = 'xiami:artist'
173 IE_DESC = '虾米音乐 - 歌手'
79fc8496 174 _VALID_URL = r'https?://(?:www\.)?xiami\.com/artist/(?P<id>[^/?#&]+)'
4e0c0c15 175 _TYPE = '2'
79fc8496 176 _TESTS = [{
89c0dc9a
B
177 'url': 'http://www.xiami.com/artist/2132?spm=0.0.0.0.dKaScp',
178 'info_dict': {
179 'id': '2132',
180 },
181 'playlist_count': 20,
7759be38 182 'skip': 'Georestricted',
79fc8496
S
183 }, {
184 'url': 'http://www.xiami.com/artist/bC5Tk2K6eb99',
185 'only_matching': True,
186 }]
89c0dc9a 187
89c0dc9a 188
4e0c0c15 189class XiamiCollectionIE(XiamiPlaylistBaseIE):
89c0dc9a
B
190 IE_NAME = 'xiami:collection'
191 IE_DESC = '虾米音乐 - 精选集'
79fc8496 192 _VALID_URL = r'https?://(?:www\.)?xiami\.com/collect/(?P<id>[^/?#&]+)'
4e0c0c15 193 _TYPE = '3'
89c0dc9a
B
194 _TEST = {
195 'url': 'http://www.xiami.com/collect/156527391?spm=a1z1s.2943601.6856193.12.4jpBnr',
196 'info_dict': {
197 'id': '156527391',
198 },
4e0c0c15 199 'playlist_mincount': 29,
7759be38 200 'skip': 'Georestricted',
89c0dc9a 201 }