]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/mixcloud.py
Update changelog.
[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 compat_urllib_parse_unquote
7 from ..utils import (
8 ExtractorError,
9 HEADRequest,
10 str_to_int,
11 )
12
13
14 class MixcloudIE(InfoExtractor):
15 _VALID_URL = r'^(?:https?://)?(?:www\.)?mixcloud\.com/([^/]+)/([^/]+)'
16 IE_NAME = 'mixcloud'
17
18 _TESTS = [{
19 'url': 'http://www.mixcloud.com/dholbach/cryptkeeper/',
20 'info_dict': {
21 'id': 'dholbach-cryptkeeper',
22 'ext': 'mp3',
23 'title': 'Cryptkeeper',
24 'description': 'After quite a long silence from myself, finally another Drum\'n\'Bass mix with my favourite current dance floor bangers.',
25 'uploader': 'Daniel Holbach',
26 'uploader_id': 'dholbach',
27 'thumbnail': 're:https?://.*\.jpg',
28 'view_count': int,
29 'like_count': int,
30 },
31 }, {
32 'url': 'http://www.mixcloud.com/gillespeterson/caribou-7-inch-vinyl-mix-chat/',
33 'info_dict': {
34 'id': 'gillespeterson-caribou-7-inch-vinyl-mix-chat',
35 'ext': 'mp3',
36 'title': 'Caribou 7 inch Vinyl Mix & Chat',
37 'description': 'md5:2b8aec6adce69f9d41724647c65875e8',
38 'uploader': 'Gilles Peterson Worldwide',
39 'uploader_id': 'gillespeterson',
40 'thumbnail': 're:https?://.*/images/',
41 'view_count': int,
42 'like_count': int,
43 },
44 }]
45
46 def _check_url(self, url, track_id, ext):
47 try:
48 # We only want to know if the request succeed
49 # don't download the whole file
50 self._request_webpage(
51 HEADRequest(url), track_id,
52 'Trying %s URL' % ext)
53 return True
54 except ExtractorError:
55 return False
56
57 def _real_extract(self, url):
58 mobj = re.match(self._VALID_URL, url)
59 uploader = mobj.group(1)
60 cloudcast_name = mobj.group(2)
61 track_id = compat_urllib_parse_unquote('-'.join((uploader, cloudcast_name)))
62
63 webpage = self._download_webpage(url, track_id)
64
65 preview_url = self._search_regex(
66 r'\s(?:data-preview-url|m-preview)="([^"]+)"', webpage, 'preview url')
67 song_url = re.sub(r'audiocdn(\d+)', r'stream\1', preview_url)
68 song_url = song_url.replace('/previews/', '/c/originals/')
69 if not self._check_url(song_url, track_id, 'mp3'):
70 song_url = song_url.replace('.mp3', '.m4a').replace('originals/', 'm4a/64/')
71 if not self._check_url(song_url, track_id, 'm4a'):
72 raise ExtractorError('Unable to extract track url')
73
74 PREFIX = (
75 r'm-play-on-spacebar[^>]+'
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 = str_to_int(self._search_regex(
89 r'\bbutton-favorite\b[^>]+m-ajax-toggle-count="([^"]+)"',
90 webpage, 'like count', fatal=False))
91 view_count = str_to_int(self._search_regex(
92 [r'<meta itemprop="interactionCount" content="UserPlays:([0-9]+)"',
93 r'/listeners/?">([0-9,.]+)</a>'],
94 webpage, 'play count', fatal=False))
95
96 return {
97 'id': track_id,
98 'title': title,
99 'url': song_url,
100 'description': description,
101 'thumbnail': thumbnail,
102 'uploader': uploader,
103 'uploader_id': uploader_id,
104 'view_count': view_count,
105 'like_count': like_count,
106 }