[QQMusic] Add new extractor
[youtube-dl] / youtube_dl / extractor / qqmusic.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 from .common import InfoExtractor
5 from ..utils import strip_jsonp
6
7 # guid is a random number generated in javascript, but seems a fixed number
8 # also works
9 guid = '1'
10
11
12 class QQMusicIE(InfoExtractor):
13     _VALID_URL = r'http://y.qq.com/#type=song&mid=(?P<id>[0-9A-Za-z]+)'
14     _TESTS = [{
15         'url': 'http://y.qq.com/#type=song&mid=004295Et37taLD',
16         'md5': 'bed90b6db2a7a7a7e11bc585f471f63a',
17         'info_dict': {
18             'id': '004295Et37taLD',
19             'ext': 'm4a',
20             'title': '可惜没如果',
21             'upload_date': '20141227',
22             'creator': '林俊杰',
23         }
24     }]
25
26     def _real_extract(self, url):
27         mid = self._match_id(url)
28
29         detail_info_page = self._download_webpage(
30             'http://s.plcloud.music.qq.com/fcgi-bin/fcg_yqq_song_detail_info.fcg?songmid=%s&play=0' % mid,
31             mid, note='Download sont detail info',
32             errnote='Unable to get song detail info')
33
34         song_name = self._html_search_regex(
35             r"songname:\s*'([^']+)'", detail_info_page, 'song name')
36
37         publish_time = self._html_search_regex(
38             r'发行时间:(\d{4}-\d{2}-\d{2})', detail_info_page,
39             'publish time').replace('-', '')
40
41         singer = self._html_search_regex(
42             r"singer:\s*'([^']+)", detail_info_page, 'singer')
43
44         vkey = self._download_json(
45             'http://base.music.qq.com/fcgi-bin/fcg_musicexpress.fcg?json=3&guid=%s' % guid,
46             mid, note='Retrieve vkey', errnote='Unable to get vkey',
47             transform_source=strip_jsonp)['key']
48         song_url = 'http://cc.stream.qqmusic.qq.com/C200%s.m4a?vkey=%s&guid=%s&fromtag=0' % (mid, vkey, guid)
49
50         return {
51             'id': mid,
52             'url': song_url,
53             'title': song_name,
54             'upload_date': publish_time,
55             'creator': singer,
56         }