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.

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