]> gitweb @ CieloNegro.org - youtube-dl.git/blob - youtube_dl/extractor/mixcloud.py
Fix imports and general cleanup
[youtube-dl.git] / youtube_dl / extractor / mixcloud.py
1 from __future__ import unicode_literals
2
3 import re
4
5 from .common import InfoExtractor
6 from ..compat import (
7     compat_urllib_parse,
8 )
9 from ..utils import (
10     ExtractorError,
11     HEADRequest,
12     int_or_none,
13     parse_iso8601,
14 )
15
16
17 class MixcloudIE(InfoExtractor):
18     _VALID_URL = r'^(?:https?://)?(?:www\.)?mixcloud\.com/([^/]+)/([^/]+)'
19     IE_NAME = 'mixcloud'
20
21     _TEST = {
22         'url': 'http://www.mixcloud.com/dholbach/cryptkeeper/',
23         'info_dict': {
24             'id': 'dholbach-cryptkeeper',
25             'ext': 'mp3',
26             'title': 'Cryptkeeper',
27             'description': 'After quite a long silence from myself, finally another Drum\'n\'Bass mix with my favourite current dance floor bangers.',
28             'uploader': 'Daniel Holbach',
29             'uploader_id': 'dholbach',
30             'upload_date': '20111115',
31             'timestamp': 1321359578,
32             'thumbnail': 're:https?://.*\.jpg',
33             'view_count': int,
34             'like_count': int,
35         },
36     }
37
38     def _get_url(self, track_id, template_url):
39         server_count = 30
40         for i in range(server_count):
41             url = template_url % i
42             try:
43                 # We only want to know if the request succeed
44                 # don't download the whole file
45                 self._request_webpage(
46                     HEADRequest(url), track_id,
47                     'Checking URL %d/%d ...' % (i + 1, server_count + 1))
48                 return url
49             except ExtractorError:
50                 pass
51
52         return None
53
54     def _real_extract(self, url):
55         mobj = re.match(self._VALID_URL, url)
56         uploader = mobj.group(1)
57         cloudcast_name = mobj.group(2)
58         track_id = compat_urllib_parse.unquote('-'.join((uploader, cloudcast_name)))
59
60         webpage = self._download_webpage(url, track_id)
61
62         preview_url = self._search_regex(
63             r'\s(?:data-preview-url|m-preview)="(.+?)"', webpage, 'preview url')
64         song_url = preview_url.replace('/previews/', '/c/originals/')
65         template_url = re.sub(r'(stream\d*)', 'stream%d', song_url)
66         final_song_url = self._get_url(track_id, template_url)
67         if final_song_url is None:
68             self.to_screen('Trying with m4a extension')
69             template_url = template_url.replace('.mp3', '.m4a').replace('originals/', 'm4a/64/')
70             final_song_url = self._get_url(track_id, template_url)
71         if final_song_url is None:
72             raise ExtractorError('Unable to extract track url')
73
74         PREFIX = (
75             r'<span class="play-button[^"]*?"'
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 = int_or_none(self._search_regex(
89             r'<meta itemprop="interactionCount" content="UserLikes:([0-9]+)"',
90             webpage, 'like count', fatal=False))
91         view_count = int_or_none(self._search_regex(
92             r'<meta itemprop="interactionCount" content="UserPlays:([0-9]+)"',
93             webpage, 'play count', fatal=False))
94         timestamp = parse_iso8601(self._search_regex(
95             r'<time itemprop="dateCreated" datetime="([^"]+)">',
96             webpage, 'upload date'))
97
98         return {
99             'id': track_id,
100             'title': title,
101             'url': final_song_url,
102             'description': description,
103             'thumbnail': thumbnail,
104             'uploader': uploader,
105             'uploader_id': uploader_id,
106             'timestamp': timestamp,
107             'view_count': view_count,
108             'like_count': like_count,
109         }