]> git.bitcoin.ninja Git - youtube-dl/blob - youtube_dl/extractor/mixcloud.py
[pornhub:playlistbase] Use orderedSet
[youtube-dl] / 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     parse_count,
11     str_to_int,
12 )
13
14
15 class MixcloudIE(InfoExtractor):
16     _VALID_URL = r'^(?:https?://)?(?:www\.)?mixcloud\.com/([^/]+)/([^/]+)'
17     IE_NAME = 'mixcloud'
18
19     _TESTS = [{
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             'thumbnail': 're:https?://.*\.jpg',
29             'view_count': int,
30             'like_count': int,
31         },
32     }, {
33         'url': 'http://www.mixcloud.com/gillespeterson/caribou-7-inch-vinyl-mix-chat/',
34         'info_dict': {
35             'id': 'gillespeterson-caribou-7-inch-vinyl-mix-chat',
36             'ext': 'mp3',
37             'title': 'Caribou 7 inch Vinyl Mix & Chat',
38             'description': 'md5:2b8aec6adce69f9d41724647c65875e8',
39             'uploader': 'Gilles Peterson Worldwide',
40             'uploader_id': 'gillespeterson',
41             'thumbnail': 're:https?://.*/images/',
42             'view_count': int,
43             'like_count': int,
44         },
45     }]
46
47     def _check_url(self, url, track_id, ext):
48         try:
49             # We only want to know if the request succeed
50             # don't download the whole file
51             self._request_webpage(
52                 HEADRequest(url), track_id,
53                 'Trying %s URL' % ext)
54             return True
55         except ExtractorError:
56             return False
57
58     def _real_extract(self, url):
59         mobj = re.match(self._VALID_URL, url)
60         uploader = mobj.group(1)
61         cloudcast_name = mobj.group(2)
62         track_id = compat_urllib_parse_unquote('-'.join((uploader, cloudcast_name)))
63
64         webpage = self._download_webpage(url, track_id)
65
66         preview_url = self._search_regex(
67             r'\s(?:data-preview-url|m-preview)="([^"]+)"', webpage, 'preview url')
68         song_url = re.sub(r'audiocdn(\d+)', r'stream\1', preview_url)
69         song_url = song_url.replace('/previews/', '/c/originals/')
70         if not self._check_url(song_url, track_id, 'mp3'):
71             song_url = song_url.replace('.mp3', '.m4a').replace('originals/', 'm4a/64/')
72             if not self._check_url(song_url, track_id, 'm4a'):
73                 raise ExtractorError('Unable to extract track url')
74
75         PREFIX = (
76             r'm-play-on-spacebar[^>]+'
77             r'(?:\s+[a-zA-Z0-9-]+(?:="[^"]+")?)*?\s+')
78         title = self._html_search_regex(
79             PREFIX + r'm-title="([^"]+)"', webpage, 'title')
80         thumbnail = self._proto_relative_url(self._html_search_regex(
81             PREFIX + r'm-thumbnail-url="([^"]+)"', webpage, 'thumbnail',
82             fatal=False))
83         uploader = self._html_search_regex(
84             PREFIX + r'm-owner-name="([^"]+)"',
85             webpage, 'uploader', fatal=False)
86         uploader_id = self._search_regex(
87             r'\s+"profile": "([^"]+)",', webpage, 'uploader id', fatal=False)
88         description = self._og_search_description(webpage)
89         like_count = parse_count(self._search_regex(
90             r'\bbutton-favorite[^>]+>.*?<span[^>]+class=["\']toggle-number[^>]+>\s*([^<]+)',
91             webpage, 'like count', fatal=False))
92         view_count = str_to_int(self._search_regex(
93             [r'<meta itemprop="interactionCount" content="UserPlays:([0-9]+)"',
94              r'/listeners/?">([0-9,.]+)</a>'],
95             webpage, 'play count', fatal=False))
96
97         return {
98             'id': track_id,
99             'title': title,
100             'url': song_url,
101             'description': description,
102             'thumbnail': thumbnail,
103             'uploader': uploader,
104             'uploader_id': uploader_id,
105             'view_count': view_count,
106             'like_count': like_count,
107         }