kuwo.py 11 KB

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