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