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.

316 lines
11 KiB

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