You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

170 lines
5.7 KiB

  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. )
  11. from ..compat import compat_urllib_request
  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. 'description': 'md5:4348ff1dd24036906baa7b6f973f8d30',
  24. }
  25. }]
  26. # Reference: m_r_GetRUin() in top_player.js
  27. # http://imgcache.gtimg.cn/music/portal_v3/y/top_player.js
  28. @staticmethod
  29. def m_r_get_ruin():
  30. curMs = int(time.time() * 1000) % 1000
  31. return int(round(random.random() * 2147483647) * curMs % 1E10)
  32. def _real_extract(self, url):
  33. mid = self._match_id(url)
  34. detail_info_page = self._download_webpage(
  35. 'http://s.plcloud.music.qq.com/fcgi-bin/fcg_yqq_song_detail_info.fcg?songmid=%s&play=0' % mid,
  36. mid, note='Download song detail info',
  37. errnote='Unable to get song detail info', encoding='gbk')
  38. song_name = self._html_search_regex(
  39. r"songname:\s*'([^']+)'", detail_info_page, 'song name')
  40. publish_time = self._html_search_regex(
  41. r'发行时间:(\d{4}-\d{2}-\d{2})', detail_info_page,
  42. 'publish time', default=None)
  43. if publish_time:
  44. publish_time = publish_time.replace('-', '')
  45. singer = self._html_search_regex(
  46. r"singer:\s*'([^']+)", detail_info_page, 'singer', default=None)
  47. lrc_content = self._html_search_regex(
  48. r'<div class="content" id="lrc_content"[^<>]*>([^<>]+)</div>',
  49. detail_info_page, 'LRC lyrics', default=None)
  50. guid = self.m_r_get_ruin()
  51. vkey = self._download_json(
  52. 'http://base.music.qq.com/fcgi-bin/fcg_musicexpress.fcg?json=3&guid=%s' % guid,
  53. mid, note='Retrieve vkey', errnote='Unable to get vkey',
  54. transform_source=strip_jsonp)['key']
  55. song_url = 'http://cc.stream.qqmusic.qq.com/C200%s.m4a?vkey=%s&guid=%s&fromtag=0' % (mid, vkey, guid)
  56. return {
  57. 'id': mid,
  58. 'url': song_url,
  59. 'title': song_name,
  60. 'upload_date': publish_time,
  61. 'creator': singer,
  62. 'description': lrc_content,
  63. }
  64. class QQPlaylistBaseIE(InfoExtractor):
  65. @staticmethod
  66. def qq_static_url(category, mid):
  67. return 'http://y.qq.com/y/static/%s/%s/%s/%s.html' % (category, mid[-2], mid[-1], mid)
  68. @classmethod
  69. def get_entries_from_page(cls, page):
  70. entries = []
  71. for item in re.findall(r'class="data"[^<>]*>([^<>]+)</', page):
  72. song_mid = unescapeHTML(item).split('|')[-5]
  73. entries.append(cls.url_result(
  74. 'http://y.qq.com/#type=song&mid=' + song_mid, 'QQMusic',
  75. song_mid))
  76. return entries
  77. class QQMusicSingerIE(QQPlaylistBaseIE):
  78. _VALID_URL = r'http://y.qq.com/#type=singer&mid=(?P<id>[0-9A-Za-z]+)'
  79. _TEST = {
  80. 'url': 'http://y.qq.com/#type=singer&mid=001BLpXF2DyJe2',
  81. 'info_dict': {
  82. 'id': '001BLpXF2DyJe2',
  83. 'title': '林俊杰',
  84. 'description': 'md5:2a222d89ba4455a3af19940c0481bb78',
  85. },
  86. 'playlist_count': 12,
  87. }
  88. def _real_extract(self, url):
  89. mid = self._match_id(url)
  90. singer_page = self._download_webpage(
  91. self.qq_static_url('singer', mid), mid, 'Download singer page')
  92. entries = self.get_entries_from_page(singer_page)
  93. singer_name = self._html_search_regex(
  94. r"singername\s*:\s*'([^']+)'", singer_page, 'singer name',
  95. default=None)
  96. singer_id = self._html_search_regex(
  97. r"singerid\s*:\s*'([0-9]+)'", singer_page, 'singer id',
  98. default=None)
  99. singer_desc = None
  100. if singer_id:
  101. req = compat_urllib_request.Request(
  102. 'http://s.plcloud.music.qq.com/fcgi-bin/fcg_get_singer_desc.fcg?utf8=1&outCharset=utf-8&format=xml&singerid=%s' % singer_id)
  103. req.add_header(
  104. 'Referer', 'http://s.plcloud.music.qq.com/xhr_proxy_utf8.html')
  105. singer_desc_page = self._download_xml(
  106. req, mid, 'Donwload singer description XML')
  107. singer_desc = singer_desc_page.find('./data/info/desc').text
  108. return self.playlist_result(entries, mid, singer_name, singer_desc)
  109. class QQMusicAlbumIE(QQPlaylistBaseIE):
  110. _VALID_URL = r'http://y.qq.com/#type=album&mid=(?P<id>[0-9A-Za-z]+)'
  111. _TEST = {
  112. 'url': 'http://y.qq.com/#type=album&mid=000gXCTb2AhRR1&play=0',
  113. 'info_dict': {
  114. 'id': '000gXCTb2AhRR1',
  115. 'title': '我们都是这样长大的',
  116. 'description': 'md5:d216c55a2d4b3537fe4415b8767d74d6',
  117. },
  118. 'playlist_count': 4,
  119. }
  120. def _real_extract(self, url):
  121. mid = self._match_id(url)
  122. album_page = self._download_webpage(
  123. self.qq_static_url('album', mid), mid, 'Download album page')
  124. entries = self.get_entries_from_page(album_page)
  125. album_name = self._html_search_regex(
  126. r"albumname\s*:\s*'([^']+)',", album_page, 'album name',
  127. default=None)
  128. album_detail = self._html_search_regex(
  129. r'<div class="album_detail close_detail">\s*<p>((?:[^<>]+(?:<br />)?)+)</p>',
  130. album_page, 'album details', default=None)
  131. return self.playlist_result(entries, mid, album_name, album_detail)