qqmusic.py 3.9 KB

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