[extractors] Use http_headers for setting the User-Agent and the Referer
[youtube-dl] / youtube_dl / extractor / videomega.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..compat import (
8     compat_urllib_parse,
9     compat_urllib_request,
10 )
11 from ..utils import (
12     ExtractorError,
13     remove_start,
14 )
15
16
17 class VideoMegaIE(InfoExtractor):
18     _VALID_URL = r'''(?x)https?://
19         (?:www\.)?videomega\.tv/
20         (?:iframe\.php)?\?ref=(?P<id>[A-Za-z0-9]+)
21         '''
22     _TEST = {
23         'url': 'http://videomega.tv/?ref=QR0HCUHI1661IHUCH0RQ',
24         'md5': 'bf5c2f95c4c917536e80936af7bc51e1',
25         'info_dict': {
26             'id': 'QR0HCUHI1661IHUCH0RQ',
27             'ext': 'mp4',
28             'title': 'Big Buck Bunny',
29             'thumbnail': 're:^https?://.*\.jpg$',
30         }
31     }
32
33     def _real_extract(self, url):
34         video_id = self._match_id(url)
35
36         iframe_url = 'http://videomega.tv/iframe.php?ref={0:}'.format(video_id)
37         req = compat_urllib_request.Request(iframe_url)
38         req.add_header('Referer', url)
39         webpage = self._download_webpage(req, video_id)
40
41         try:
42             escaped_data = re.findall(r'unescape\("([^"]+)"\)', webpage)[-1]
43         except IndexError:
44             raise ExtractorError('Unable to extract escaped data')
45
46         playlist = compat_urllib_parse.unquote(escaped_data)
47
48         thumbnail = self._search_regex(
49             r'image:\s*"([^"]+)"', playlist, 'thumbnail', fatal=False)
50         video_url = self._search_regex(r'file:\s*"([^"]+)"', playlist, 'URL')
51         title = remove_start(self._html_search_regex(
52             r'<title>(.*?)</title>', webpage, 'title'), 'VideoMega.tv - ')
53
54         formats = [{
55             'format_id': 'sd',
56             'url': video_url,
57         }]
58         self._sort_formats(formats)
59
60         return {
61             'id': video_id,
62             'title': title,
63             'formats': formats,
64             'thumbnail': thumbnail,
65             'http_headers': {
66                 'Referer': iframe_url,
67             },
68         }