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