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.

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