]> gitweb @ CieloNegro.org - youtube-dl.git/blob - youtube_dl/extractor/hotnewhiphop.py
[generic] remove unused imports
[youtube-dl.git] / youtube_dl / extractor / hotnewhiphop.py
1 from __future__ import unicode_literals
2
3 import re
4 import base64
5
6 from .common import InfoExtractor
7 from ..utils import (
8     compat_urllib_parse,
9     compat_urllib_request,
10     ExtractorError,
11     HEADRequest,
12 )
13
14
15 class HotNewHipHopIE(InfoExtractor):
16     _VALID_URL = r'http://www\.hotnewhiphop\.com/.*\.(?P<id>.*)\.html'
17     _TEST = {
18         'url': 'http://www.hotnewhiphop.com/freddie-gibbs-lay-it-down-song.1435540.html',
19         'file': '1435540.mp3',
20         'md5': '2c2cd2f76ef11a9b3b581e8b232f3d96',
21         'info_dict': {
22             'title': 'Freddie Gibbs - Lay It Down'
23         }
24     }
25
26     def _real_extract(self, url):
27         m = re.match(self._VALID_URL, url)
28         video_id = m.group('id')
29
30         webpage_src = self._download_webpage(url, video_id)
31
32         video_url_base64 = self._search_regex(
33             r'data-path="(.*?)"', webpage_src, u'video URL', fatal=False)
34
35         if video_url_base64 is None:
36             video_url = self._search_regex(
37                 r'"contentUrl" content="(.*?)"', webpage_src, u'video URL')
38             return self.url_result(video_url, ie='Youtube')
39
40         reqdata = compat_urllib_parse.urlencode([
41             ('mediaType', 's'),
42             ('mediaId', video_id),
43         ])
44         r = compat_urllib_request.Request(
45             'http://www.hotnewhiphop.com/ajax/media/getActions/', data=reqdata)
46         r.add_header('Content-Type', 'application/x-www-form-urlencoded')
47         mkd = self._download_json(
48             r, video_id, note='Requesting media key',
49             errnote='Could not download media key')
50         if 'mediaKey' not in mkd:
51             raise ExtractorError('Did not get a media key')
52
53         redirect_url = base64.b64decode(video_url_base64).decode('utf-8')
54         redirect_req = HEADRequest(redirect_url)
55         req = self._request_webpage(
56             redirect_req, video_id,
57             note='Resolving final URL', errnote='Could not resolve final URL')
58         video_url = req.geturl()
59         if video_url.endswith('.html'):
60             raise ExtractorError('Redirect failed')
61
62         video_title = self._og_search_title(webpage_src).strip()
63
64         return {
65             'id': video_id,
66             'url': video_url,
67             'title': video_title,
68             'thumbnail': self._og_search_thumbnail(webpage_src),
69         }