[QQMusic] Add singer info extractor
[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 strip_jsonp
10 from ..compat import compat_urllib_request
11
12
13 class QQMusicIE(InfoExtractor):
14     _VALID_URL = r'http://y.qq.com/#type=song&mid=(?P<id>[0-9A-Za-z]+)'
15     _TESTS = [{
16         'url': 'http://y.qq.com/#type=song&mid=004295Et37taLD',
17         'md5': 'bed90b6db2a7a7a7e11bc585f471f63a',
18         'info_dict': {
19             'id': '004295Et37taLD',
20             'ext': 'm4a',
21             'title': '可惜没如果',
22             'upload_date': '20141227',
23             'creator': '林俊杰',
24         }
25     }]
26
27     # Reference: m_r_GetRUin() in top_player.js
28     # http://imgcache.gtimg.cn/music/portal_v3/y/top_player.js
29     @staticmethod
30     def m_r_get_ruin():
31         curMs = int(time.time() * 1000) % 1000
32         return int(round(random.random() * 2147483647) * curMs % 1E10)
33
34     def _real_extract(self, url):
35         mid = self._match_id(url)
36
37         detail_info_page = self._download_webpage(
38             'http://s.plcloud.music.qq.com/fcgi-bin/fcg_yqq_song_detail_info.fcg?songmid=%s&play=0' % mid,
39             mid, note='Download song detail info',
40             errnote='Unable to get song detail info')
41
42         song_name = self._html_search_regex(
43             r"songname:\s*'([^']+)'", detail_info_page, 'song name')
44
45         publish_time = self._html_search_regex(
46             r'发行时间:(\d{4}-\d{2}-\d{2})', detail_info_page,
47             'publish time').replace('-', '')
48
49         singer = self._html_search_regex(
50             r"singer:\s*'([^']+)", detail_info_page, 'singer')
51
52         guid = self.m_r_get_ruin()
53
54         vkey = self._download_json(
55             'http://base.music.qq.com/fcgi-bin/fcg_musicexpress.fcg?json=3&guid=%s' % guid,
56             mid, note='Retrieve vkey', errnote='Unable to get vkey',
57             transform_source=strip_jsonp)['key']
58         song_url = 'http://cc.stream.qqmusic.qq.com/C200%s.m4a?vkey=%s&guid=%s&fromtag=0' % (mid, vkey, guid)
59
60         return {
61             'id': mid,
62             'url': song_url,
63             'title': song_name,
64             'upload_date': publish_time,
65             'creator': singer,
66         }
67
68
69 class QQMusicSingerIE(InfoExtractor):
70     _VALID_URL = r'http://y.qq.com/#type=singer&mid=(?P<id>[0-9A-Za-z]+)'
71     _TEST = {
72         'url': 'http://y.qq.com/#type=singer&mid=001BLpXF2DyJe2',
73         'info_dict': {
74             'id': '001BLpXF2DyJe2',
75             'title': '林俊杰',
76             'description': 'md5:2a222d89ba4455a3af19940c0481bb78',
77         },
78         'playlist_count': 12,
79     }
80
81     def _real_extract(self, url):
82         mid = self._match_id(url)
83
84         singer_page = self._download_webpage(
85             'http://y.qq.com/y/static/singer/%s/%s/%s.html' % (mid[-2], mid[-1], mid),
86             'Download singer page')
87
88         entries = []
89
90         for item in re.findall(r'<span class="data">([^<>]+)</span>', singer_page):
91             song_mid = item.split('|')[-5]
92             entries.append(self.url_result(
93                 'http://y.qq.com/#type=song&mid=' + song_mid, 'QQMusic', song_mid))
94
95         singer_name = self._html_search_regex(
96             r"singername\s*:\s*'([^']+)'", singer_page, 'singer name',
97             default=None)
98
99         singer_id = self._html_search_regex(
100             r"singerid\s*:\s*'([0-9]+)'", singer_page, 'singer id',
101             default=None)
102
103         singer_desc = None
104
105         if singer_id:
106             req = compat_urllib_request.Request(
107                 'http://s.plcloud.music.qq.com/fcgi-bin/fcg_get_singer_desc.fcg?utf8=1&outCharset=utf-8&format=xml&singerid=%s' % singer_id)
108             req.add_header(
109                 'Referer', 'http://s.plcloud.music.qq.com/xhr_proxy_utf8.html')
110             singer_desc_page = self._download_xml(
111                 req, 'Donwload singer description XML')
112
113             singer_desc = singer_desc_page.find('./data/info/desc').text
114
115         return self.playlist_result(entries, mid, singer_name, singer_desc)