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