kuwo.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..utils import (
  6. get_element_by_id,
  7. clean_html,
  8. ExtractorError,
  9. InAdvancePagedList,
  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, tolerate_ip_deny=False):
  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 == 'IPDeny' and not tolerate_ip_deny:
  30. raise ExtractorError('This song is blocked in this region', expected=True)
  31. if song_url.startswith('http://') or song_url.startswith('https://'):
  32. formats.append({
  33. 'url': song_url,
  34. 'format_id': file_format['format'],
  35. 'format': file_format['format'],
  36. 'preference': file_format['preference'],
  37. 'abr': file_format.get('abr'),
  38. })
  39. return formats
  40. class KuwoIE(KuwoBaseIE):
  41. IE_NAME = 'kuwo:song'
  42. IE_DESC = '酷我音乐'
  43. _VALID_URL = r'https?://www\.kuwo\.cn/yinyue/(?P<id>\d+)'
  44. _TESTS = [{
  45. 'url': 'http://www.kuwo.cn/yinyue/635632/',
  46. 'info_dict': {
  47. 'id': '635632',
  48. 'ext': 'ape',
  49. 'title': '爱我别走',
  50. 'creator': '张震岳',
  51. 'upload_date': '20080122',
  52. 'description': 'md5:ed13f58e3c3bf3f7fd9fbc4e5a7aa75c'
  53. },
  54. 'skip': 'this song has been offline because of copyright issues',
  55. }, {
  56. 'url': 'http://www.kuwo.cn/yinyue/6446136/',
  57. 'info_dict': {
  58. 'id': '6446136',
  59. 'ext': 'mp3',
  60. 'title': '心',
  61. 'description': 'md5:b2ab6295d014005bfc607525bfc1e38a',
  62. 'creator': 'IU',
  63. 'upload_date': '20150518',
  64. },
  65. 'params': {
  66. 'format': 'mp3-320'
  67. },
  68. }, {
  69. 'url': 'http://www.kuwo.cn/yinyue/3197154?catalog=yueku2016',
  70. 'only_matching': True,
  71. }]
  72. def _real_extract(self, url):
  73. song_id = self._match_id(url)
  74. webpage = self._download_webpage(
  75. url, song_id, note='Download song detail info',
  76. errnote='Unable to get song detail info')
  77. if '对不起,该歌曲由于版权问题已被下线,将返回网站首页' in webpage:
  78. raise ExtractorError('this song has been offline because of copyright issues', expected=True)
  79. song_name = self._html_search_regex(
  80. r'(?s)class="(?:[^"\s]+\s+)*title(?:\s+[^"\s]+)*".*?<h1[^>]+title="([^"]+)"', webpage, 'song name')
  81. singer_name = self._html_search_regex(
  82. r'<div[^>]+class="s_img">\s*<a[^>]+title="([^>]+)"',
  83. webpage, 'singer name', fatal=False)
  84. lrc_content = clean_html(get_element_by_id('lrcContent', webpage))
  85. if lrc_content == '暂无': # indicates no lyrics
  86. lrc_content = None
  87. formats = self._get_formats(song_id)
  88. self._sort_formats(formats)
  89. album_id = self._html_search_regex(
  90. r'<p[^>]+class="album"[^<]+<a[^>]+href="http://www\.kuwo\.cn/album/(\d+)/"',
  91. webpage, 'album id', fatal=False)
  92. publish_time = None
  93. if album_id is not None:
  94. album_info_page = self._download_webpage(
  95. 'http://www.kuwo.cn/album/%s/' % album_id, song_id,
  96. note='Download album detail info',
  97. errnote='Unable to get album detail info')
  98. publish_time = self._html_search_regex(
  99. r'发行时间:(\d{4}-\d{2}-\d{2})', album_info_page,
  100. 'publish time', fatal=False)
  101. if publish_time:
  102. publish_time = publish_time.replace('-', '')
  103. return {
  104. 'id': song_id,
  105. 'title': song_name,
  106. 'creator': singer_name,
  107. 'upload_date': publish_time,
  108. 'description': lrc_content,
  109. 'formats': formats,
  110. }
  111. class KuwoAlbumIE(InfoExtractor):
  112. IE_NAME = 'kuwo:album'
  113. IE_DESC = '酷我音乐 - 专辑'
  114. _VALID_URL = r'https?://www\.kuwo\.cn/album/(?P<id>\d+?)/'
  115. _TEST = {
  116. 'url': 'http://www.kuwo.cn/album/502294/',
  117. 'info_dict': {
  118. 'id': '502294',
  119. 'title': 'M',
  120. 'description': 'md5:6a7235a84cc6400ec3b38a7bdaf1d60c',
  121. },
  122. 'playlist_count': 2,
  123. }
  124. def _real_extract(self, url):
  125. album_id = self._match_id(url)
  126. webpage = self._download_webpage(
  127. url, album_id, note='Download album info',
  128. errnote='Unable to get album info')
  129. album_name = self._html_search_regex(
  130. r'<div[^>]+class="comm"[^<]+<h1[^>]+title="([^"]+)"', webpage,
  131. 'album name')
  132. album_intro = remove_start(
  133. clean_html(get_element_by_id('intro', webpage)),
  134. '%s简介:' % album_name)
  135. entries = [
  136. self.url_result(song_url, 'Kuwo') for song_url in re.findall(
  137. r'<p[^>]+class="listen"><a[^>]+href="(http://www\.kuwo\.cn/yinyue/\d+/)"',
  138. webpage)
  139. ]
  140. return self.playlist_result(entries, album_id, album_name, album_intro)
  141. class KuwoChartIE(InfoExtractor):
  142. IE_NAME = 'kuwo:chart'
  143. IE_DESC = '酷我音乐 - 排行榜'
  144. _VALID_URL = r'https?://yinyue\.kuwo\.cn/billboard_(?P<id>[^.]+).htm'
  145. _TEST = {
  146. 'url': 'http://yinyue.kuwo.cn/billboard_香港中文龙虎榜.htm',
  147. 'info_dict': {
  148. 'id': '香港中文龙虎榜',
  149. },
  150. 'playlist_mincount': 10,
  151. }
  152. def _real_extract(self, url):
  153. chart_id = self._match_id(url)
  154. webpage = self._download_webpage(
  155. url, chart_id, note='Download chart info',
  156. errnote='Unable to get chart info')
  157. entries = [
  158. self.url_result(song_url, 'Kuwo') for song_url in re.findall(
  159. r'<a[^>]+href="(http://www\.kuwo\.cn/yinyue/\d+)', webpage)
  160. ]
  161. return self.playlist_result(entries, chart_id)
  162. class KuwoSingerIE(InfoExtractor):
  163. IE_NAME = 'kuwo:singer'
  164. IE_DESC = '酷我音乐 - 歌手'
  165. _VALID_URL = r'https?://www\.kuwo\.cn/mingxing/(?P<id>[^/]+)'
  166. _TESTS = [{
  167. 'url': 'http://www.kuwo.cn/mingxing/bruno+mars/',
  168. 'info_dict': {
  169. 'id': 'bruno+mars',
  170. 'title': 'Bruno Mars',
  171. },
  172. 'playlist_mincount': 329,
  173. }, {
  174. 'url': 'http://www.kuwo.cn/mingxing/Ali/music.htm',
  175. 'info_dict': {
  176. 'id': 'Ali',
  177. 'title': 'Ali',
  178. },
  179. 'playlist_mincount': 95,
  180. 'skip': 'Regularly stalls travis build', # See https://travis-ci.org/rg3/youtube-dl/jobs/78878540
  181. }]
  182. PAGE_SIZE = 15
  183. def _real_extract(self, url):
  184. singer_id = self._match_id(url)
  185. webpage = self._download_webpage(
  186. url, singer_id, note='Download singer info',
  187. errnote='Unable to get singer info')
  188. singer_name = self._html_search_regex(
  189. r'<h1>([^<]+)</h1>', webpage, 'singer name')
  190. artist_id = self._html_search_regex(
  191. r'data-artistid="(\d+)"', webpage, 'artist id')
  192. page_count = int(self._html_search_regex(
  193. r'data-page="(\d+)"', webpage, 'page count'))
  194. def page_func(page_num):
  195. webpage = self._download_webpage(
  196. 'http://www.kuwo.cn/artist/contentMusicsAjax',
  197. singer_id, note='Download song list page #%d' % (page_num + 1),
  198. errnote='Unable to get song list page #%d' % (page_num + 1),
  199. query={'artistId': artist_id, 'pn': page_num, 'rn': self.PAGE_SIZE})
  200. return [
  201. self.url_result(song_url, 'Kuwo') for song_url in re.findall(
  202. r'<div[^>]+class="name"><a[^>]+href="(http://www\.kuwo\.cn/yinyue/\d+)',
  203. webpage)
  204. ]
  205. entries = InAdvancePagedList(page_func, page_count, self.PAGE_SIZE)
  206. return self.playlist_result(entries, singer_id, singer_name)
  207. class KuwoCategoryIE(InfoExtractor):
  208. IE_NAME = 'kuwo:category'
  209. IE_DESC = '酷我音乐 - 分类'
  210. _VALID_URL = r'https?://yinyue\.kuwo\.cn/yy/cinfo_(?P<id>\d+?).htm'
  211. _TEST = {
  212. 'url': 'http://yinyue.kuwo.cn/yy/cinfo_86375.htm',
  213. 'info_dict': {
  214. 'id': '86375',
  215. 'title': '八十年代精选',
  216. 'description': '这些都是属于八十年代的回忆!',
  217. },
  218. 'playlist_count': 30,
  219. }
  220. def _real_extract(self, url):
  221. category_id = self._match_id(url)
  222. webpage = self._download_webpage(
  223. url, category_id, note='Download category info',
  224. errnote='Unable to get category info')
  225. category_name = self._html_search_regex(
  226. r'<h1[^>]+title="([^<>]+?)">[^<>]+?</h1>', webpage, 'category name')
  227. category_desc = remove_start(
  228. get_element_by_id('intro', webpage).strip(),
  229. '%s简介:' % category_name)
  230. jsonm = self._parse_json(self._html_search_regex(
  231. r'var\s+jsonm\s*=\s*([^;]+);', webpage, 'category songs'), category_id)
  232. entries = [
  233. self.url_result('http://www.kuwo.cn/yinyue/%s/' % song['musicrid'], 'Kuwo')
  234. for song in jsonm['musiclist']
  235. ]
  236. return self.playlist_result(entries, category_id, category_name, category_desc)
  237. class KuwoMvIE(KuwoBaseIE):
  238. IE_NAME = 'kuwo:mv'
  239. IE_DESC = '酷我音乐 - MV'
  240. _VALID_URL = r'https?://www\.kuwo\.cn/mv/(?P<id>\d+?)/'
  241. _TEST = {
  242. 'url': 'http://www.kuwo.cn/mv/6480076/',
  243. 'info_dict': {
  244. 'id': '6480076',
  245. 'ext': 'mp4',
  246. 'title': 'My HouseMV',
  247. 'creator': '2PM',
  248. },
  249. # In this video, music URLs (anti.s) are blocked outside China and
  250. # USA, while the MV URL (mvurl) is available globally, so force the MV
  251. # URL for consistent results in different countries
  252. 'params': {
  253. 'format': 'mv',
  254. },
  255. }
  256. _FORMATS = KuwoBaseIE._FORMATS + [
  257. {'format': 'mkv', 'ext': 'mkv', 'preference': 250},
  258. {'format': 'mp4', 'ext': 'mp4', 'preference': 200},
  259. ]
  260. def _real_extract(self, url):
  261. song_id = self._match_id(url)
  262. webpage = self._download_webpage(
  263. url, song_id, note='Download mv detail info: %s' % song_id,
  264. errnote='Unable to get mv detail info: %s' % song_id)
  265. mobj = re.search(
  266. r'<h1[^>]+title="(?P<song>[^"]+)">[^<]+<span[^>]+title="(?P<singer>[^"]+)"',
  267. webpage)
  268. if mobj:
  269. song_name = mobj.group('song')
  270. singer_name = mobj.group('singer')
  271. else:
  272. raise ExtractorError('Unable to find song or singer names')
  273. formats = self._get_formats(song_id, tolerate_ip_deny=True)
  274. mv_url = self._download_webpage(
  275. 'http://www.kuwo.cn/yy/st/mvurl?rid=MUSIC_%s' % song_id,
  276. song_id, note='Download %s MV URL' % song_id)
  277. formats.append({
  278. 'url': mv_url,
  279. 'format_id': 'mv',
  280. })
  281. self._sort_formats(formats)
  282. return {
  283. 'id': song_id,
  284. 'title': song_name,
  285. 'creator': singer_name,
  286. 'formats': formats,
  287. }