kuwo.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. _TESTS = [{
  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. 'url': 'http://www.kuwo.cn/mingxing/Ali/music.htm',
  167. 'info_dict': {
  168. 'id': 'Ali',
  169. 'title': 'Ali',
  170. },
  171. 'playlist_mincount': 95,
  172. }]
  173. def _real_extract(self, url):
  174. singer_id = self._match_id(url)
  175. webpage = self._download_webpage(
  176. url, singer_id, note='Download singer info',
  177. errnote='Unable to get singer info')
  178. singer_name = self._html_search_regex(
  179. r'<div class="title clearfix">[\n\s\t]*?<h1>(.+?)<span', webpage, 'singer name'
  180. )
  181. entries = []
  182. first_page_only = False if re.match(r'.+/music(?:_[0-9]+)?\.htm', url) else True
  183. for page_num in itertools.count(1):
  184. webpage = self._download_webpage(
  185. 'http://www.kuwo.cn/mingxing/%s/music_%d.htm' % (singer_id, page_num),
  186. singer_id, note='Download song list page #%d' % page_num,
  187. errnote='Unable to get song list page #%d' % page_num)
  188. entries.extend([
  189. self.url_result("http://www.kuwo.cn/yinyue/%s/" % song_id, 'Kuwo', song_id)
  190. for song_id in re.findall(
  191. r'<p class="m_name"><a href="http://www\.kuwo\.cn/yinyue/([0-9]+)/',
  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>[0-9]+?).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 = re.sub(
  217. r'^.+简介:', '', get_element_by_id("intro", webpage).strip())
  218. jsonm = self._parse_json(self._html_search_regex(
  219. r'var jsonm = (\{.+?\});', webpage, 'category songs'), category_id)
  220. entries = [
  221. self.url_result(
  222. "http://www.kuwo.cn/yinyue/%s/" % song['musicrid'],
  223. 'Kuwo', song['musicrid'])
  224. for song in jsonm['musiclist']
  225. ]
  226. return self.playlist_result(entries, category_id, category_name, category_desc)
  227. class KuwoMvIE(KuwoIE):
  228. IE_NAME = 'kuwo:mv'
  229. _VALID_URL = r'http://www\.kuwo\.cn/mv/(?P<id>[0-9]+?)/'
  230. _TESTS = [{
  231. 'url': 'http://www.kuwo.cn/mv/6480076/',
  232. 'info_dict': {
  233. 'id': '6480076',
  234. 'ext': 'mkv',
  235. 'title': '我们家MV',
  236. 'creator': '2PM',
  237. },
  238. }]
  239. _FORMATS = KuwoIE._FORMATS + [
  240. {'format': 'mkv', 'ext': 'mkv', 'preference': 250},
  241. {'format': 'mp4', 'ext': 'mp4', 'preference': 200},
  242. ]
  243. def _real_extract(self, url):
  244. song_id = self._match_id(url)
  245. webpage = self._download_webpage(
  246. url, song_id, note='Download mv detail info: %s' % song_id,
  247. errnote='Unable to get mv detail info: %s' % song_id)
  248. mobj = re.search(
  249. r'<h1 title="(?P<song>.+?)">[^<>]+<span .+?title="(?P<singer>.+?)".+?>[^<>]+</span></h1>',
  250. webpage)
  251. if mobj:
  252. song_name = mobj.group('song')
  253. singer_name = mobj.group('singer')
  254. else:
  255. raise ExtractorError("Unable to find song or singer names")
  256. formats = self._get_formats(song_id)
  257. return {
  258. 'id': song_id,
  259. 'title': song_name,
  260. 'creator': singer_name,
  261. 'formats': formats,
  262. }