qqmusic.py 5.4 KB

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