logo

youtube-dl

[mirror] Download/Watch videos from video hostersgit clone https://hacktivis.me/git/mirror/youtube-dl.git

qqmusic.py (13646B)


  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. clean_html,
  9. ExtractorError,
  10. strip_jsonp,
  11. unescapeHTML,
  12. )
  13. class QQMusicIE(InfoExtractor):
  14. IE_NAME = 'qqmusic'
  15. IE_DESC = 'QQ音乐'
  16. _VALID_URL = r'https?://y\.qq\.com/n/yqq/song/(?P<id>[0-9A-Za-z]+)\.html'
  17. _TESTS = [{
  18. 'url': 'https://y.qq.com/n/yqq/song/004295Et37taLD.html',
  19. 'md5': '5f1e6cea39e182857da7ffc5ef5e6bb8',
  20. 'info_dict': {
  21. 'id': '004295Et37taLD',
  22. 'ext': 'mp3',
  23. 'title': '可惜没如果',
  24. 'release_date': '20141227',
  25. 'creator': '林俊杰',
  26. 'description': 'md5:d85afb3051952ecc50a1ee8a286d1eac',
  27. 'thumbnail': r're:^https?://.*\.jpg$',
  28. }
  29. }, {
  30. 'note': 'There is no mp3-320 version of this song.',
  31. 'url': 'https://y.qq.com/n/yqq/song/004MsGEo3DdNxV.html',
  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': r're:^https?://.*\.jpg$',
  41. }
  42. }, {
  43. 'note': 'lyrics not in .lrc format',
  44. 'url': 'https://y.qq.com/n/yqq/song/001JyApY11tIp6.html',
  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:c9b20210587cbcd6836a1c597bab4525',
  52. 'thumbnail': r'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. def get_singer_all_songs(self, singmid, num):
  138. return self._download_webpage(
  139. r'https://c.y.qq.com/v8/fcg-bin/fcg_v8_singer_track_cp.fcg', singmid,
  140. query={
  141. 'format': 'json',
  142. 'inCharset': 'utf8',
  143. 'outCharset': 'utf-8',
  144. 'platform': 'yqq',
  145. 'needNewCode': 0,
  146. 'singermid': singmid,
  147. 'order': 'listen',
  148. 'begin': 0,
  149. 'num': num,
  150. 'songstatus': 1,
  151. })
  152. def get_entries_from_page(self, singmid):
  153. entries = []
  154. default_num = 1
  155. json_text = self.get_singer_all_songs(singmid, default_num)
  156. json_obj_all_songs = self._parse_json(json_text, singmid)
  157. if json_obj_all_songs['code'] == 0:
  158. total = json_obj_all_songs['data']['total']
  159. json_text = self.get_singer_all_songs(singmid, total)
  160. json_obj_all_songs = self._parse_json(json_text, singmid)
  161. for item in json_obj_all_songs['data']['list']:
  162. if item['musicData'].get('songmid') is not None:
  163. songmid = item['musicData']['songmid']
  164. entries.append(self.url_result(
  165. r'https://y.qq.com/n/yqq/song/%s.html' % songmid, 'QQMusic', songmid))
  166. return entries
  167. class QQMusicSingerIE(QQPlaylistBaseIE):
  168. IE_NAME = 'qqmusic:singer'
  169. IE_DESC = 'QQ音乐 - 歌手'
  170. _VALID_URL = r'https?://y\.qq\.com/n/yqq/singer/(?P<id>[0-9A-Za-z]+)\.html'
  171. _TEST = {
  172. 'url': 'https://y.qq.com/n/yqq/singer/001BLpXF2DyJe2.html',
  173. 'info_dict': {
  174. 'id': '001BLpXF2DyJe2',
  175. 'title': '林俊杰',
  176. 'description': 'md5:870ec08f7d8547c29c93010899103751',
  177. },
  178. 'playlist_mincount': 12,
  179. }
  180. def _real_extract(self, url):
  181. mid = self._match_id(url)
  182. entries = self.get_entries_from_page(mid)
  183. singer_page = self._download_webpage(url, mid, 'Download singer page')
  184. singer_name = self._html_search_regex(
  185. r"singername\s*:\s*'(.*?)'", singer_page, 'singer name', default=None)
  186. singer_desc = None
  187. if mid:
  188. singer_desc_page = self._download_xml(
  189. 'http://s.plcloud.music.qq.com/fcgi-bin/fcg_get_singer_desc.fcg', mid,
  190. 'Donwload singer description XML',
  191. query={'utf8': 1, 'outCharset': 'utf-8', 'format': 'xml', 'singermid': mid},
  192. headers={'Referer': 'https://y.qq.com/n/yqq/singer/'})
  193. singer_desc = singer_desc_page.find('./data/info/desc').text
  194. return self.playlist_result(entries, mid, singer_name, singer_desc)
  195. class QQMusicAlbumIE(QQPlaylistBaseIE):
  196. IE_NAME = 'qqmusic:album'
  197. IE_DESC = 'QQ音乐 - 专辑'
  198. _VALID_URL = r'https?://y\.qq\.com/n/yqq/album/(?P<id>[0-9A-Za-z]+)\.html'
  199. _TESTS = [{
  200. 'url': 'https://y.qq.com/n/yqq/album/000gXCTb2AhRR1.html',
  201. 'info_dict': {
  202. 'id': '000gXCTb2AhRR1',
  203. 'title': '我们都是这样长大的',
  204. 'description': 'md5:179c5dce203a5931970d306aa9607ea6',
  205. },
  206. 'playlist_count': 4,
  207. }, {
  208. 'url': 'https://y.qq.com/n/yqq/album/002Y5a3b3AlCu3.html',
  209. 'info_dict': {
  210. 'id': '002Y5a3b3AlCu3',
  211. 'title': '그리고...',
  212. 'description': 'md5:a48823755615508a95080e81b51ba729',
  213. },
  214. 'playlist_count': 8,
  215. }]
  216. def _real_extract(self, url):
  217. mid = self._match_id(url)
  218. album = self._download_json(
  219. 'http://i.y.qq.com/v8/fcg-bin/fcg_v8_album_info_cp.fcg?albummid=%s&format=json' % mid,
  220. mid, 'Download album page')['data']
  221. entries = [
  222. self.url_result(
  223. 'https://y.qq.com/n/yqq/song/' + song['songmid'] + '.html', 'QQMusic', song['songmid']
  224. ) for song in album['list']
  225. ]
  226. album_name = album.get('name')
  227. album_detail = album.get('desc')
  228. if album_detail is not None:
  229. album_detail = album_detail.strip()
  230. return self.playlist_result(entries, mid, album_name, album_detail)
  231. class QQMusicToplistIE(QQPlaylistBaseIE):
  232. IE_NAME = 'qqmusic:toplist'
  233. IE_DESC = 'QQ音乐 - 排行榜'
  234. _VALID_URL = r'https?://y\.qq\.com/n/yqq/toplist/(?P<id>[0-9]+)\.html'
  235. _TESTS = [{
  236. 'url': 'https://y.qq.com/n/yqq/toplist/123.html',
  237. 'info_dict': {
  238. 'id': '123',
  239. 'title': '美国iTunes榜',
  240. 'description': 'md5:89db2335fdbb10678dee2d43fe9aba08',
  241. },
  242. 'playlist_count': 100,
  243. }, {
  244. 'url': 'https://y.qq.com/n/yqq/toplist/3.html',
  245. 'info_dict': {
  246. 'id': '3',
  247. 'title': '巅峰榜·欧美',
  248. 'description': 'md5:5a600d42c01696b26b71f8c4d43407da',
  249. },
  250. 'playlist_count': 100,
  251. }, {
  252. 'url': 'https://y.qq.com/n/yqq/toplist/106.html',
  253. 'info_dict': {
  254. 'id': '106',
  255. 'title': '韩国Mnet榜',
  256. 'description': 'md5:cb84b325215e1d21708c615cac82a6e7',
  257. },
  258. 'playlist_count': 50,
  259. }]
  260. def _real_extract(self, url):
  261. list_id = self._match_id(url)
  262. toplist_json = self._download_json(
  263. 'http://i.y.qq.com/v8/fcg-bin/fcg_v8_toplist_cp.fcg', list_id,
  264. note='Download toplist page',
  265. query={'type': 'toplist', 'topid': list_id, 'format': 'json'})
  266. entries = [self.url_result(
  267. 'https://y.qq.com/n/yqq/song/' + song['data']['songmid'] + '.html', 'QQMusic',
  268. song['data']['songmid'])
  269. for song in toplist_json['songlist']]
  270. topinfo = toplist_json.get('topinfo', {})
  271. list_name = topinfo.get('ListName')
  272. list_description = topinfo.get('info')
  273. return self.playlist_result(entries, list_id, list_name, list_description)
  274. class QQMusicPlaylistIE(QQPlaylistBaseIE):
  275. IE_NAME = 'qqmusic:playlist'
  276. IE_DESC = 'QQ音乐 - 歌单'
  277. _VALID_URL = r'https?://y\.qq\.com/n/yqq/playlist/(?P<id>[0-9]+)\.html'
  278. _TESTS = [{
  279. 'url': 'http://y.qq.com/n/yqq/playlist/3462654915.html',
  280. 'info_dict': {
  281. 'id': '3462654915',
  282. 'title': '韩国5月新歌精选下旬',
  283. 'description': 'md5:d2c9d758a96b9888cf4fe82f603121d4',
  284. },
  285. 'playlist_count': 40,
  286. 'skip': 'playlist gone',
  287. }, {
  288. 'url': 'https://y.qq.com/n/yqq/playlist/1374105607.html',
  289. 'info_dict': {
  290. 'id': '1374105607',
  291. 'title': '易入人心的华语民谣',
  292. 'description': '民谣的歌曲易于传唱、、歌词朗朗伤口、旋律简单温馨。属于那种才入耳孔。却上心头的感觉。没有太多的复杂情绪。简单而直接地表达乐者的情绪,就是这样的简单才易入人心。',
  293. },
  294. 'playlist_count': 20,
  295. }]
  296. def _real_extract(self, url):
  297. list_id = self._match_id(url)
  298. list_json = self._download_json(
  299. 'http://i.y.qq.com/qzone-music/fcg-bin/fcg_ucc_getcdinfo_byids_cp.fcg',
  300. list_id, 'Download list page',
  301. query={'type': 1, 'json': 1, 'utf8': 1, 'onlysong': 0, 'disstid': list_id},
  302. transform_source=strip_jsonp)
  303. if not len(list_json.get('cdlist', [])):
  304. if list_json.get('code'):
  305. raise ExtractorError(
  306. 'QQ Music said: error %d in fetching playlist info' % list_json['code'],
  307. expected=True)
  308. raise ExtractorError('Unable to get playlist info')
  309. cdlist = list_json['cdlist'][0]
  310. entries = [self.url_result(
  311. 'https://y.qq.com/n/yqq/song/' + song['songmid'] + '.html', 'QQMusic', song['songmid'])
  312. for song in cdlist['songlist']]
  313. list_name = cdlist.get('dissname')
  314. list_description = clean_html(unescapeHTML(cdlist.get('desc')))
  315. return self.playlist_result(entries, list_id, list_name, list_description)