qqmusic.py 14 KB

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