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.

314 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. }]
  178. def _real_extract(self, url):
  179. singer_id = self._match_id(url)
  180. webpage = self._download_webpage(
  181. url, singer_id, note='Download singer info',
  182. errnote='Unable to get singer info')
  183. singer_name = self._html_search_regex(
  184. r'<div class="title clearfix">\s*<h1>([^<]+)<span', webpage, 'singer name'
  185. )
  186. entries = []
  187. first_page_only = False if re.search(r'/music(?:_\d+)?\.htm', url) else True
  188. for page_num in itertools.count(1):
  189. webpage = self._download_webpage(
  190. 'http://www.kuwo.cn/mingxing/%s/music_%d.htm' % (singer_id, page_num),
  191. singer_id, note='Download song list page #%d' % page_num,
  192. errnote='Unable to get song list page #%d' % page_num)
  193. entries.extend([
  194. self.url_result(song_url, 'Kuwo') for song_url in re.findall(
  195. r'<p[^>]+class="m_name"><a[^>]+href="(http://www\.kuwo\.cn/yinyue/\d+)/',
  196. webpage)
  197. ][:10 if first_page_only else None])
  198. if first_page_only or not re.search(r'<a[^>]+href="[^"]+">下一页</a>', webpage):
  199. break
  200. return self.playlist_result(entries, singer_id, singer_name)
  201. class KuwoCategoryIE(InfoExtractor):
  202. IE_NAME = 'kuwo:category'
  203. IE_DESC = '酷我音乐 - 分类'
  204. _VALID_URL = r'http://yinyue\.kuwo\.cn/yy/cinfo_(?P<id>\d+?).htm'
  205. _TEST = {
  206. 'url': 'http://yinyue.kuwo.cn/yy/cinfo_86375.htm',
  207. 'info_dict': {
  208. 'id': '86375',
  209. 'title': '八十年代精选',
  210. 'description': '这些都是属于八十年代的回忆!',
  211. },
  212. 'playlist_count': 30,
  213. }
  214. def _real_extract(self, url):
  215. category_id = self._match_id(url)
  216. webpage = self._download_webpage(
  217. url, category_id, note='Download category info',
  218. errnote='Unable to get category info')
  219. category_name = self._html_search_regex(
  220. r'<h1[^>]+title="([^<>]+?)">[^<>]+?</h1>', webpage, 'category name')
  221. category_desc = remove_start(
  222. get_element_by_id('intro', webpage).strip(),
  223. '%s简介:' % category_name)
  224. jsonm = self._parse_json(self._html_search_regex(
  225. r'var\s+jsonm\s*=\s*([^;]+);', webpage, 'category songs'), category_id)
  226. entries = [
  227. self.url_result('http://www.kuwo.cn/yinyue/%s/' % song['musicrid'], 'Kuwo')
  228. for song in jsonm['musiclist']
  229. ]
  230. return self.playlist_result(entries, category_id, category_name, category_desc)
  231. class KuwoMvIE(KuwoBaseIE):
  232. IE_NAME = 'kuwo:mv'
  233. IE_DESC = '酷我音乐 - MV'
  234. _VALID_URL = r'http://www\.kuwo\.cn/mv/(?P<id>\d+?)/'
  235. _TEST = {
  236. 'url': 'http://www.kuwo.cn/mv/6480076/',
  237. 'info_dict': {
  238. 'id': '6480076',
  239. 'ext': 'mkv',
  240. 'title': '我们家MV',
  241. 'creator': '2PM',
  242. },
  243. }
  244. _FORMATS = KuwoBaseIE._FORMATS + [
  245. {'format': 'mkv', 'ext': 'mkv', 'preference': 250},
  246. {'format': 'mp4', 'ext': 'mp4', 'preference': 200},
  247. ]
  248. def _real_extract(self, url):
  249. song_id = self._match_id(url)
  250. webpage = self._download_webpage(
  251. url, song_id, note='Download mv detail info: %s' % song_id,
  252. errnote='Unable to get mv detail info: %s' % song_id)
  253. mobj = re.search(
  254. r'<h1[^>]+title="(?P<song>[^"]+)">[^<]+<span[^>]+title="(?P<singer>[^"]+)"',
  255. webpage)
  256. if mobj:
  257. song_name = mobj.group('song')
  258. singer_name = mobj.group('singer')
  259. else:
  260. raise ExtractorError('Unable to find song or singer names')
  261. formats = self._get_formats(song_id)
  262. return {
  263. 'id': song_id,
  264. 'title': song_name,
  265. 'creator': singer_name,
  266. 'formats': formats,
  267. }