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