]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/mixcloud.py
debian/copyright: Update my copyright years.
[youtubedl] / 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 str_to_int,
13 parse_iso8601,
14 )
15
16
17 class MixcloudIE(InfoExtractor):
18 _VALID_URL = r'^(?:https?://)?(?:www\.)?mixcloud\.com/([^/]+)/([^/]+)'
19 IE_NAME = 'mixcloud'
20
21 _TESTS = [{
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 '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': 'm4a',
41 'title': 'Electric Relaxation vol. 3',
42 'description': 'md5:2b8aec6adce69f9d41724647c65875e8',
43 'uploader': 'Daniel Drumz',
44 'uploader_id': 'gillespeterson',
45 'thumbnail': 're:https?://.*\.jpg',
46 'view_count': int,
47 'like_count': int,
48 },
49 }]
50
51 def _get_url(self, track_id, template_url):
52 server_count = 30
53 for i in range(server_count):
54 url = template_url % i
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 'Checking URL %d/%d ...' % (i + 1, server_count + 1))
61 return url
62 except ExtractorError:
63 pass
64
65 return None
66
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)))
72
73 webpage = self._download_webpage(url, track_id)
74
75 preview_url = self._search_regex(
76 r'\s(?:data-preview-url|m-preview)="([^"]+)"', webpage, 'preview url')
77 song_url = preview_url.replace('/previews/', '/c/originals/')
78 template_url = re.sub(r'(stream\d*)', 'stream%d', song_url)
79 final_song_url = self._get_url(track_id, template_url)
80 if final_song_url is None:
81 self.to_screen('Trying with m4a extension')
82 template_url = template_url.replace('.mp3', '.m4a').replace('originals/', 'm4a/64/')
83 final_song_url = self._get_url(track_id, template_url)
84 if final_song_url is None:
85 raise ExtractorError('Unable to extract track url')
86
87 PREFIX = (
88 r'<span class="play-button[^"]*?"'
89 r'(?:\s+[a-zA-Z0-9-]+(?:="[^"]+")?)*?\s+')
90 title = self._html_search_regex(
91 PREFIX + r'm-title="([^"]+)"', webpage, 'title')
92 thumbnail = self._proto_relative_url(self._html_search_regex(
93 PREFIX + r'm-thumbnail-url="([^"]+)"', webpage, 'thumbnail',
94 fatal=False))
95 uploader = self._html_search_regex(
96 PREFIX + r'm-owner-name="([^"]+)"',
97 webpage, 'uploader', fatal=False)
98 uploader_id = self._search_regex(
99 r'\s+"profile": "([^"]+)",', webpage, 'uploader id', fatal=False)
100 description = self._og_search_description(webpage)
101 like_count = str_to_int(self._search_regex(
102 [r'<meta itemprop="interactionCount" content="UserLikes:([0-9]+)"',
103 r'/favorites/?">([0-9]+)<'],
104 webpage, 'like count', fatal=False))
105 view_count = str_to_int(self._search_regex(
106 [r'<meta itemprop="interactionCount" content="UserPlays:([0-9]+)"',
107 r'/listeners/?">([0-9,.]+)</a>'],
108 webpage, 'play count', fatal=False))
109 timestamp = parse_iso8601(self._search_regex(
110 r'<time itemprop="dateCreated" datetime="([^"]+)">',
111 webpage, 'upload date', default=None))
112
113 return {
114 'id': track_id,
115 'title': title,
116 'url': final_song_url,
117 'description': description,
118 'thumbnail': thumbnail,
119 'uploader': uploader,
120 'uploader_id': uploader_id,
121 'timestamp': timestamp,
122 'view_count': view_count,
123 'like_count': like_count,
124 }