]>
Commit | Line | Data |
---|---|---|
5d98908b YCH |
1 | # coding: utf-8 |
2 | from __future__ import unicode_literals | |
3 | ||
a2043572 YCH |
4 | import random |
5 | import time | |
8afff9f8 | 6 | import re |
a2043572 | 7 | |
5d98908b | 8 | from .common import InfoExtractor |
5edea45f YCH |
9 | from ..utils import ( |
10 | strip_jsonp, | |
11 | unescapeHTML, | |
0d0d5d37 | 12 | clean_html, |
5edea45f | 13 | ) |
8afff9f8 | 14 | from ..compat import compat_urllib_request |
5d98908b | 15 | |
5d98908b YCH |
16 | |
17 | class QQMusicIE(InfoExtractor): | |
7ec676bb | 18 | IE_NAME = 'qqmusic' |
5d98908b YCH |
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', | |
55e5841f | 22 | 'md5': '9ce1c1c8445f561506d2e3cfb0255705', |
5d98908b YCH |
23 | 'info_dict': { |
24 | 'id': '004295Et37taLD', | |
55e5841f | 25 | 'ext': 'mp3', |
5d98908b YCH |
26 | 'title': '可惜没如果', |
27 | 'upload_date': '20141227', | |
28 | 'creator': '林俊杰', | |
b813d8ca | 29 | 'description': 'md5:d327722d0361576fde558f1ac68a7065', |
85a06486 | 30 | 'thumbnail': 're:^https?://.*\.jpg$', |
5e3915cb | 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', | |
85a06486 | 43 | 'thumbnail': 're:^https?://.*\.jpg$', |
5d98908b YCH |
44 | } |
45 | }] | |
46 | ||
55e5841f | 47 | _FORMATS = { |
8b8cde21 | 48 | 'mp3-320': {'prefix': 'M800', 'ext': 'mp3', 'preference': 40, 'abr': 320}, |
49 | 'mp3-128': {'prefix': 'M500', 'ext': 'mp3', 'preference': 30, 'abr': 128}, | |
55e5841f | 50 | 'm4a': {'prefix': 'C200', 'ext': 'm4a', 'preference': 10} |
51 | } | |
52 | ||
a2043572 YCH |
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 | ||
5d98908b YCH |
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, | |
8afff9f8 | 65 | mid, note='Download song detail info', |
c9a77969 | 66 | errnote='Unable to get song detail info', encoding='gbk') |
5d98908b YCH |
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, | |
a685ae51 YCH |
73 | 'publish time', default=None) |
74 | if publish_time: | |
75 | publish_time = publish_time.replace('-', '') | |
5d98908b YCH |
76 | |
77 | singer = self._html_search_regex( | |
a685ae51 YCH |
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) | |
b813d8ca YCH |
83 | if lrc_content: |
84 | lrc_content = lrc_content.replace('\\n', '\n') | |
5d98908b | 85 | |
5e3915cb | 86 | thumbnail_url = None |
87 | albummid = self._search_regex( | |
0392ac98 | 88 | [r'albummid:\'([0-9a-zA-Z]+)\'', r'"albummid":"([0-9a-zA-Z]+)"'], |
89 | detail_info_page, 'album mid', default=None) | |
5e3915cb | 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 | ||
a2043572 YCH |
94 | guid = self.m_r_get_ruin() |
95 | ||
5d98908b YCH |
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'] | |
55e5841f | 100 | |
101 | formats = [] | |
e8ac61e8 | 102 | for format_id, details in self._FORMATS.items(): |
55e5841f | 103 | formats.append({ |
104 | 'url': 'http://cc.stream.qqmusic.qq.com/%s%s.%s?vkey=%s&guid=%s&fromtag=0' | |
e8ac61e8 YCH |
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'), | |
55e5841f | 110 | }) |
4d58b24c | 111 | self._check_formats(formats, mid) |
55e5841f | 112 | self._sort_formats(formats) |
5d98908b YCH |
113 | |
114 | return { | |
115 | 'id': mid, | |
55e5841f | 116 | 'formats': formats, |
5d98908b YCH |
117 | 'title': song_name, |
118 | 'upload_date': publish_time, | |
119 | 'creator': singer, | |
a685ae51 | 120 | 'description': lrc_content, |
5e3915cb | 121 | 'thumbnail': thumbnail_url, |
5d98908b | 122 | } |
8afff9f8 YCH |
123 | |
124 | ||
5edea45f YCH |
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 | ||
5edea45f YCH |
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( | |
a685ae51 YCH |
137 | 'http://y.qq.com/#type=song&mid=' + song_mid, 'QQMusic', |
138 | song_mid)) | |
5edea45f YCH |
139 | |
140 | return entries | |
141 | ||
142 | ||
143 | class QQMusicSingerIE(QQPlaylistBaseIE): | |
7ec676bb | 144 | IE_NAME = 'qqmusic:singer' |
8afff9f8 YCH |
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( | |
5edea45f | 160 | self.qq_static_url('singer', mid), mid, 'Download singer page') |
8afff9f8 | 161 | |
5edea45f | 162 | entries = self.get_entries_from_page(singer_page) |
8afff9f8 YCH |
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( | |
5edea45f | 180 | req, mid, 'Donwload singer description XML') |
8afff9f8 YCH |
181 | |
182 | singer_desc = singer_desc_page.find('./data/info/desc').text | |
183 | ||
184 | return self.playlist_result(entries, mid, singer_name, singer_desc) | |
5edea45f YCH |
185 | |
186 | ||
187 | class QQMusicAlbumIE(QQPlaylistBaseIE): | |
7ec676bb | 188 | IE_NAME = 'qqmusic:album' |
5edea45f YCH |
189 | _VALID_URL = r'http://y.qq.com/#type=album&mid=(?P<id>[0-9A-Za-z]+)' |
190 | ||
29b809de | 191 | _TESTS = [{ |
192 | 'url': 'http://y.qq.com/#type=album&mid=000gXCTb2AhRR1', | |
5edea45f YCH |
193 | 'info_dict': { |
194 | 'id': '000gXCTb2AhRR1', | |
195 | 'title': '我们都是这样长大的', | |
fc7ae675 | 196 | 'description': 'md5:179c5dce203a5931970d306aa9607ea6', |
5edea45f YCH |
197 | }, |
198 | 'playlist_count': 4, | |
29b809de | 199 | }, { |
200 | 'url': 'http://y.qq.com/#type=album&mid=002Y5a3b3AlCu3', | |
201 | 'info_dict': { | |
202 | 'id': '002Y5a3b3AlCu3', | |
203 | 'title': '그리고...', | |
fc7ae675 | 204 | 'description': 'md5:a48823755615508a95080e81b51ba729', |
29b809de | 205 | }, |
206 | 'playlist_count': 8, | |
207 | }] | |
5edea45f YCH |
208 | |
209 | def _real_extract(self, url): | |
210 | mid = self._match_id(url) | |
211 | ||
29b809de | 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 | ] | |
dfc4eca2 | 221 | album_name = album.get('name') |
29b809de | 222 | album_detail = album.get('desc') |
fc7ae675 YCH |
223 | if album_detail is not None: |
224 | album_detail = album_detail.strip() | |
5edea45f YCH |
225 | |
226 | return self.playlist_result(entries, mid, album_name, album_detail) | |
41333b97 | 227 | |
228 | ||
229 | class QQMusicToplistIE(QQPlaylistBaseIE): | |
7ec676bb | 230 | IE_NAME = 'qqmusic:toplist' |
41333b97 | 231 | _VALID_URL = r'http://y\.qq\.com/#type=toplist&p=(?P<id>(top|global)_[0-9]+)' |
54889739 | 232 | |
41333b97 | 233 | _TESTS = [{ |
eedda32e | 234 | 'url': 'http://y.qq.com/#type=toplist&p=global_123', |
41333b97 | 235 | 'info_dict': { |
eedda32e | 236 | 'id': 'global_123', |
237 | 'title': '美国iTunes榜', | |
41333b97 | 238 | }, |
239 | 'playlist_count': 10, | |
240 | }, { | |
eedda32e | 241 | 'url': 'http://y.qq.com/#type=toplist&p=top_3', |
41333b97 | 242 | 'info_dict': { |
eedda32e | 243 | 'id': 'top_3', |
41333b97 | 244 | 'title': 'QQ音乐巅峰榜·欧美', |
eedda32e | 245 | 'description': 'QQ音乐巅峰榜·欧美根据用户收听行为自动生成,集结当下最流行的欧美新歌!:更新时间:每周四22点|统' |
246 | '计周期:一周(上周四至本周三)|统计对象:三个月内发行的欧美歌曲|统计数量:100首|统计算法:根据' | |
247 | '歌曲在一周内的有效播放次数,由高到低取前100名(同一歌手最多允许5首歌曲同时上榜)|有效播放次数:' | |
248 | '登录用户完整播放一首歌曲,记为一次有效播放;同一用户收听同一首歌曲,每天记录为1次有效播放' | |
41333b97 | 249 | }, |
250 | 'playlist_count': 100, | |
fd4eefed | 251 | }, { |
eedda32e | 252 | 'url': 'http://y.qq.com/#type=toplist&p=global_106', |
fd4eefed | 253 | 'info_dict': { |
eedda32e | 254 | 'id': 'global_106', |
255 | 'title': '韩国Mnet榜', | |
fd4eefed | 256 | }, |
257 | 'playlist_count': 50, | |
41333b97 | 258 | }] |
259 | ||
41333b97 | 260 | def _real_extract(self, url): |
261 | list_id = self._match_id(url) | |
262 | ||
29ea5728 | 263 | list_type, num_id = list_id.split("_") |
41333b97 | 264 | |
29ea5728 | 265 | toplist_json = self._download_json( |
eedda32e | 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') | |
41333b97 | 269 | |
eedda32e | 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 | ] | |
41333b97 | 275 | |
9d4f213f YCH |
276 | topinfo = toplist_json.get('topinfo', {}) |
277 | list_name = topinfo.get('ListName') | |
278 | list_description = topinfo.get('info') | |
eedda32e | 279 | return self.playlist_result(entries, list_id, list_name, list_description) |
0d0d5d37 | 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 | ||
e9d33454 | 310 | list_name = list_json.get('dissname') |
0d0d5d37 | 311 | list_description = clean_html(unescapeHTML(list_json.get('desc'))) |
312 | return self.playlist_result(entries, list_id, list_name, list_description) |