qqmusic.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import random
  4. import time
  5. from .common import InfoExtractor
  6. from ..utils import strip_jsonp
  7. class QQMusicIE(InfoExtractor):
  8. _VALID_URL = r'http://y.qq.com/#type=song&mid=(?P<id>[0-9A-Za-z]+)'
  9. _TESTS = [{
  10. 'url': 'http://y.qq.com/#type=song&mid=004295Et37taLD',
  11. 'md5': 'bed90b6db2a7a7a7e11bc585f471f63a',
  12. 'info_dict': {
  13. 'id': '004295Et37taLD',
  14. 'ext': 'm4a',
  15. 'title': '可惜没如果',
  16. 'upload_date': '20141227',
  17. 'creator': '林俊杰',
  18. }
  19. }]
  20. # Reference: m_r_GetRUin() in top_player.js
  21. # http://imgcache.gtimg.cn/music/portal_v3/y/top_player.js
  22. @staticmethod
  23. def m_r_get_ruin():
  24. curMs = int(time.time() * 1000) % 1000
  25. return int(round(random.random() * 2147483647) * curMs % 1E10)
  26. def _real_extract(self, url):
  27. mid = self._match_id(url)
  28. detail_info_page = self._download_webpage(
  29. 'http://s.plcloud.music.qq.com/fcgi-bin/fcg_yqq_song_detail_info.fcg?songmid=%s&play=0' % mid,
  30. mid, note='Download sont detail info',
  31. errnote='Unable to get song detail info')
  32. song_name = self._html_search_regex(
  33. r"songname:\s*'([^']+)'", detail_info_page, 'song name')
  34. publish_time = self._html_search_regex(
  35. r'发行时间:(\d{4}-\d{2}-\d{2})', detail_info_page,
  36. 'publish time').replace('-', '')
  37. singer = self._html_search_regex(
  38. r"singer:\s*'([^']+)", detail_info_page, 'singer')
  39. guid = self.m_r_get_ruin()
  40. vkey = self._download_json(
  41. 'http://base.music.qq.com/fcgi-bin/fcg_musicexpress.fcg?json=3&guid=%s' % guid,
  42. mid, note='Retrieve vkey', errnote='Unable to get vkey',
  43. transform_source=strip_jsonp)['key']
  44. song_url = 'http://cc.stream.qqmusic.qq.com/C200%s.m4a?vkey=%s&guid=%s&fromtag=0' % (mid, vkey, guid)
  45. return {
  46. 'id': mid,
  47. 'url': song_url,
  48. 'title': song_name,
  49. 'upload_date': publish_time,
  50. 'creator': singer,
  51. }