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.

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