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

9 years ago
9 years ago
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. import itertools
  5. from .common import InfoExtractor
  6. from ..utils import (
  7. get_element_by_id,
  8. clean_html,
  9. ExtractorError,
  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'http://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. def _real_extract(self, url):
  75. song_id = self._match_id(url)
  76. webpage = self._download_webpage(
  77. url, song_id, note='Download song detail info',
  78. errnote='Unable to get song detail info')
  79. if '对不起,该歌曲由于版权问题已被下线,将返回网站首页' in webpage:
  80. raise ExtractorError('this song has been offline because of copyright issues', expected=True)
  81. song_name = self._html_search_regex(
  82. r'(?s)class="(?:[^"\s]+\s+)*title(?:\s+[^"\s]+)*".*?<h1[^>]+title="([^"]+)"', webpage, 'song name')
  83. singer_name = self._html_search_regex(
  84. r'<div[^>]+class="s_img">\s*<a[^>]+title="([^>]+)"',
  85. webpage, 'singer name', fatal=False)
  86. lrc_content = clean_html(get_element_by_id('lrcContent', webpage))
  87. if lrc_content == '暂无': # indicates no lyrics
  88. lrc_content = None
  89. formats = self._get_formats(song_id)
  90. album_id = self._html_search_regex(
  91. r'<p[^>]+class="album"[^<]+<a[^>]+href="http://www\.kuwo\.cn/album/(\d+)/"',
  92. webpage, 'album id', fatal=False)
  93. publish_time = None
  94. if album_id is not None:
  95. album_info_page = self._download_webpage(
  96. 'http://www.kuwo.cn/album/%s/' % album_id, song_id,
  97. note='Download album detail info',
  98. errnote='Unable to get album detail info')
  99. publish_time = self._html_search_regex(
  100. r'发行时间:(\d{4}-\d{2}-\d{2})', album_info_page,
  101. 'publish time', fatal=False)
  102. if publish_time:
  103. publish_time = publish_time.replace('-', '')
  104. return {
  105. 'id': song_id,
  106. 'title': song_name,
  107. 'creator': singer_name,
  108. 'upload_date': publish_time,
  109. 'description': lrc_content,
  110. 'formats': formats,
  111. }
  112. class KuwoAlbumIE(InfoExtractor):
  113. IE_NAME = 'kuwo:album'
  114. IE_DESC = '酷我音乐 - 专辑'
  115. _VALID_URL = r'http://www\.kuwo\.cn/album/(?P<id>\d+?)/'
  116. _TEST = {
  117. 'url': 'http://www.kuwo.cn/album/502294/',
  118. 'info_dict': {
  119. 'id': '502294',
  120. 'title': 'M',
  121. 'description': 'md5:6a7235a84cc6400ec3b38a7bdaf1d60c',
  122. },
  123. 'playlist_count': 2,
  124. }
  125. def _real_extract(self, url):
  126. album_id = self._match_id(url)
  127. webpage = self._download_webpage(
  128. url, album_id, note='Download album info',
  129. errnote='Unable to get album info')
  130. album_name = self._html_search_regex(
  131. r'<div[^>]+class="comm"[^<]+<h1[^>]+title="([^"]+)"', webpage,
  132. 'album name')
  133. album_intro = remove_start(
  134. clean_html(get_element_by_id('intro', webpage)),
  135. '%s简介:' % album_name)
  136. entries = [
  137. self.url_result(song_url, 'Kuwo') for song_url in re.findall(
  138. r'<p[^>]+class="listen"><a[^>]+href="(http://www\.kuwo\.cn/yinyue/\d+/)"',
  139. webpage)
  140. ]
  141. return self.playlist_result(entries, album_id, album_name, album_intro)
  142. class KuwoChartIE(InfoExtractor):
  143. IE_NAME = 'kuwo:chart'
  144. IE_DESC = '酷我音乐 - 排行榜'
  145. _VALID_URL = r'http://yinyue\.kuwo\.cn/billboard_(?P<id>[^.]+).htm'
  146. _TEST = {
  147. 'url': 'http://yinyue.kuwo.cn/billboard_香港中文龙虎榜.htm',
  148. 'info_dict': {
  149. 'id': '香港中文龙虎榜',
  150. 'title': '香港中文龙虎榜',
  151. 'description': 're:\d{4}第\d{2}期',
  152. },
  153. 'playlist_mincount': 10,
  154. }
  155. def _real_extract(self, url):
  156. chart_id = self._match_id(url)
  157. webpage = self._download_webpage(
  158. url, chart_id, note='Download chart info',
  159. errnote='Unable to get chart info')
  160. chart_name = self._html_search_regex(
  161. r'<h1[^>]+class="unDis">([^<]+)</h1>', webpage, 'chart name')
  162. chart_desc = self._html_search_regex(
  163. r'<p[^>]+class="tabDef">(\d{4}第\d{2}期)</p>', webpage, 'chart desc')
  164. entries = [
  165. self.url_result(song_url, 'Kuwo') for song_url in re.findall(
  166. r'<a[^>]+href="(http://www\.kuwo\.cn/yinyue/\d+)/"', webpage)
  167. ]
  168. return self.playlist_result(entries, chart_id, chart_name, chart_desc)
  169. class KuwoSingerIE(InfoExtractor):
  170. IE_NAME = 'kuwo:singer'
  171. IE_DESC = '酷我音乐 - 歌手'
  172. _VALID_URL = r'http://www\.kuwo\.cn/mingxing/(?P<id>[^/]+)'
  173. _TESTS = [{
  174. 'url': 'http://www.kuwo.cn/mingxing/bruno+mars/',
  175. 'info_dict': {
  176. 'id': 'bruno+mars',
  177. 'title': 'Bruno Mars',
  178. },
  179. 'playlist_count': 10,
  180. }, {
  181. 'url': 'http://www.kuwo.cn/mingxing/Ali/music.htm',
  182. 'info_dict': {
  183. 'id': 'Ali',
  184. 'title': 'Ali',
  185. },
  186. 'playlist_mincount': 95,
  187. 'skip': 'Regularly stalls travis build', # See https://travis-ci.org/rg3/youtube-dl/jobs/78878540
  188. }]
  189. def _real_extract(self, url):
  190. singer_id = self._match_id(url)
  191. webpage = self._download_webpage(
  192. url, singer_id, note='Download singer info',
  193. errnote='Unable to get singer info')
  194. singer_name = self._html_search_regex(
  195. r'<div class="title clearfix">\s*<h1>([^<]+)<span', webpage, 'singer name'
  196. )
  197. entries = []
  198. first_page_only = False if re.search(r'/music(?:_\d+)?\.htm', url) else True
  199. for page_num in itertools.count(1):
  200. webpage = self._download_webpage(
  201. 'http://www.kuwo.cn/mingxing/%s/music_%d.htm' % (singer_id, page_num),
  202. singer_id, note='Download song list page #%d' % page_num,
  203. errnote='Unable to get song list page #%d' % page_num)
  204. entries.extend([
  205. self.url_result(song_url, 'Kuwo') for song_url in re.findall(
  206. r'<p[^>]+class="m_name"><a[^>]+href="(http://www\.kuwo\.cn/yinyue/\d+)/',
  207. webpage)
  208. ][:10 if first_page_only else None])
  209. if first_page_only or not re.search(r'<a[^>]+href="[^"]+">下一页</a>', webpage):
  210. break
  211. return self.playlist_result(entries, singer_id, singer_name)
  212. class KuwoCategoryIE(InfoExtractor):
  213. IE_NAME = 'kuwo:category'
  214. IE_DESC = '酷我音乐 - 分类'
  215. _VALID_URL = r'http://yinyue\.kuwo\.cn/yy/cinfo_(?P<id>\d+?).htm'
  216. _TEST = {
  217. 'url': 'http://yinyue.kuwo.cn/yy/cinfo_86375.htm',
  218. 'info_dict': {
  219. 'id': '86375',
  220. 'title': '八十年代精选',
  221. 'description': '这些都是属于八十年代的回忆!',
  222. },
  223. 'playlist_count': 30,
  224. }
  225. def _real_extract(self, url):
  226. category_id = self._match_id(url)
  227. webpage = self._download_webpage(
  228. url, category_id, note='Download category info',
  229. errnote='Unable to get category info')
  230. category_name = self._html_search_regex(
  231. r'<h1[^>]+title="([^<>]+?)">[^<>]+?</h1>', webpage, 'category name')
  232. category_desc = remove_start(
  233. get_element_by_id('intro', webpage).strip(),
  234. '%s简介:' % category_name)
  235. jsonm = self._parse_json(self._html_search_regex(
  236. r'var\s+jsonm\s*=\s*([^;]+);', webpage, 'category songs'), category_id)
  237. entries = [
  238. self.url_result('http://www.kuwo.cn/yinyue/%s/' % song['musicrid'], 'Kuwo')
  239. for song in jsonm['musiclist']
  240. ]
  241. return self.playlist_result(entries, category_id, category_name, category_desc)
  242. class KuwoMvIE(KuwoBaseIE):
  243. IE_NAME = 'kuwo:mv'
  244. IE_DESC = '酷我音乐 - MV'
  245. _VALID_URL = r'http://www\.kuwo\.cn/mv/(?P<id>\d+?)/'
  246. _TEST = {
  247. 'url': 'http://www.kuwo.cn/mv/6480076/',
  248. 'info_dict': {
  249. 'id': '6480076',
  250. 'ext': 'mp4',
  251. 'title': 'My HouseMV',
  252. 'creator': '2PM',
  253. },
  254. # In this video, music URLs (anti.s) are blocked outside China and
  255. # USA, while the MV URL (mvurl) is available globally, so force the MV
  256. # URL for consistent results in different countries
  257. 'params': {
  258. 'format': 'mv',
  259. },
  260. }
  261. _FORMATS = KuwoBaseIE._FORMATS + [
  262. {'format': 'mkv', 'ext': 'mkv', 'preference': 250},
  263. {'format': 'mp4', 'ext': 'mp4', 'preference': 200},
  264. ]
  265. def _real_extract(self, url):
  266. song_id = self._match_id(url)
  267. webpage = self._download_webpage(
  268. url, song_id, note='Download mv detail info: %s' % song_id,
  269. errnote='Unable to get mv detail info: %s' % song_id)
  270. mobj = re.search(
  271. r'<h1[^>]+title="(?P<song>[^"]+)">[^<]+<span[^>]+title="(?P<singer>[^"]+)"',
  272. webpage)
  273. if mobj:
  274. song_name = mobj.group('song')
  275. singer_name = mobj.group('singer')
  276. else:
  277. raise ExtractorError('Unable to find song or singer names')
  278. formats = self._get_formats(song_id, tolerate_ip_deny=True)
  279. mv_url = self._download_webpage(
  280. 'http://www.kuwo.cn/yy/st/mvurl?rid=MUSIC_%s' % song_id,
  281. song_id, note='Download %s MV URL' % song_id)
  282. formats.append({
  283. 'url': mv_url,
  284. 'format_id': 'mv',
  285. })
  286. self._sort_formats(formats)
  287. return {
  288. 'id': song_id,
  289. 'title': song_name,
  290. 'creator': singer_name,
  291. 'formats': formats,
  292. }