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.

344 lines
12 KiB

10 years ago
  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. clean_html,
  11. )
  12. from ..compat import compat_urllib_request
  13. class QQMusicIE(InfoExtractor):
  14. IE_NAME = 'qqmusic'
  15. IE_DESC = 'QQ音乐'
  16. _VALID_URL = r'http://y.qq.com/#type=song&mid=(?P<id>[0-9A-Za-z]+)'
  17. _TESTS = [{
  18. 'url': 'http://y.qq.com/#type=song&mid=004295Et37taLD',
  19. 'md5': '9ce1c1c8445f561506d2e3cfb0255705',
  20. 'info_dict': {
  21. 'id': '004295Et37taLD',
  22. 'ext': 'mp3',
  23. 'title': '可惜没如果',
  24. 'release_date': '20141227',
  25. 'creator': '林俊杰',
  26. 'description': 'md5:d327722d0361576fde558f1ac68a7065',
  27. 'thumbnail': 're:^https?://.*\.jpg$',
  28. }
  29. }, {
  30. 'note': 'There is no mp3-320 version of this song.',
  31. 'url': 'http://y.qq.com/#type=song&mid=004MsGEo3DdNxV',
  32. 'md5': 'fa3926f0c585cda0af8fa4f796482e3e',
  33. 'info_dict': {
  34. 'id': '004MsGEo3DdNxV',
  35. 'ext': 'mp3',
  36. 'title': '如果',
  37. 'release_date': '20050626',
  38. 'creator': '李季美',
  39. 'description': 'md5:46857d5ed62bc4ba84607a805dccf437',
  40. 'thumbnail': 're:^https?://.*\.jpg$',
  41. }
  42. }, {
  43. 'note': 'lyrics not in .lrc format',
  44. 'url': 'http://y.qq.com/#type=song&mid=001JyApY11tIp6',
  45. 'info_dict': {
  46. 'id': '001JyApY11tIp6',
  47. 'ext': 'mp3',
  48. 'title': 'Shadows Over Transylvania',
  49. 'release_date': '19970225',
  50. 'creator': 'Dark Funeral',
  51. 'description': 'md5:ed14d5bd7ecec19609108052c25b2c11',
  52. 'thumbnail': 're:^https?://.*\.jpg$',
  53. },
  54. 'params': {
  55. 'skip_download': True,
  56. },
  57. }]
  58. _FORMATS = {
  59. 'mp3-320': {'prefix': 'M800', 'ext': 'mp3', 'preference': 40, 'abr': 320},
  60. 'mp3-128': {'prefix': 'M500', 'ext': 'mp3', 'preference': 30, 'abr': 128},
  61. 'm4a': {'prefix': 'C200', 'ext': 'm4a', 'preference': 10}
  62. }
  63. # Reference: m_r_GetRUin() in top_player.js
  64. # http://imgcache.gtimg.cn/music/portal_v3/y/top_player.js
  65. @staticmethod
  66. def m_r_get_ruin():
  67. curMs = int(time.time() * 1000) % 1000
  68. return int(round(random.random() * 2147483647) * curMs % 1E10)
  69. def _real_extract(self, url):
  70. mid = self._match_id(url)
  71. detail_info_page = self._download_webpage(
  72. 'http://s.plcloud.music.qq.com/fcgi-bin/fcg_yqq_song_detail_info.fcg?songmid=%s&play=0' % mid,
  73. mid, note='Download song detail info',
  74. errnote='Unable to get song detail info', encoding='gbk')
  75. song_name = self._html_search_regex(
  76. r"songname:\s*'([^']+)'", detail_info_page, 'song name')
  77. publish_time = self._html_search_regex(
  78. r'发行时间:(\d{4}-\d{2}-\d{2})', detail_info_page,
  79. 'publish time', default=None)
  80. if publish_time:
  81. publish_time = publish_time.replace('-', '')
  82. singer = self._html_search_regex(
  83. r"singer:\s*'([^']+)", detail_info_page, 'singer', default=None)
  84. lrc_content = self._html_search_regex(
  85. r'<div class="content" id="lrc_content"[^<>]*>([^<>]+)</div>',
  86. detail_info_page, 'LRC lyrics', default=None)
  87. if lrc_content:
  88. lrc_content = lrc_content.replace('\\n', '\n')
  89. thumbnail_url = None
  90. albummid = self._search_regex(
  91. [r'albummid:\'([0-9a-zA-Z]+)\'', r'"albummid":"([0-9a-zA-Z]+)"'],
  92. detail_info_page, 'album mid', default=None)
  93. if albummid:
  94. thumbnail_url = "http://i.gtimg.cn/music/photo/mid_album_500/%s/%s/%s.jpg" \
  95. % (albummid[-2:-1], albummid[-1], albummid)
  96. guid = self.m_r_get_ruin()
  97. vkey = self._download_json(
  98. 'http://base.music.qq.com/fcgi-bin/fcg_musicexpress.fcg?json=3&guid=%s' % guid,
  99. mid, note='Retrieve vkey', errnote='Unable to get vkey',
  100. transform_source=strip_jsonp)['key']
  101. formats = []
  102. for format_id, details in self._FORMATS.items():
  103. formats.append({
  104. 'url': 'http://cc.stream.qqmusic.qq.com/%s%s.%s?vkey=%s&guid=%s&fromtag=0'
  105. % (details['prefix'], mid, details['ext'], vkey, guid),
  106. 'format': format_id,
  107. 'format_id': format_id,
  108. 'preference': details['preference'],
  109. 'abr': details.get('abr'),
  110. })
  111. self._check_formats(formats, mid)
  112. self._sort_formats(formats)
  113. actual_lrc_lyrics = ''.join(
  114. line + '\n' for line in re.findall(
  115. r'(?m)^(\[[0-9]{2}:[0-9]{2}(?:\.[0-9]{2,})?\][^\n]*|\[[^\]]*\])', lrc_content))
  116. info_dict = {
  117. 'id': mid,
  118. 'formats': formats,
  119. 'title': song_name,
  120. 'release_date': publish_time,
  121. 'creator': singer,
  122. 'description': lrc_content,
  123. 'thumbnail': thumbnail_url
  124. }
  125. if actual_lrc_lyrics:
  126. info_dict['subtitles'] = {
  127. 'origin': [{
  128. 'ext': 'lrc',
  129. 'data': actual_lrc_lyrics,
  130. }]
  131. }
  132. return info_dict
  133. class QQPlaylistBaseIE(InfoExtractor):
  134. @staticmethod
  135. def qq_static_url(category, mid):
  136. return 'http://y.qq.com/y/static/%s/%s/%s/%s.html' % (category, mid[-2], mid[-1], mid)
  137. @classmethod
  138. def get_entries_from_page(cls, page):
  139. entries = []
  140. for item in re.findall(r'class="data"[^<>]*>([^<>]+)</', page):
  141. song_mid = unescapeHTML(item).split('|')[-5]
  142. entries.append(cls.url_result(
  143. 'http://y.qq.com/#type=song&mid=' + song_mid, 'QQMusic',
  144. song_mid))
  145. return entries
  146. class QQMusicSingerIE(QQPlaylistBaseIE):
  147. IE_NAME = 'qqmusic:singer'
  148. IE_DESC = 'QQ音乐 - 歌手'
  149. _VALID_URL = r'http://y.qq.com/#type=singer&mid=(?P<id>[0-9A-Za-z]+)'
  150. _TEST = {
  151. 'url': 'http://y.qq.com/#type=singer&mid=001BLpXF2DyJe2',
  152. 'info_dict': {
  153. 'id': '001BLpXF2DyJe2',
  154. 'title': '林俊杰',
  155. 'description': 'md5:2a222d89ba4455a3af19940c0481bb78',
  156. },
  157. 'playlist_count': 12,
  158. }
  159. def _real_extract(self, url):
  160. mid = self._match_id(url)
  161. singer_page = self._download_webpage(
  162. self.qq_static_url('singer', mid), mid, 'Download singer page')
  163. entries = self.get_entries_from_page(singer_page)
  164. singer_name = self._html_search_regex(
  165. r"singername\s*:\s*'([^']+)'", singer_page, 'singer name',
  166. default=None)
  167. singer_id = self._html_search_regex(
  168. r"singerid\s*:\s*'([0-9]+)'", singer_page, 'singer id',
  169. default=None)
  170. singer_desc = None
  171. if singer_id:
  172. req = compat_urllib_request.Request(
  173. 'http://s.plcloud.music.qq.com/fcgi-bin/fcg_get_singer_desc.fcg?utf8=1&outCharset=utf-8&format=xml&singerid=%s' % singer_id)
  174. req.add_header(
  175. 'Referer', 'http://s.plcloud.music.qq.com/xhr_proxy_utf8.html')
  176. singer_desc_page = self._download_xml(
  177. req, mid, 'Donwload singer description XML')
  178. singer_desc = singer_desc_page.find('./data/info/desc').text
  179. return self.playlist_result(entries, mid, singer_name, singer_desc)
  180. class QQMusicAlbumIE(QQPlaylistBaseIE):
  181. IE_NAME = 'qqmusic:album'
  182. IE_DESC = 'QQ音乐 - 专辑'
  183. _VALID_URL = r'http://y.qq.com/#type=album&mid=(?P<id>[0-9A-Za-z]+)'
  184. _TESTS = [{
  185. 'url': 'http://y.qq.com/#type=album&mid=000gXCTb2AhRR1',
  186. 'info_dict': {
  187. 'id': '000gXCTb2AhRR1',
  188. 'title': '我们都是这样长大的',
  189. 'description': 'md5:179c5dce203a5931970d306aa9607ea6',
  190. },
  191. 'playlist_count': 4,
  192. }, {
  193. 'url': 'http://y.qq.com/#type=album&mid=002Y5a3b3AlCu3',
  194. 'info_dict': {
  195. 'id': '002Y5a3b3AlCu3',
  196. 'title': '그리고...',
  197. 'description': 'md5:a48823755615508a95080e81b51ba729',
  198. },
  199. 'playlist_count': 8,
  200. }]
  201. def _real_extract(self, url):
  202. mid = self._match_id(url)
  203. album = self._download_json(
  204. 'http://i.y.qq.com/v8/fcg-bin/fcg_v8_album_info_cp.fcg?albummid=%s&format=json' % mid,
  205. mid, 'Download album page')['data']
  206. entries = [
  207. self.url_result(
  208. 'http://y.qq.com/#type=song&mid=' + song['songmid'], 'QQMusic', song['songmid']
  209. ) for song in album['list']
  210. ]
  211. album_name = album.get('name')
  212. album_detail = album.get('desc')
  213. if album_detail is not None:
  214. album_detail = album_detail.strip()
  215. return self.playlist_result(entries, mid, album_name, album_detail)
  216. class QQMusicToplistIE(QQPlaylistBaseIE):
  217. IE_NAME = 'qqmusic:toplist'
  218. IE_DESC = 'QQ音乐 - 排行榜'
  219. _VALID_URL = r'http://y\.qq\.com/#type=toplist&p=(?P<id>(top|global)_[0-9]+)'
  220. _TESTS = [{
  221. 'url': 'http://y.qq.com/#type=toplist&p=global_123',
  222. 'info_dict': {
  223. 'id': 'global_123',
  224. 'title': '美国iTunes榜',
  225. },
  226. 'playlist_count': 10,
  227. }, {
  228. 'url': 'http://y.qq.com/#type=toplist&p=top_3',
  229. 'info_dict': {
  230. 'id': 'top_3',
  231. 'title': 'QQ音乐巅峰榜·欧美',
  232. 'description': 'QQ音乐巅峰榜·欧美根据用户收听行为自动生成,集结当下最流行的欧美新歌!:更新时间:每周四22点|统'
  233. '计周期:一周(上周四至本周三)|统计对象:三个月内发行的欧美歌曲|统计数量:100首|统计算法:根据'
  234. '歌曲在一周内的有效播放次数,由高到低取前100名(同一歌手最多允许5首歌曲同时上榜)|有效播放次数:'
  235. '登录用户完整播放一首歌曲,记为一次有效播放;同一用户收听同一首歌曲,每天记录为1次有效播放'
  236. },
  237. 'playlist_count': 100,
  238. }, {
  239. 'url': 'http://y.qq.com/#type=toplist&p=global_106',
  240. 'info_dict': {
  241. 'id': 'global_106',
  242. 'title': '韩国Mnet榜',
  243. },
  244. 'playlist_count': 50,
  245. }]
  246. def _real_extract(self, url):
  247. list_id = self._match_id(url)
  248. list_type, num_id = list_id.split("_")
  249. toplist_json = self._download_json(
  250. 'http://i.y.qq.com/v8/fcg-bin/fcg_v8_toplist_cp.fcg?type=%s&topid=%s&format=json'
  251. % (list_type, num_id),
  252. list_id, 'Download toplist page')
  253. entries = [
  254. self.url_result(
  255. 'http://y.qq.com/#type=song&mid=' + song['data']['songmid'], 'QQMusic', song['data']['songmid']
  256. ) for song in toplist_json['songlist']
  257. ]
  258. topinfo = toplist_json.get('topinfo', {})
  259. list_name = topinfo.get('ListName')
  260. list_description = topinfo.get('info')
  261. return self.playlist_result(entries, list_id, list_name, list_description)
  262. class QQMusicPlaylistIE(QQPlaylistBaseIE):
  263. IE_NAME = 'qqmusic:playlist'
  264. IE_DESC = 'QQ音乐 - 歌单'
  265. _VALID_URL = r'http://y\.qq\.com/#type=taoge&id=(?P<id>[0-9]+)'
  266. _TEST = {
  267. 'url': 'http://y.qq.com/#type=taoge&id=3462654915',
  268. 'info_dict': {
  269. 'id': '3462654915',
  270. 'title': '韩国5月新歌精选下旬',
  271. 'description': 'md5:d2c9d758a96b9888cf4fe82f603121d4',
  272. },
  273. 'playlist_count': 40,
  274. }
  275. def _real_extract(self, url):
  276. list_id = self._match_id(url)
  277. list_json = self._download_json(
  278. 'http://i.y.qq.com/qzone-music/fcg-bin/fcg_ucc_getcdinfo_byids_cp.fcg?type=1&json=1&utf8=1&onlysong=0&disstid=%s'
  279. % list_id, list_id, 'Download list page',
  280. transform_source=strip_jsonp)['cdlist'][0]
  281. entries = [
  282. self.url_result(
  283. 'http://y.qq.com/#type=song&mid=' + song['songmid'], 'QQMusic', song['songmid']
  284. ) for song in list_json['songlist']
  285. ]
  286. list_name = list_json.get('dissname')
  287. list_description = clean_html(unescapeHTML(list_json.get('desc')))
  288. return self.playlist_result(entries, list_id, list_name, list_description)