]> jfr.im git - yt-dlp.git/blame - youtube_dl/extractor/hotnewhiphop.py
Rename compat_urllib_request_Request to sanitized_Request and move to utils
[yt-dlp.git] / youtube_dl / extractor / hotnewhiphop.py
CommitLineData
99f770ca
PH
1from __future__ import unicode_literals
2
5b66de88
JMS
3import base64
4
5from .common import InfoExtractor
5c40bba8 6from ..compat import (
99f770ca
PH
7 compat_urllib_parse,
8 compat_urllib_request,
5c40bba8
PH
9)
10from ..utils import (
99f770ca
PH
11 ExtractorError,
12 HEADRequest,
13)
5b66de88
JMS
14
15
16class HotNewHipHopIE(InfoExtractor):
59188de1 17 _VALID_URL = r'http://www\.hotnewhiphop\.com/.*\.(?P<id>.*)\.html'
6f5ac90c 18 _TEST = {
99f770ca 19 'url': 'http://www.hotnewhiphop.com/freddie-gibbs-lay-it-down-song.1435540.html',
99f770ca
PH
20 'md5': '2c2cd2f76ef11a9b3b581e8b232f3d96',
21 'info_dict': {
5c40bba8
PH
22 'id': '1435540',
23 'ext': 'mp3',
99f770ca 24 'title': 'Freddie Gibbs - Lay It Down'
6f5ac90c
PH
25 }
26 }
5b66de88
JMS
27
28 def _real_extract(self, url):
5c40bba8
PH
29 video_id = self._match_id(url)
30 webpage = self._download_webpage(url, video_id)
5b66de88 31
99f770ca 32 video_url_base64 = self._search_regex(
5c40bba8 33 r'data-path="(.*?)"', webpage, 'video URL', default=None)
ed54491c 34
99f770ca
PH
35 if video_url_base64 is None:
36 video_url = self._search_regex(
5c40bba8 37 r'"contentUrl" content="(.*?)"', webpage, 'content URL')
ed54491c 38 return self.url_result(video_url, ie='Youtube')
5b66de88 39
99f770ca
PH
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')
5b66de88 61
5c40bba8 62 video_title = self._og_search_title(webpage).strip()
5b66de88 63
99f770ca
PH
64 return {
65 'id': video_id,
66 'url': video_url,
67 'title': video_title,
5c40bba8 68 'thumbnail': self._og_search_thumbnail(webpage),
99f770ca 69 }