qqmusic.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import random
  4. import time
  5. import re
  6. from .common import InfoExtractor
  7. from ..utils import (
  8. strip_jsonp,
  9. unescapeHTML,
  10. js_to_json,
  11. )
  12. from ..compat import compat_urllib_request
  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. 'description': 'md5:4348ff1dd24036906baa7b6f973f8d30',
  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. def _real_extract(self, url):
  34. mid = self._match_id(url)
  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 song detail info',
  38. errnote='Unable to get song detail info', encoding='gbk')
  39. song_name = self._html_search_regex(
  40. r"songname:\s*'([^']+)'", detail_info_page, 'song name')
  41. publish_time = self._html_search_regex(
  42. r'发行时间:(\d{4}-\d{2}-\d{2})', detail_info_page,
  43. 'publish time', default=None)
  44. if publish_time:
  45. publish_time = publish_time.replace('-', '')
  46. singer = self._html_search_regex(
  47. r"singer:\s*'([^']+)", detail_info_page, 'singer', default=None)
  48. lrc_content = self._html_search_regex(
  49. r'<div class="content" id="lrc_content"[^<>]*>([^<>]+)</div>',
  50. detail_info_page, 'LRC lyrics', default=None)
  51. guid = self.m_r_get_ruin()
  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. return {
  58. 'id': mid,
  59. 'url': song_url,
  60. 'title': song_name,
  61. 'upload_date': publish_time,
  62. 'creator': singer,
  63. 'description': lrc_content,
  64. }
  65. class QQPlaylistBaseIE(InfoExtractor):
  66. @staticmethod
  67. def qq_static_url(category, mid):
  68. return 'http://y.qq.com/y/static/%s/%s/%s/%s.html' % (category, mid[-2], mid[-1], mid)
  69. @classmethod
  70. def get_entries_from_page(cls, page):
  71. entries = []
  72. for item in re.findall(r'class="data"[^<>]*>([^<>]+)</', page):
  73. song_mid = unescapeHTML(item).split('|')[-5]
  74. entries.append(cls.url_result(
  75. 'http://y.qq.com/#type=song&mid=' + song_mid, 'QQMusic',
  76. song_mid))
  77. return entries
  78. class QQMusicSingerIE(QQPlaylistBaseIE):
  79. _VALID_URL = r'http://y.qq.com/#type=singer&mid=(?P<id>[0-9A-Za-z]+)'
  80. _TEST = {
  81. 'url': 'http://y.qq.com/#type=singer&mid=001BLpXF2DyJe2',
  82. 'info_dict': {
  83. 'id': '001BLpXF2DyJe2',
  84. 'title': '林俊杰',
  85. 'description': 'md5:2a222d89ba4455a3af19940c0481bb78',
  86. },
  87. 'playlist_count': 12,
  88. }
  89. def _real_extract(self, url):
  90. mid = self._match_id(url)
  91. singer_page = self._download_webpage(
  92. self.qq_static_url('singer', mid), mid, 'Download singer page')
  93. entries = self.get_entries_from_page(singer_page)
  94. singer_name = self._html_search_regex(
  95. r"singername\s*:\s*'([^']+)'", singer_page, 'singer name',
  96. default=None)
  97. singer_id = self._html_search_regex(
  98. r"singerid\s*:\s*'([0-9]+)'", singer_page, 'singer id',
  99. default=None)
  100. singer_desc = None
  101. if singer_id:
  102. req = compat_urllib_request.Request(
  103. 'http://s.plcloud.music.qq.com/fcgi-bin/fcg_get_singer_desc.fcg?utf8=1&outCharset=utf-8&format=xml&singerid=%s' % singer_id)
  104. req.add_header(
  105. 'Referer', 'http://s.plcloud.music.qq.com/xhr_proxy_utf8.html')
  106. singer_desc_page = self._download_xml(
  107. req, mid, 'Donwload singer description XML')
  108. singer_desc = singer_desc_page.find('./data/info/desc').text
  109. return self.playlist_result(entries, mid, singer_name, singer_desc)
  110. class QQMusicAlbumIE(QQPlaylistBaseIE):
  111. _VALID_URL = r'http://y.qq.com/#type=album&mid=(?P<id>[0-9A-Za-z]+)'
  112. _TEST = {
  113. 'url': 'http://y.qq.com/#type=album&mid=000gXCTb2AhRR1&play=0',
  114. 'info_dict': {
  115. 'id': '000gXCTb2AhRR1',
  116. 'title': '我们都是这样长大的',
  117. 'description': 'md5:d216c55a2d4b3537fe4415b8767d74d6',
  118. },
  119. 'playlist_count': 4,
  120. }
  121. def _real_extract(self, url):
  122. mid = self._match_id(url)
  123. album_page = self._download_webpage(
  124. self.qq_static_url('album', mid), mid, 'Download album page')
  125. entries = self.get_entries_from_page(album_page)
  126. album_name = self._html_search_regex(
  127. r"albumname\s*:\s*'([^']+)',", album_page, 'album name',
  128. default=None)
  129. album_detail = self._html_search_regex(
  130. r'<div class="album_detail close_detail">\s*<p>((?:[^<>]+(?:<br />)?)+)</p>',
  131. album_page, 'album details', default=None)
  132. return self.playlist_result(entries, mid, album_name, album_detail)
  133. class QQMusicToplistIE(QQPlaylistBaseIE):
  134. _VALID_URL = r'http://y\.qq\.com/#type=toplist&p=(?P<id>(top|global)_[0-9]+)'
  135. _TESTS = [{
  136. 'url': 'http://y.qq.com/#type=toplist&p=global_12',
  137. 'info_dict': {
  138. 'id': 'global_12',
  139. 'title': 'itunes榜',
  140. },
  141. 'playlist_count': 10,
  142. }, {
  143. 'url': 'http://y.qq.com/#type=toplist&p=top_6',
  144. 'info_dict': {
  145. 'id': 'top_6',
  146. 'title': 'QQ音乐巅峰榜·欧美',
  147. },
  148. 'playlist_count': 100,
  149. }, {
  150. 'url': 'http://y.qq.com/#type=toplist&p=global_5',
  151. 'info_dict': {
  152. 'id': 'global_5',
  153. 'title': '韩国mnet排行榜',
  154. },
  155. 'playlist_count': 50,
  156. }]
  157. @staticmethod
  158. def strip_qq_jsonp(code):
  159. return js_to_json(re.sub(r'^MusicJsonCallback\((.*?)\)/\*.+?\*/$', r'\1', code))
  160. def _real_extract(self, url):
  161. list_id = self._match_id(url)
  162. list_type = list_id.split("_")[0]
  163. num_id = list_id.split("_")[1]
  164. list_page = self._download_webpage(
  165. "http://y.qq.com/y/static/toplist/index/%s.html" % list_id,
  166. list_id, 'Download toplist page')
  167. entries = []
  168. jsonp_url = ""
  169. if list_type == 'top':
  170. jsonp_url = "http://y.qq.com/y/static/toplist/json/top/%s/1.js" % num_id
  171. elif list_type == 'global':
  172. jsonp_url = "http://y.qq.com/y/static/toplist/json/global/%s/1_1.js" % num_id
  173. list = self._download_json(jsonp_url, list_id, note='Retrieve toplist json',
  174. errnote='Unable to get toplist json', transform_source=self.strip_qq_jsonp)
  175. for song in list['l']:
  176. s = song['s']
  177. song_mid = s.split("|")[20]
  178. entries.append(self.url_result(
  179. 'http://y.qq.com/#type=song&mid=' + song_mid, 'QQMusic',
  180. song_mid))
  181. list_name = self._html_search_regex(
  182. r'<h2 id="top_name">([^\']+)</h2>', list_page, 'top list name',
  183. default=None)
  184. list_desc = None
  185. return self.playlist_result(entries, list_id, list_name, list_desc)