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.

355 lines
12 KiB

10 years ago
10 years ago
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..utils import (
  6. get_element_by_id,
  7. clean_html,
  8. ExtractorError,
  9. InAdvancePagedList,
  10. remove_start,
  11. )
  12. class KuwoBaseIE(InfoExtractor):
  13. _FORMATS = [
  14. {'format': 'ape', 'ext': 'ape', 'preference': 100},
  15. {'format': 'mp3-320', 'ext': 'mp3', 'br': '320kmp3', 'abr': 320, 'preference': 80},
  16. {'format': 'mp3-192', 'ext': 'mp3', 'br': '192kmp3', 'abr': 192, 'preference': 70},
  17. {'format': 'mp3-128', 'ext': 'mp3', 'br': '128kmp3', 'abr': 128, 'preference': 60},
  18. {'format': 'wma', 'ext': 'wma', 'preference': 20},
  19. {'format': 'aac', 'ext': 'aac', 'abr': 48, 'preference': 10}
  20. ]
  21. def _get_formats(self, song_id, tolerate_ip_deny=False):
  22. formats = []
  23. for file_format in self._FORMATS:
  24. headers = {}
  25. cn_verification_proxy = self._downloader.params.get('cn_verification_proxy')
  26. if cn_verification_proxy:
  27. headers['Ytdl-request-proxy'] = cn_verification_proxy
  28. query = {
  29. 'format': file_format['ext'],
  30. 'br': file_format.get('br', ''),
  31. 'rid': 'MUSIC_%s' % song_id,
  32. 'type': 'convert_url',
  33. 'response': 'url'
  34. }
  35. song_url = self._download_webpage(
  36. 'http://antiserver.kuwo.cn/anti.s',
  37. song_id, note='Download %s url info' % file_format['format'],
  38. query=query, headers=headers,
  39. )
  40. if song_url == 'IPDeny' and not tolerate_ip_deny:
  41. raise ExtractorError('This song is blocked in this region', expected=True)
  42. if song_url.startswith('http://') or song_url.startswith('https://'):
  43. formats.append({
  44. 'url': song_url,
  45. 'format_id': file_format['format'],
  46. 'format': file_format['format'],
  47. 'preference': file_format['preference'],
  48. 'abr': file_format.get('abr'),
  49. })
  50. return formats
  51. class KuwoIE(KuwoBaseIE):
  52. IE_NAME = 'kuwo:song'
  53. IE_DESC = '酷我音乐'
  54. _VALID_URL = r'https?://www\.kuwo\.cn/yinyue/(?P<id>\d+)'
  55. _TESTS = [{
  56. 'url': 'http://www.kuwo.cn/yinyue/635632/',
  57. 'info_dict': {
  58. 'id': '635632',
  59. 'ext': 'ape',
  60. 'title': '爱我别走',
  61. 'creator': '张震岳',
  62. 'upload_date': '20080122',
  63. 'description': 'md5:ed13f58e3c3bf3f7fd9fbc4e5a7aa75c'
  64. },
  65. 'skip': 'this song has been offline because of copyright issues',
  66. }, {
  67. 'url': 'http://www.kuwo.cn/yinyue/6446136/',
  68. 'info_dict': {
  69. 'id': '6446136',
  70. 'ext': 'mp3',
  71. 'title': '',
  72. 'description': 'md5:5d0e947b242c35dc0eb1d2fce9fbf02c',
  73. 'creator': 'IU',
  74. 'upload_date': '20150518',
  75. },
  76. 'params': {
  77. 'format': 'mp3-320'
  78. },
  79. }, {
  80. 'url': 'http://www.kuwo.cn/yinyue/3197154?catalog=yueku2016',
  81. 'only_matching': True,
  82. }]
  83. def _real_extract(self, url):
  84. song_id = self._match_id(url)
  85. webpage = self._download_webpage(
  86. url, song_id, note='Download song detail info',
  87. errnote='Unable to get song detail info')
  88. if '对不起,该歌曲由于版权问题已被下线,将返回网站首页' in webpage:
  89. raise ExtractorError('this song has been offline because of copyright issues', expected=True)
  90. song_name = self._html_search_regex(
  91. r'<p[^>]+id="lrcName">([^<]+)</p>', webpage, 'song name')
  92. singer_name = remove_start(self._html_search_regex(
  93. r'<a[^>]+href="http://www\.kuwo\.cn/artist/content\?name=([^"]+)">',
  94. webpage, 'singer name', fatal=False), '歌手')
  95. lrc_content = clean_html(get_element_by_id('lrcContent', webpage))
  96. if lrc_content == '暂无': # indicates no lyrics
  97. lrc_content = None
  98. formats = self._get_formats(song_id)
  99. self._sort_formats(formats)
  100. album_id = self._html_search_regex(
  101. r'<a[^>]+href="http://www\.kuwo\.cn/album/(\d+)/"',
  102. webpage, 'album id', fatal=False)
  103. publish_time = None
  104. if album_id is not None:
  105. album_info_page = self._download_webpage(
  106. 'http://www.kuwo.cn/album/%s/' % album_id, song_id,
  107. note='Download album detail info',
  108. errnote='Unable to get album detail info')
  109. publish_time = self._html_search_regex(
  110. r'发行时间:(\d{4}-\d{2}-\d{2})', album_info_page,
  111. 'publish time', fatal=False)
  112. if publish_time:
  113. publish_time = publish_time.replace('-', '')
  114. return {
  115. 'id': song_id,
  116. 'title': song_name,
  117. 'creator': singer_name,
  118. 'upload_date': publish_time,
  119. 'description': lrc_content,
  120. 'formats': formats,
  121. }
  122. class KuwoAlbumIE(InfoExtractor):
  123. IE_NAME = 'kuwo:album'
  124. IE_DESC = '酷我音乐 - 专辑'
  125. _VALID_URL = r'https?://www\.kuwo\.cn/album/(?P<id>\d+?)/'
  126. _TEST = {
  127. 'url': 'http://www.kuwo.cn/album/502294/',
  128. 'info_dict': {
  129. 'id': '502294',
  130. 'title': 'Made\xa0Series\xa0《M》',
  131. 'description': 'md5:d463f0d8a0ff3c3ea3d6ed7452a9483f',
  132. },
  133. 'playlist_count': 2,
  134. }
  135. def _real_extract(self, url):
  136. album_id = self._match_id(url)
  137. webpage = self._download_webpage(
  138. url, album_id, note='Download album info',
  139. errnote='Unable to get album info')
  140. album_name = self._html_search_regex(
  141. r'<div[^>]+class="comm"[^<]+<h1[^>]+title="([^"]+)"', webpage,
  142. 'album name')
  143. album_intro = remove_start(
  144. clean_html(get_element_by_id('intro', webpage)),
  145. '%s简介:' % album_name)
  146. entries = [
  147. self.url_result(song_url, 'Kuwo') for song_url in re.findall(
  148. r'<p[^>]+class="listen"><a[^>]+href="(http://www\.kuwo\.cn/yinyue/\d+/)"',
  149. webpage)
  150. ]
  151. return self.playlist_result(entries, album_id, album_name, album_intro)
  152. class KuwoChartIE(InfoExtractor):
  153. IE_NAME = 'kuwo:chart'
  154. IE_DESC = '酷我音乐 - 排行榜'
  155. _VALID_URL = r'https?://yinyue\.kuwo\.cn/billboard_(?P<id>[^.]+).htm'
  156. _TEST = {
  157. 'url': 'http://yinyue.kuwo.cn/billboard_香港中文龙虎榜.htm',
  158. 'info_dict': {
  159. 'id': '香港中文龙虎榜',
  160. },
  161. 'playlist_mincount': 10,
  162. }
  163. def _real_extract(self, url):
  164. chart_id = self._match_id(url)
  165. webpage = self._download_webpage(
  166. url, chart_id, note='Download chart info',
  167. errnote='Unable to get chart info')
  168. entries = [
  169. self.url_result(song_url, 'Kuwo') for song_url in re.findall(
  170. r'<a[^>]+href="(http://www\.kuwo\.cn/yinyue/\d+)', webpage)
  171. ]
  172. return self.playlist_result(entries, chart_id)
  173. class KuwoSingerIE(InfoExtractor):
  174. IE_NAME = 'kuwo:singer'
  175. IE_DESC = '酷我音乐 - 歌手'
  176. _VALID_URL = r'https?://www\.kuwo\.cn/mingxing/(?P<id>[^/]+)'
  177. _TESTS = [{
  178. 'url': 'http://www.kuwo.cn/mingxing/bruno+mars/',
  179. 'info_dict': {
  180. 'id': 'bruno+mars',
  181. 'title': 'Bruno\xa0Mars',
  182. },
  183. 'playlist_mincount': 329,
  184. }, {
  185. 'url': 'http://www.kuwo.cn/mingxing/Ali/music.htm',
  186. 'info_dict': {
  187. 'id': 'Ali',
  188. 'title': 'Ali',
  189. },
  190. 'playlist_mincount': 95,
  191. 'skip': 'Regularly stalls travis build', # See https://travis-ci.org/rg3/youtube-dl/jobs/78878540
  192. }]
  193. PAGE_SIZE = 15
  194. def _real_extract(self, url):
  195. singer_id = self._match_id(url)
  196. webpage = self._download_webpage(
  197. url, singer_id, note='Download singer info',
  198. errnote='Unable to get singer info')
  199. singer_name = self._html_search_regex(
  200. r'<h1>([^<]+)</h1>', webpage, 'singer name')
  201. artist_id = self._html_search_regex(
  202. r'data-artistid="(\d+)"', webpage, 'artist id')
  203. page_count = int(self._html_search_regex(
  204. r'data-page="(\d+)"', webpage, 'page count'))
  205. def page_func(page_num):
  206. webpage = self._download_webpage(
  207. 'http://www.kuwo.cn/artist/contentMusicsAjax',
  208. singer_id, note='Download song list page #%d' % (page_num + 1),
  209. errnote='Unable to get song list page #%d' % (page_num + 1),
  210. query={'artistId': artist_id, 'pn': page_num, 'rn': self.PAGE_SIZE})
  211. return [
  212. self.url_result(song_url, 'Kuwo') for song_url in re.findall(
  213. r'<div[^>]+class="name"><a[^>]+href="(http://www\.kuwo\.cn/yinyue/\d+)',
  214. webpage)
  215. ]
  216. entries = InAdvancePagedList(page_func, page_count, self.PAGE_SIZE)
  217. return self.playlist_result(entries, singer_id, singer_name)
  218. class KuwoCategoryIE(InfoExtractor):
  219. IE_NAME = 'kuwo:category'
  220. IE_DESC = '酷我音乐 - 分类'
  221. _VALID_URL = r'https?://yinyue\.kuwo\.cn/yy/cinfo_(?P<id>\d+?).htm'
  222. _TEST = {
  223. 'url': 'http://yinyue.kuwo.cn/yy/cinfo_86375.htm',
  224. 'info_dict': {
  225. 'id': '86375',
  226. 'title': '八十年代精选',
  227. 'description': '这些都是属于八十年代的回忆!',
  228. },
  229. 'playlist_mincount': 24,
  230. }
  231. def _real_extract(self, url):
  232. category_id = self._match_id(url)
  233. webpage = self._download_webpage(
  234. url, category_id, note='Download category info',
  235. errnote='Unable to get category info')
  236. category_name = self._html_search_regex(
  237. r'<h1[^>]+title="([^<>]+?)">[^<>]+?</h1>', webpage, 'category name')
  238. category_desc = remove_start(
  239. get_element_by_id('intro', webpage).strip(),
  240. '%s简介:' % category_name)
  241. if category_desc == '暂无':
  242. category_desc = None
  243. jsonm = self._parse_json(self._html_search_regex(
  244. r'var\s+jsonm\s*=\s*([^;]+);', webpage, 'category songs'), category_id)
  245. entries = [
  246. self.url_result('http://www.kuwo.cn/yinyue/%s/' % song['musicrid'], 'Kuwo')
  247. for song in jsonm['musiclist']
  248. ]
  249. return self.playlist_result(entries, category_id, category_name, category_desc)
  250. class KuwoMvIE(KuwoBaseIE):
  251. IE_NAME = 'kuwo:mv'
  252. IE_DESC = '酷我音乐 - MV'
  253. _VALID_URL = r'https?://www\.kuwo\.cn/mv/(?P<id>\d+?)/'
  254. _TEST = {
  255. 'url': 'http://www.kuwo.cn/mv/6480076/',
  256. 'info_dict': {
  257. 'id': '6480076',
  258. 'ext': 'mp4',
  259. 'title': 'My HouseMV',
  260. 'creator': 'PM02:00',
  261. },
  262. # In this video, music URLs (anti.s) are blocked outside China and
  263. # USA, while the MV URL (mvurl) is available globally, so force the MV
  264. # URL for consistent results in different countries
  265. 'params': {
  266. 'format': 'mv',
  267. },
  268. }
  269. _FORMATS = KuwoBaseIE._FORMATS + [
  270. {'format': 'mkv', 'ext': 'mkv', 'preference': 250},
  271. {'format': 'mp4', 'ext': 'mp4', 'preference': 200},
  272. ]
  273. def _real_extract(self, url):
  274. song_id = self._match_id(url)
  275. webpage = self._download_webpage(
  276. url, song_id, note='Download mv detail info: %s' % song_id,
  277. errnote='Unable to get mv detail info: %s' % song_id)
  278. mobj = re.search(
  279. r'<h1[^>]+title="(?P<song>[^"]+)">[^<]+<span[^>]+title="(?P<singer>[^"]+)"',
  280. webpage)
  281. if mobj:
  282. song_name = mobj.group('song')
  283. singer_name = mobj.group('singer')
  284. else:
  285. raise ExtractorError('Unable to find song or singer names')
  286. formats = self._get_formats(song_id, tolerate_ip_deny=True)
  287. mv_url = self._download_webpage(
  288. 'http://www.kuwo.cn/yy/st/mvurl?rid=MUSIC_%s' % song_id,
  289. song_id, note='Download %s MV URL' % song_id)
  290. formats.append({
  291. 'url': mv_url,
  292. 'format_id': 'mv',
  293. })
  294. self._sort_formats(formats)
  295. return {
  296. 'id': song_id,
  297. 'title': song_name,
  298. 'creator': singer_name,
  299. 'formats': formats,
  300. }