1 from __future__ 
import unicode_literals
 
   8 from .common 
import InfoExtractor
 
  12     compat_urllib_parse_unquote
, 
  23 class MixcloudIE(InfoExtractor
): 
  24     _VALID_URL 
= r
'https?://(?:(?:www|beta|m)\.)?mixcloud\.com/([^/]+)/(?!stream|uploads|favorites|listens|playlists)([^/]+)' 
  28         'url': 'http://www.mixcloud.com/dholbach/cryptkeeper/', 
  30             'id': 'dholbach-cryptkeeper', 
  32             'title': 'Cryptkeeper', 
  33             'description': 'After quite a long silence from myself, finally another Drum\'n\'Bass mix with my favourite current dance floor bangers.', 
  34             'uploader': 'Daniel Holbach', 
  35             'uploader_id': 'dholbach', 
  36             'thumbnail': r
're:https?://.*\.jpg', 
  40         'url': 'http://www.mixcloud.com/gillespeterson/caribou-7-inch-vinyl-mix-chat/', 
  42             'id': 'gillespeterson-caribou-7-inch-vinyl-mix-chat', 
  44             'title': 'Caribou 7 inch Vinyl Mix & Chat', 
  45             'description': 'md5:2b8aec6adce69f9d41724647c65875e8', 
  46             'uploader': 'Gilles Peterson Worldwide', 
  47             'uploader_id': 'gillespeterson', 
  48             'thumbnail': 're:https?://.*', 
  52         'url': 'https://beta.mixcloud.com/RedLightRadio/nosedrip-15-red-light-radio-01-18-2016/', 
  53         'only_matching': True, 
  56     # See https://www.mixcloud.com/media/js2/www_js_2.9e23256562c080482435196ca3975ab5.js 
  58     def _decrypt_play_info(play_info
): 
  59         KEY 
= 'pleasedontdownloadourmusictheartistswontgetpaid' 
  61         play_info 
= base64
.b64decode(play_info
.encode('ascii')) 
  64             compat_chr(compat_ord(ch
) ^ 
compat_ord(KEY
[idx 
% len(KEY
)])) 
  65             for idx
, ch 
in enumerate(play_info
)]) 
  67     def _real_extract(self
, url
): 
  68         mobj 
= re
.match(self
._VALID
_URL
, url
) 
  69         uploader 
= mobj
.group(1) 
  70         cloudcast_name 
= mobj
.group(2) 
  71         track_id 
= compat_urllib_parse_unquote('-'.join((uploader
, cloudcast_name
))) 
  73         webpage 
= self
._download
_webpage
(url
, track_id
) 
  75         message 
= self
._html
_search
_regex
( 
  76             r
'(?s)<div[^>]+class="global-message cloudcast-disabled-notice-light"[^>]*>(.+?)<(?:a|/div)', 
  77             webpage
, 'error message', default
=None) 
  79         encrypted_play_info 
= self
._search
_regex
( 
  80             r
'm-play-info="([^"]+)"', webpage
, 'play info') 
  81         play_info 
= self
._parse
_json
( 
  82             self
._decrypt
_play
_info
(encrypted_play_info
), track_id
) 
  84         if message 
and 'stream_url' not in play_info
: 
  85             raise ExtractorError('%s said: %s' % (self
.IE_NAME
, message
), expected
=True) 
  87         song_url 
= play_info
['stream_url'] 
  89         title 
= self
._html
_search
_regex
(r
'm-title="([^"]+)"', webpage
, 'title') 
  90         thumbnail 
= self
._proto
_relative
_url
(self
._html
_search
_regex
( 
  91             r
'm-thumbnail-url="([^"]+)"', webpage
, 'thumbnail', fatal
=False)) 
  92         uploader 
= self
._html
_search
_regex
( 
  93             r
'm-owner-name="([^"]+)"', webpage
, 'uploader', fatal
=False) 
  94         uploader_id 
= self
._search
_regex
( 
  95             r
'\s+"profile": "([^"]+)",', webpage
, 'uploader id', fatal
=False) 
  96         description 
= self
._og
_search
_description
(webpage
) 
  97         view_count 
= str_to_int(self
._search
_regex
( 
  98             [r
'<meta itemprop="interactionCount" content="UserPlays:([0-9]+)"', 
  99              r
'/listeners/?">([0-9,.]+)</a>', 
 100              r
'm-tooltip=["\']([\d
,.]+) plays
'], 
 101             webpage, 'play count
', default=None)) 
 107             'description
': description, 
 108             'thumbnail
': thumbnail, 
 109             'uploader
': uploader, 
 110             'uploader_id
': uploader_id, 
 111             'view_count
': view_count, 
 115 class MixcloudPlaylistBaseIE(InfoExtractor): 
 118     def _find_urls_in_page(self, page): 
 119         for url in re.findall(r'm
-play
-button m
-url
="(?P<url>[^"]+)"', page): 
 120             yield self.url_result( 
 121                 compat_urlparse.urljoin('https://www.mixcloud.com', clean_html(url)), 
 124     def _fetch_tracks_page(self, path, video_id, page_name, current_page, real_page_number=None): 
 125         real_page_number = real_page_number or current_page + 1 
 126         return self._download_webpage( 
 127             'https://www.mixcloud.com/%s/' % path, video_id, 
 128             note='Download %s (page %d)' % (page_name, current_page + 1), 
 129             errnote='Unable to download %s' % page_name, 
 130             query={'page': real_page_number, 'list': 'main', '_ajax': '1'}, 
 131             headers={'X-Requested-With': 'XMLHttpRequest'}) 
 133     def _tracks_page_func(self, page, video_id, page_name, current_page): 
 134         resp = self._fetch_tracks_page(page, video_id, page_name, current_page) 
 136         for item in self._find_urls_in_page(resp): 
 139     def _get_user_description(self, page_content): 
 140         return self._html_search_regex( 
 141             r'<div[^>]+class="description
-text
"[^>]*>(.+?)</div>', 
 142             page_content, 'user description', fatal=False) 
 145 class MixcloudUserIE(MixcloudPlaylistBaseIE): 
 146     _VALID_URL = r'^(?:https?://)?(?:www\.)?mixcloud\.com/(?P<user>[^/]+)/(?P<type>uploads|favorites|listens)?/?$' 
 147     IE_NAME = 'mixcloud:user' 
 150         'url': 'http://www.mixcloud.com/dholbach/', 
 152             'id': 'dholbach_uploads', 
 153             'title': 'Daniel Holbach (uploads)', 
 154             'description': 'md5:327af72d1efeb404a8216c27240d1370', 
 156         'playlist_mincount': 11, 
 158         'url': 'http://www.mixcloud.com/dholbach/uploads/', 
 160             'id': 'dholbach_uploads', 
 161             'title': 'Daniel Holbach (uploads)', 
 162             'description': 'md5:327af72d1efeb404a8216c27240d1370', 
 164         'playlist_mincount': 11, 
 166         'url': 'http://www.mixcloud.com/dholbach/favorites/', 
 168             'id': 'dholbach_favorites', 
 169             'title': 'Daniel Holbach (favorites)', 
 170             'description': 'md5:327af72d1efeb404a8216c27240d1370', 
 173             'playlist_items': '1-100', 
 175         'playlist_mincount': 100, 
 177         'url': 'http://www.mixcloud.com/dholbach/listens/', 
 179             'id': 'dholbach_listens', 
 180             'title': 'Daniel Holbach (listens)', 
 181             'description': 'md5:327af72d1efeb404a8216c27240d1370', 
 184             'playlist_items': '1-100', 
 186         'playlist_mincount': 100, 
 189     def _real_extract(self, url): 
 190         mobj = re.match(self._VALID_URL, url) 
 191         user_id = mobj.group('user') 
 192         list_type = mobj.group('type') 
 194         # if only a profile URL was supplied, default to download all uploads 
 195         if list_type is None: 
 196             list_type = 'uploads' 
 198         video_id = '%s_%s' % (user_id, list_type) 
 200         profile = self._download_webpage( 
 201             'https://www.mixcloud.com/%s/' % user_id, video_id, 
 202             note='Downloading user profile', 
 203             errnote='Unable to download user profile') 
 205         username = self._og_search_title(profile) 
 206         description = self._get_user_description(profile) 
 208         entries = OnDemandPagedList( 
 210                 self._tracks_page_func, 
 211                 '%s/%s' % (user_id, list_type), video_id, 'list of %s' % list_type), 
 212             self._PAGE_SIZE, use_cache=True) 
 214         return self.playlist_result( 
 215             entries, video_id, '%s (%s)' % (username, list_type), description) 
 218 class MixcloudPlaylistIE(MixcloudPlaylistBaseIE): 
 219     _VALID_URL = r'^(?:https?://)?(?:www\.)?mixcloud\.com/(?P<user>[^/]+)/playlists/(?P<playlist>[^/]+)/?$' 
 220     IE_NAME = 'mixcloud:playlist' 
 223         'url': 'https://www.mixcloud.com/RedBullThre3style/playlists/tokyo-finalists-2015/', 
 225             'id': 'RedBullThre3style_tokyo-finalists-2015', 
 226             'title': 'National Champions 2015', 
 227             'description': 'md5:6ff5fb01ac76a31abc9b3939c16243a3', 
 229         'playlist_mincount': 16, 
 231         'url': 'https://www.mixcloud.com/maxvibes/playlists/jazzcat-on-ness-radio/', 
 233             'id': 'maxvibes_jazzcat-on-ness-radio', 
 234             'title': 'Jazzcat on Ness Radio', 
 235             'description': 'md5:7bbbf0d6359a0b8cda85224be0f8f263', 
 237         'playlist_mincount': 23 
 240     def _real_extract(self, url): 
 241         mobj = re.match(self._VALID_URL, url) 
 242         user_id = mobj.group('user') 
 243         playlist_id = mobj.group('playlist') 
 244         video_id = '%s_%s' % (user_id, playlist_id) 
 246         profile = self._download_webpage( 
 248             note='Downloading playlist page', 
 249             errnote='Unable to download playlist page') 
 251         description = self._get_user_description(profile) 
 252         playlist_title = self._html_search_regex( 
 253             r'<span[^>]+class="[^
"]*list-playlist-title[^"]*"[^>]*>(.*?)</span>', 
 254             profile, 'playlist title') 
 256         entries = OnDemandPagedList( 
 258                 self._tracks_page_func, 
 259                 '%s/playlists/%s' % (user_id, playlist_id), video_id, 'tracklist'), 
 262         return self.playlist_result(entries, video_id, playlist_title, description) 
 265 class MixcloudStreamIE(MixcloudPlaylistBaseIE): 
 266     _VALID_URL = r'^(?:https?://)?(?:www\.)?mixcloud\.com/(?P<id>[^/]+)/stream/?$' 
 267     IE_NAME = 'mixcloud:stream' 
 270         'url': 'https://www.mixcloud.com/FirstEar/stream/', 
 273             'title': 'First Ear', 
 274             'description': 'Curators of good music\nfirstearmusic.com', 
 276         'playlist_mincount': 192, 
 279     def _real_extract(self, url): 
 280         user_id = self._match_id(url) 
 282         webpage = self._download_webpage(url, user_id) 
 287         def _handle_page(page): 
 288             entries.extend(self._find_urls_in_page(page)) 
 289             return self._search_regex( 
 290                 r'm-next-page-url="([^
"]+)"', page, 
 291                 'next page URL
', default=None) 
 293         next_page_url = _handle_page(webpage) 
 295         for idx in itertools.count(0): 
 296             if not next_page_url or prev_page_url == next_page_url: 
 299             prev_page_url = next_page_url 
 300             current_page = int(self._search_regex( 
 301                 r'\?page
=(\d
+)', next_page_url, 'next page number
')) 
 303             next_page_url = _handle_page(self._fetch_tracks_page( 
 304                 '%s/stream
' % user_id, user_id, 'stream
', idx, 
 305                 real_page_number=current_page)) 
 307         username = self._og_search_title(webpage) 
 308         description = self._get_user_description(webpage) 
 310         return self.playlist_result(entries, user_id, username, description)