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.

350 lines
12 KiB

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