[QQMusic] Implement the guid algorithm
[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
7 from .common import InfoExtractor
8 from ..utils import strip_jsonp
9
10
11 class QQMusicIE(InfoExtractor):
12     _VALID_URL = r'http://y.qq.com/#type=song&mid=(?P<id>[0-9A-Za-z]+)'
13     _TESTS = [{
14         'url': 'http://y.qq.com/#type=song&mid=004295Et37taLD',
15         'md5': 'bed90b6db2a7a7a7e11bc585f471f63a',
16         'info_dict': {
17             'id': '004295Et37taLD',
18             'ext': 'm4a',
19             'title': '可惜没如果',
20             'upload_date': '20141227',
21             'creator': '林俊杰',
22         }
23     }]
24
25     # Reference: m_r_GetRUin() in top_player.js
26     # http://imgcache.gtimg.cn/music/portal_v3/y/top_player.js
27     @staticmethod
28     def m_r_get_ruin():
29         curMs = int(time.time() * 1000) % 1000
30         return int(round(random.random() * 2147483647) * curMs % 1E10)
31
32     def _real_extract(self, url):
33         mid = self._match_id(url)
34
35         detail_info_page = self._download_webpage(
36             'http://s.plcloud.music.qq.com/fcgi-bin/fcg_yqq_song_detail_info.fcg?songmid=%s&play=0' % mid,
37             mid, note='Download sont detail info',
38             errnote='Unable to get song detail info')
39
40         song_name = self._html_search_regex(
41             r"songname:\s*'([^']+)'", detail_info_page, 'song name')
42
43         publish_time = self._html_search_regex(
44             r'发行时间:(\d{4}-\d{2}-\d{2})', detail_info_page,
45             'publish time').replace('-', '')
46
47         singer = self._html_search_regex(
48             r"singer:\s*'([^']+)", detail_info_page, 'singer')
49
50         guid = self.m_r_get_ruin()
51
52         vkey = self._download_json(
53             'http://base.music.qq.com/fcgi-bin/fcg_musicexpress.fcg?json=3&guid=%s' % guid,
54             mid, note='Retrieve vkey', errnote='Unable to get vkey',
55             transform_source=strip_jsonp)['key']
56         song_url = 'http://cc.stream.qqmusic.qq.com/C200%s.m4a?vkey=%s&guid=%s&fromtag=0' % (mid, vkey, guid)
57
58         return {
59             'id': mid,
60             'url': song_url,
61             'title': song_name,
62             'upload_date': publish_time,
63             'creator': singer,
64         }