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