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.

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