kuwo.py 11 KB

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