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.

319 lines
11 KiB

  1. from __future__ import unicode_literals
  2. import base64
  3. import functools
  4. import itertools
  5. import re
  6. from .common import InfoExtractor
  7. from ..compat import (
  8. compat_chr,
  9. compat_ord,
  10. compat_urllib_parse_unquote,
  11. compat_urlparse,
  12. )
  13. from ..utils import (
  14. clean_html,
  15. ExtractorError,
  16. OnDemandPagedList,
  17. parse_count,
  18. str_to_int,
  19. )
  20. class MixcloudIE(InfoExtractor):
  21. _VALID_URL = r'^(?:https?://)?(?:www\.)?mixcloud\.com/([^/]+)/(?!stream|uploads|favorites|listens|playlists)([^/]+)'
  22. IE_NAME = 'mixcloud'
  23. _TESTS = [{
  24. 'url': 'http://www.mixcloud.com/dholbach/cryptkeeper/',
  25. 'info_dict': {
  26. 'id': 'dholbach-cryptkeeper',
  27. 'ext': 'm4a',
  28. 'title': 'Cryptkeeper',
  29. 'description': 'After quite a long silence from myself, finally another Drum\'n\'Bass mix with my favourite current dance floor bangers.',
  30. 'uploader': 'Daniel Holbach',
  31. 'uploader_id': 'dholbach',
  32. 'thumbnail': 're:https?://.*\.jpg',
  33. 'view_count': int,
  34. 'like_count': int,
  35. },
  36. }, {
  37. 'url': 'http://www.mixcloud.com/gillespeterson/caribou-7-inch-vinyl-mix-chat/',
  38. 'info_dict': {
  39. 'id': 'gillespeterson-caribou-7-inch-vinyl-mix-chat',
  40. 'ext': 'mp3',
  41. 'title': 'Caribou 7 inch Vinyl Mix & Chat',
  42. 'description': 'md5:2b8aec6adce69f9d41724647c65875e8',
  43. 'uploader': 'Gilles Peterson Worldwide',
  44. 'uploader_id': 'gillespeterson',
  45. 'thumbnail': 're:https?://.*',
  46. 'view_count': int,
  47. 'like_count': int,
  48. },
  49. }]
  50. # See https://www.mixcloud.com/media/js2/www_js_2.9e23256562c080482435196ca3975ab5.js
  51. @staticmethod
  52. def _decrypt_play_info(play_info):
  53. KEY = 'pleasedontdownloadourmusictheartistswontgetpaid'
  54. play_info = base64.b64decode(play_info.encode('ascii'))
  55. return ''.join([
  56. compat_chr(compat_ord(ch) ^ compat_ord(KEY[idx % len(KEY)]))
  57. for idx, ch in enumerate(play_info)])
  58. def _real_extract(self, url):
  59. mobj = re.match(self._VALID_URL, url)
  60. uploader = mobj.group(1)
  61. cloudcast_name = mobj.group(2)
  62. track_id = compat_urllib_parse_unquote('-'.join((uploader, cloudcast_name)))
  63. webpage = self._download_webpage(url, track_id)
  64. message = self._html_search_regex(
  65. r'(?s)<div[^>]+class="global-message cloudcast-disabled-notice-light"[^>]*>(.+?)<(?:a|/div)',
  66. webpage, 'error message', default=None)
  67. encrypted_play_info = self._search_regex(
  68. r'm-play-info="([^"]+)"', webpage, 'play info')
  69. play_info = self._parse_json(
  70. self._decrypt_play_info(encrypted_play_info), track_id)
  71. if message and 'stream_url' not in play_info:
  72. raise ExtractorError('%s said: %s' % (self.IE_NAME, message), expected=True)
  73. song_url = play_info['stream_url']
  74. PREFIX = (
  75. r'm-play-on-spacebar[^>]+'
  76. r'(?:\s+[a-zA-Z0-9-]+(?:="[^"]+")?)*?\s+')
  77. title = self._html_search_regex(
  78. PREFIX + r'm-title="([^"]+)"', webpage, 'title')
  79. thumbnail = self._proto_relative_url(self._html_search_regex(
  80. PREFIX + r'm-thumbnail-url="([^"]+)"', webpage, 'thumbnail',
  81. fatal=False))
  82. uploader = self._html_search_regex(
  83. PREFIX + r'm-owner-name="([^"]+)"',
  84. webpage, 'uploader', fatal=False)
  85. uploader_id = self._search_regex(
  86. r'\s+"profile": "([^"]+)",', webpage, 'uploader id', fatal=False)
  87. description = self._og_search_description(webpage)
  88. like_count = parse_count(self._search_regex(
  89. r'\bbutton-favorite[^>]+>.*?<span[^>]+class=["\']toggle-number[^>]+>\s*([^<]+)',
  90. webpage, 'like count', default=None))
  91. view_count = str_to_int(self._search_regex(
  92. [r'<meta itemprop="interactionCount" content="UserPlays:([0-9]+)"',
  93. r'/listeners/?">([0-9,.]+)</a>'],
  94. webpage, 'play count', default=None))
  95. return {
  96. 'id': track_id,
  97. 'title': title,
  98. 'url': song_url,
  99. 'description': description,
  100. 'thumbnail': thumbnail,
  101. 'uploader': uploader,
  102. 'uploader_id': uploader_id,
  103. 'view_count': view_count,
  104. 'like_count': like_count,
  105. }
  106. class MixcloudPlaylistBaseIE(InfoExtractor):
  107. _PAGE_SIZE = 24
  108. def _find_urls_in_page(self, page):
  109. for url in re.findall(r'm-play-button m-url="(?P<url>[^"]+)"', page):
  110. yield self.url_result(
  111. compat_urlparse.urljoin('https://www.mixcloud.com', clean_html(url)),
  112. MixcloudIE.ie_key())
  113. def _fetch_tracks_page(self, path, video_id, page_name, current_page, real_page_number=None):
  114. real_page_number = real_page_number or current_page + 1
  115. return self._download_webpage(
  116. 'https://www.mixcloud.com/%s/' % path, video_id,
  117. note='Download %s (page %d)' % (page_name, current_page + 1),
  118. errnote='Unable to download %s' % page_name,
  119. query={'page': real_page_number, 'list': 'main', '_ajax': '1'},
  120. headers={'X-Requested-With': 'XMLHttpRequest'})
  121. def _tracks_page_func(self, page, video_id, page_name, current_page):
  122. resp = self._fetch_tracks_page(page, video_id, page_name, current_page)
  123. for item in self._find_urls_in_page(resp):
  124. yield item
  125. def _get_user_description(self, page_content):
  126. return self._html_search_regex(
  127. r'<div[^>]+class="description-text"[^>]*>(.+?)</div>',
  128. page_content, 'user description', fatal=False)
  129. class MixcloudUserIE(MixcloudPlaylistBaseIE):
  130. _VALID_URL = r'^(?:https?://)?(?:www\.)?mixcloud\.com/(?P<user>[^/]+)/(?P<type>uploads|favorites|listens)?/?$'
  131. IE_NAME = 'mixcloud:user'
  132. _TESTS = [{
  133. 'url': 'http://www.mixcloud.com/dholbach/',
  134. 'info_dict': {
  135. 'id': 'dholbach_uploads',
  136. 'title': 'Daniel Holbach (uploads)',
  137. 'description': 'md5:327af72d1efeb404a8216c27240d1370',
  138. },
  139. 'playlist_mincount': 11,
  140. }, {
  141. 'url': 'http://www.mixcloud.com/dholbach/uploads/',
  142. 'info_dict': {
  143. 'id': 'dholbach_uploads',
  144. 'title': 'Daniel Holbach (uploads)',
  145. 'description': 'md5:327af72d1efeb404a8216c27240d1370',
  146. },
  147. 'playlist_mincount': 11,
  148. }, {
  149. 'url': 'http://www.mixcloud.com/dholbach/favorites/',
  150. 'info_dict': {
  151. 'id': 'dholbach_favorites',
  152. 'title': 'Daniel Holbach (favorites)',
  153. 'description': 'md5:327af72d1efeb404a8216c27240d1370',
  154. },
  155. 'params': {
  156. 'playlist_items': '1-100',
  157. },
  158. 'playlist_mincount': 100,
  159. }, {
  160. 'url': 'http://www.mixcloud.com/dholbach/listens/',
  161. 'info_dict': {
  162. 'id': 'dholbach_listens',
  163. 'title': 'Daniel Holbach (listens)',
  164. 'description': 'md5:327af72d1efeb404a8216c27240d1370',
  165. },
  166. 'params': {
  167. 'playlist_items': '1-100',
  168. },
  169. 'playlist_mincount': 100,
  170. }]
  171. def _real_extract(self, url):
  172. mobj = re.match(self._VALID_URL, url)
  173. user_id = mobj.group('user')
  174. list_type = mobj.group('type')
  175. # if only a profile URL was supplied, default to download all uploads
  176. if list_type is None:
  177. list_type = 'uploads'
  178. video_id = '%s_%s' % (user_id, list_type)
  179. profile = self._download_webpage(
  180. 'https://www.mixcloud.com/%s/' % user_id, video_id,
  181. note='Downloading user profile',
  182. errnote='Unable to download user profile')
  183. username = self._og_search_title(profile)
  184. description = self._get_user_description(profile)
  185. entries = OnDemandPagedList(
  186. functools.partial(
  187. self._tracks_page_func,
  188. '%s/%s' % (user_id, list_type), video_id, 'list of %s' % list_type),
  189. self._PAGE_SIZE, use_cache=True)
  190. return self.playlist_result(
  191. entries, video_id, '%s (%s)' % (username, list_type), description)
  192. class MixcloudPlaylistIE(MixcloudPlaylistBaseIE):
  193. _VALID_URL = r'^(?:https?://)?(?:www\.)?mixcloud\.com/(?P<user>[^/]+)/playlists/(?P<playlist>[^/]+)/?$'
  194. IE_NAME = 'mixcloud:playlist'
  195. _TESTS = [{
  196. 'url': 'https://www.mixcloud.com/RedBullThre3style/playlists/tokyo-finalists-2015/',
  197. 'info_dict': {
  198. 'id': 'RedBullThre3style_tokyo-finalists-2015',
  199. 'title': 'National Champions 2015',
  200. 'description': 'md5:6ff5fb01ac76a31abc9b3939c16243a3',
  201. },
  202. 'playlist_mincount': 16,
  203. }, {
  204. 'url': 'https://www.mixcloud.com/maxvibes/playlists/jazzcat-on-ness-radio/',
  205. 'info_dict': {
  206. 'id': 'maxvibes_jazzcat-on-ness-radio',
  207. 'title': 'Jazzcat on Ness Radio',
  208. 'description': 'md5:7bbbf0d6359a0b8cda85224be0f8f263',
  209. },
  210. 'playlist_mincount': 23
  211. }]
  212. def _real_extract(self, url):
  213. mobj = re.match(self._VALID_URL, url)
  214. user_id = mobj.group('user')
  215. playlist_id = mobj.group('playlist')
  216. video_id = '%s_%s' % (user_id, playlist_id)
  217. profile = self._download_webpage(
  218. url, user_id,
  219. note='Downloading playlist page',
  220. errnote='Unable to download playlist page')
  221. description = self._get_user_description(profile)
  222. playlist_title = self._html_search_regex(
  223. r'<span[^>]+class="[^"]*list-playlist-title[^"]*"[^>]*>(.*?)</span>',
  224. profile, 'playlist title')
  225. entries = OnDemandPagedList(
  226. functools.partial(
  227. self._tracks_page_func,
  228. '%s/playlists/%s' % (user_id, playlist_id), video_id, 'tracklist'),
  229. self._PAGE_SIZE)
  230. return self.playlist_result(entries, video_id, playlist_title, description)
  231. class MixcloudStreamIE(MixcloudPlaylistBaseIE):
  232. _VALID_URL = r'^(?:https?://)?(?:www\.)?mixcloud\.com/(?P<id>[^/]+)/stream/?$'
  233. IE_NAME = 'mixcloud:stream'
  234. _TEST = {
  235. 'url': 'https://www.mixcloud.com/FirstEar/stream/',
  236. 'info_dict': {
  237. 'id': 'FirstEar',
  238. 'title': 'First Ear',
  239. 'description': 'Curators of good music\nfirstearmusic.com',
  240. },
  241. 'playlist_mincount': 192,
  242. }
  243. def _real_extract(self, url):
  244. user_id = self._match_id(url)
  245. webpage = self._download_webpage(url, user_id)
  246. entries = []
  247. prev_page_url = None
  248. def _handle_page(page):
  249. entries.extend(self._find_urls_in_page(page))
  250. return self._search_regex(
  251. r'm-next-page-url="([^"]+)"', page,
  252. 'next page URL', default=None)
  253. next_page_url = _handle_page(webpage)
  254. for idx in itertools.count(0):
  255. if not next_page_url or prev_page_url == next_page_url:
  256. break
  257. prev_page_url = next_page_url
  258. current_page = int(self._search_regex(
  259. r'\?page=(\d+)', next_page_url, 'next page number'))
  260. next_page_url = _handle_page(self._fetch_tracks_page(
  261. '%s/stream' % user_id, user_id, 'stream', idx,
  262. real_page_number=current_page))
  263. username = self._og_search_title(webpage)
  264. description = self._get_user_description(webpage)
  265. return self.playlist_result(entries, user_id, username, description)