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.

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