Merge branch 'kuwo' of https://github.com/ping/youtube-dl into ping-kuwo
[youtube-dl] / youtube_dl / extractor / qqmusic.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import random
5 import time
6 import re
7
8 from .common import InfoExtractor
9 from ..utils import (
10     strip_jsonp,
11     unescapeHTML,
12     clean_html,
13 )
14 from ..compat import compat_urllib_request
15
16
17 class QQMusicIE(InfoExtractor):
18     IE_NAME = 'qqmusic'
19     _VALID_URL = r'http://y.qq.com/#type=song&mid=(?P<id>[0-9A-Za-z]+)'
20     _TESTS = [{
21         'url': 'http://y.qq.com/#type=song&mid=004295Et37taLD',
22         'md5': '9ce1c1c8445f561506d2e3cfb0255705',
23         'info_dict': {
24             'id': '004295Et37taLD',
25             'ext': 'mp3',
26             'title': '可惜没如果',
27             'upload_date': '20141227',
28             'creator': '林俊杰',
29             'description': 'md5:d327722d0361576fde558f1ac68a7065',
30             'thumbnail': 're:^https?://.*\.jpg$',
31         }
32     }, {
33         'note': 'There is no mp3-320 version of this song.',
34         'url': 'http://y.qq.com/#type=song&mid=004MsGEo3DdNxV',
35         'md5': 'fa3926f0c585cda0af8fa4f796482e3e',
36         'info_dict': {
37             'id': '004MsGEo3DdNxV',
38             'ext': 'mp3',
39             'title': '如果',
40             'upload_date': '20050626',
41             'creator': '李季美',
42             'description': 'md5:46857d5ed62bc4ba84607a805dccf437',
43             'thumbnail': 're:^https?://.*\.jpg$',
44         }
45     }]
46
47     _FORMATS = {
48         'mp3-320': {'prefix': 'M800', 'ext': 'mp3', 'preference': 40, 'abr': 320},
49         'mp3-128': {'prefix': 'M500', 'ext': 'mp3', 'preference': 30, 'abr': 128},
50         'm4a': {'prefix': 'C200', 'ext': 'm4a', 'preference': 10}
51     }
52
53     # Reference: m_r_GetRUin() in top_player.js
54     # http://imgcache.gtimg.cn/music/portal_v3/y/top_player.js
55     @staticmethod
56     def m_r_get_ruin():
57         curMs = int(time.time() * 1000) % 1000
58         return int(round(random.random() * 2147483647) * curMs % 1E10)
59
60     def _real_extract(self, url):
61         mid = self._match_id(url)
62
63         detail_info_page = self._download_webpage(
64             'http://s.plcloud.music.qq.com/fcgi-bin/fcg_yqq_song_detail_info.fcg?songmid=%s&play=0' % mid,
65             mid, note='Download song detail info',
66             errnote='Unable to get song detail info', encoding='gbk')
67
68         song_name = self._html_search_regex(
69             r"songname:\s*'([^']+)'", detail_info_page, 'song name')
70
71         publish_time = self._html_search_regex(
72             r'发行时间:(\d{4}-\d{2}-\d{2})', detail_info_page,
73             'publish time', default=None)
74         if publish_time:
75             publish_time = publish_time.replace('-', '')
76
77         singer = self._html_search_regex(
78             r"singer:\s*'([^']+)", detail_info_page, 'singer', default=None)
79
80         lrc_content = self._html_search_regex(
81             r'<div class="content" id="lrc_content"[^<>]*>([^<>]+)</div>',
82             detail_info_page, 'LRC lyrics', default=None)
83         if lrc_content:
84             lrc_content = lrc_content.replace('\\n', '\n')
85
86         thumbnail_url = None
87         albummid = self._search_regex(
88             [r'albummid:\'([0-9a-zA-Z]+)\'', r'"albummid":"([0-9a-zA-Z]+)"'],
89             detail_info_page, 'album mid', default=None)
90         if albummid:
91             thumbnail_url = "http://i.gtimg.cn/music/photo/mid_album_500/%s/%s/%s.jpg" \
92                             % (albummid[-2:-1], albummid[-1], albummid)
93
94         guid = self.m_r_get_ruin()
95
96         vkey = self._download_json(
97             'http://base.music.qq.com/fcgi-bin/fcg_musicexpress.fcg?json=3&guid=%s' % guid,
98             mid, note='Retrieve vkey', errnote='Unable to get vkey',
99             transform_source=strip_jsonp)['key']
100
101         formats = []
102         for format_id, details in self._FORMATS.items():
103             formats.append({
104                 'url': 'http://cc.stream.qqmusic.qq.com/%s%s.%s?vkey=%s&guid=%s&fromtag=0'
105                        % (details['prefix'], mid, details['ext'], vkey, guid),
106                 'format': format_id,
107                 'format_id': format_id,
108                 'preference': details['preference'],
109                 'abr': details.get('abr'),
110             })
111         self._check_formats(formats, mid)
112         self._sort_formats(formats)
113
114         return {
115             'id': mid,
116             'formats': formats,
117             'title': song_name,
118             'upload_date': publish_time,
119             'creator': singer,
120             'description': lrc_content,
121             'thumbnail': thumbnail_url,
122         }
123
124
125 class QQPlaylistBaseIE(InfoExtractor):
126     @staticmethod
127     def qq_static_url(category, mid):
128         return 'http://y.qq.com/y/static/%s/%s/%s/%s.html' % (category, mid[-2], mid[-1], mid)
129
130     @classmethod
131     def get_entries_from_page(cls, page):
132         entries = []
133
134         for item in re.findall(r'class="data"[^<>]*>([^<>]+)</', page):
135             song_mid = unescapeHTML(item).split('|')[-5]
136             entries.append(cls.url_result(
137                 'http://y.qq.com/#type=song&mid=' + song_mid, 'QQMusic',
138                 song_mid))
139
140         return entries
141
142
143 class QQMusicSingerIE(QQPlaylistBaseIE):
144     IE_NAME = 'qqmusic:singer'
145     _VALID_URL = r'http://y.qq.com/#type=singer&mid=(?P<id>[0-9A-Za-z]+)'
146     _TEST = {
147         'url': 'http://y.qq.com/#type=singer&mid=001BLpXF2DyJe2',
148         'info_dict': {
149             'id': '001BLpXF2DyJe2',
150             'title': '林俊杰',
151             'description': 'md5:2a222d89ba4455a3af19940c0481bb78',
152         },
153         'playlist_count': 12,
154     }
155
156     def _real_extract(self, url):
157         mid = self._match_id(url)
158
159         singer_page = self._download_webpage(
160             self.qq_static_url('singer', mid), mid, 'Download singer page')
161
162         entries = self.get_entries_from_page(singer_page)
163
164         singer_name = self._html_search_regex(
165             r"singername\s*:\s*'([^']+)'", singer_page, 'singer name',
166             default=None)
167
168         singer_id = self._html_search_regex(
169             r"singerid\s*:\s*'([0-9]+)'", singer_page, 'singer id',
170             default=None)
171
172         singer_desc = None
173
174         if singer_id:
175             req = compat_urllib_request.Request(
176                 'http://s.plcloud.music.qq.com/fcgi-bin/fcg_get_singer_desc.fcg?utf8=1&outCharset=utf-8&format=xml&singerid=%s' % singer_id)
177             req.add_header(
178                 'Referer', 'http://s.plcloud.music.qq.com/xhr_proxy_utf8.html')
179             singer_desc_page = self._download_xml(
180                 req, mid, 'Donwload singer description XML')
181
182             singer_desc = singer_desc_page.find('./data/info/desc').text
183
184         return self.playlist_result(entries, mid, singer_name, singer_desc)
185
186
187 class QQMusicAlbumIE(QQPlaylistBaseIE):
188     IE_NAME = 'qqmusic:album'
189     _VALID_URL = r'http://y.qq.com/#type=album&mid=(?P<id>[0-9A-Za-z]+)'
190
191     _TESTS = [{
192         'url': 'http://y.qq.com/#type=album&mid=000gXCTb2AhRR1',
193         'info_dict': {
194             'id': '000gXCTb2AhRR1',
195             'title': '我们都是这样长大的',
196             'description': 'md5:179c5dce203a5931970d306aa9607ea6',
197         },
198         'playlist_count': 4,
199     }, {
200         'url': 'http://y.qq.com/#type=album&mid=002Y5a3b3AlCu3',
201         'info_dict': {
202             'id': '002Y5a3b3AlCu3',
203             'title': '그리고...',
204             'description': 'md5:a48823755615508a95080e81b51ba729',
205         },
206         'playlist_count': 8,
207     }]
208
209     def _real_extract(self, url):
210         mid = self._match_id(url)
211
212         album = self._download_json(
213             'http://i.y.qq.com/v8/fcg-bin/fcg_v8_album_info_cp.fcg?albummid=%s&format=json' % mid,
214             mid, 'Download album page')['data']
215
216         entries = [
217             self.url_result(
218                 'http://y.qq.com/#type=song&mid=' + song['songmid'], 'QQMusic', song['songmid']
219             ) for song in album['list']
220         ]
221         album_name = album.get('name')
222         album_detail = album.get('desc')
223         if album_detail is not None:
224             album_detail = album_detail.strip()
225
226         return self.playlist_result(entries, mid, album_name, album_detail)
227
228
229 class QQMusicToplistIE(QQPlaylistBaseIE):
230     IE_NAME = 'qqmusic:toplist'
231     _VALID_URL = r'http://y\.qq\.com/#type=toplist&p=(?P<id>(top|global)_[0-9]+)'
232
233     _TESTS = [{
234         'url': 'http://y.qq.com/#type=toplist&p=global_123',
235         'info_dict': {
236             'id': 'global_123',
237             'title': '美国iTunes榜',
238         },
239         'playlist_count': 10,
240     }, {
241         'url': 'http://y.qq.com/#type=toplist&p=top_3',
242         'info_dict': {
243             'id': 'top_3',
244             'title': 'QQ音乐巅峰榜·欧美',
245             'description': 'QQ音乐巅峰榜·欧美根据用户收听行为自动生成,集结当下最流行的欧美新歌!:更新时间:每周四22点|统'
246                            '计周期:一周(上周四至本周三)|统计对象:三个月内发行的欧美歌曲|统计数量:100首|统计算法:根据'
247                            '歌曲在一周内的有效播放次数,由高到低取前100名(同一歌手最多允许5首歌曲同时上榜)|有效播放次数:'
248                            '登录用户完整播放一首歌曲,记为一次有效播放;同一用户收听同一首歌曲,每天记录为1次有效播放'
249         },
250         'playlist_count': 100,
251     }, {
252         'url': 'http://y.qq.com/#type=toplist&p=global_106',
253         'info_dict': {
254             'id': 'global_106',
255             'title': '韩国Mnet榜',
256         },
257         'playlist_count': 50,
258     }]
259
260     def _real_extract(self, url):
261         list_id = self._match_id(url)
262
263         list_type, num_id = list_id.split("_")
264
265         toplist_json = self._download_json(
266             'http://i.y.qq.com/v8/fcg-bin/fcg_v8_toplist_cp.fcg?type=%s&topid=%s&format=json'
267             % (list_type, num_id),
268             list_id, 'Download toplist page')
269
270         entries = [
271             self.url_result(
272                 'http://y.qq.com/#type=song&mid=' + song['data']['songmid'], 'QQMusic', song['data']['songmid']
273             ) for song in toplist_json['songlist']
274         ]
275
276         topinfo = toplist_json.get('topinfo', {})
277         list_name = topinfo.get('ListName')
278         list_description = topinfo.get('info')
279         return self.playlist_result(entries, list_id, list_name, list_description)
280
281
282 class QQMusicPlaylistIE(QQPlaylistBaseIE):
283     IE_NAME = 'qqmusic:playlist'
284     _VALID_URL = r'http://y\.qq\.com/#type=taoge&id=(?P<id>[0-9]+)'
285
286     _TEST = {
287         'url': 'http://y.qq.com/#type=taoge&id=3462654915',
288         'info_dict': {
289             'id': '3462654915',
290             'title': '韩国5月新歌精选下旬',
291             'description': 'md5:d2c9d758a96b9888cf4fe82f603121d4',
292         },
293         'playlist_count': 40,
294     }
295
296     def _real_extract(self, url):
297         list_id = self._match_id(url)
298
299         list_json = self._download_json(
300             'http://i.y.qq.com/qzone-music/fcg-bin/fcg_ucc_getcdinfo_byids_cp.fcg?type=1&json=1&utf8=1&onlysong=0&disstid=%s'
301             % list_id, list_id, 'Download list page',
302             transform_source=strip_jsonp)['cdlist'][0]
303
304         entries = [
305             self.url_result(
306                 'http://y.qq.com/#type=song&mid=' + song['songmid'], 'QQMusic', song['songmid']
307             ) for song in list_json['songlist']
308         ]
309
310         list_name = list_json.get('dissname')
311         list_description = clean_html(unescapeHTML(list_json.get('desc')))
312         return self.playlist_result(entries, list_id, list_name, list_description)