[xiami] Improve extraction (Closes #9079)
[youtube-dl] / youtube_dl / extractor / xiami.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 from .common import InfoExtractor
5 from ..compat import compat_urllib_parse_unquote
6 from ..utils import int_or_none
7
8
9 class XiamiBaseIE(InfoExtractor):
10     _API_BASE_URL = 'http://www.xiami.com/song/playlist/cat/json/id'
11
12     def _extract_track(self, track, track_id=None):
13         title = track['title']
14         track_url = self._decrypt(track['location'])
15
16         subtitles = {}
17         lyrics_url = track.get('lyric_url') or track.get('lyric')
18         if lyrics_url and lyrics_url.startswith('http'):
19             subtitles['origin'] = [{'url': lyrics_url}]
20
21         return {
22             'id': track.get('song_id') or track_id,
23             'url': track_url,
24             'title': title,
25             'thumbnail': track.get('pic') or track.get('album_pic'),
26             'duration': int_or_none(track.get('length')),
27             'creator': track.get('artist', '').split(';')[0],
28             'track': title,
29             'album': track.get('album_name'),
30             'artist': track.get('artist'),
31             'subtitles': subtitles,
32         }
33
34     def _extract_tracks(self, item_id, typ=None):
35         playlist = self._download_json(
36             '%s/%s%s' % (self._API_BASE_URL, item_id, '/type/%s' % typ if typ else ''), item_id)
37         return [
38             self._extract_track(track, item_id)
39             for track in playlist['data']['trackList']]
40
41     @staticmethod
42     def _decrypt(origin):
43         n = int(origin[0])
44         origin = origin[1:]
45         short_lenth = len(origin) // n
46         long_num = len(origin) - short_lenth * n
47         l = tuple()
48         for i in range(0, n):
49             length = short_lenth
50             if i < long_num:
51                 length += 1
52             l += (origin[0:length], )
53             origin = origin[length:]
54         ans = ''
55         for i in range(0, short_lenth + 1):
56             for j in range(0, n):
57                 if len(l[j]) > i:
58                     ans += l[j][i]
59         return compat_urllib_parse_unquote(ans).replace('^', '0')
60
61
62 class XiamiSongIE(XiamiBaseIE):
63     IE_NAME = 'xiami:song'
64     IE_DESC = '虾米音乐'
65     _VALID_URL = r'https?://(?:www\.)?xiami\.com/song/(?P<id>[0-9]+)'
66     _TESTS = [{
67         'url': 'http://www.xiami.com/song/1775610518',
68         'md5': '521dd6bea40fd5c9c69f913c232cb57e',
69         'info_dict': {
70             'id': '1775610518',
71             'ext': 'mp3',
72             'title': 'Woman',
73             'thumbnail': r're:http://img\.xiami\.net/images/album/.*\.jpg',
74             'duration': 265,
75             'creator': 'HONNE',
76             'track': 'Woman',
77             'album': 'Woman',
78             'artist': 'HONNE',
79             'subtitles': {
80                 'origin': [{
81                     'ext': 'lrc',
82                 }],
83             },
84         }
85     }, {
86         'url': 'http://www.xiami.com/song/1775256504',
87         'md5': '932a3abd45c6aa2b1fdbe028fcb4c4fc',
88         'info_dict': {
89             'id': '1775256504',
90             'ext': 'mp3',
91             'title': '悟空',
92             'thumbnail': r're:http://img\.xiami\.net/images/album/.*\.jpg',
93             'duration': 200,
94             'creator': '戴荃',
95             'track': '悟空',
96             'album': '悟空',
97             'artist': '戴荃',
98             'subtitles': {
99                 'origin': [{
100                     'ext': 'lrc',
101                 }],
102             },
103         }
104     }]
105
106     def _real_extract(self, url):
107         return self._extract_tracks(self._match_id(url))[0]
108
109
110 class XiamiPlaylistBaseIE(XiamiBaseIE):
111     def _real_extract(self, url):
112         item_id = self._match_id(url)
113         return self.playlist_result(self._extract_tracks(item_id, self._TYPE), item_id)
114
115
116 class XiamiAlbumIE(XiamiPlaylistBaseIE):
117     IE_NAME = 'xiami:album'
118     IE_DESC = '虾米音乐 - 专辑'
119     _VALID_URL = r'https?://(?:www\.)?xiami\.com/album/(?P<id>[0-9]+)'
120     _TYPE = '1'
121     _TESTS = [{
122         'url': 'http://www.xiami.com/album/2100300444',
123         'info_dict': {
124             'id': '2100300444',
125         },
126         'playlist_count': 10,
127     }, {
128         'url': 'http://www.xiami.com/album/512288?spm=a1z1s.6843761.1110925389.6.hhE9p9',
129         'only_matching': True,
130     }]
131
132
133 class XiamiArtistIE(XiamiPlaylistBaseIE):
134     IE_NAME = 'xiami:artist'
135     IE_DESC = '虾米音乐 - 歌手'
136     _VALID_URL = r'https?://(?:www\.)?xiami\.com/artist/(?P<id>[0-9]+)'
137     _TYPE = '2'
138     _TEST = {
139         'url': 'http://www.xiami.com/artist/2132?spm=0.0.0.0.dKaScp',
140         'info_dict': {
141             'id': '2132',
142         },
143         'playlist_count': 20,
144     }
145
146
147 class XiamiCollectionIE(XiamiPlaylistBaseIE):
148     IE_NAME = 'xiami:collection'
149     IE_DESC = '虾米音乐 - 精选集'
150     _VALID_URL = r'https?://(?:www\.)?xiami\.com/collect/(?P<id>[0-9]+)'
151     _TYPE = '3'
152     _TEST = {
153         'url': 'http://www.xiami.com/collect/156527391?spm=a1z1s.2943601.6856193.12.4jpBnr',
154         'info_dict': {
155             'id': '156527391',
156         },
157         'playlist_mincount': 29,
158     }