[videomega] Extend _VALID_URL and improve extraction (Closes #6260)
[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 compat_urllib_request
8
9
10 class VideoMegaIE(InfoExtractor):
11     _VALID_URL = r'https?://(?:www\.)?videomega\.tv/(?:(?:view|iframe|cdn)\.php)?\?ref=(?P<id>[A-Za-z0-9]+)'
12     _TEST = {
13         'url': 'http://videomega.tv/cdn.php?ref=AOSQBJYKIDDIKYJBQSOA&width=1070&height=600',
14         'md5': 'cc1920a58add3f05c6a93285b84fb3aa',
15         'info_dict': {
16             'id': 'AOSQBJYKIDDIKYJBQSOA',
17             'ext': 'mp4',
18             'title': '1254207',
19             'thumbnail': 're:^https?://.*\.jpg$',
20         }
21     }
22
23     def _real_extract(self, url):
24         video_id = self._match_id(url)
25
26         req = compat_urllib_request.Request(url)
27         req.add_header('Referer', url)
28         req.add_header('Cookie', 'noadvtday=0')
29         webpage = self._download_webpage(req, video_id)
30
31         title = self._html_search_regex(
32             r'<title>(.+?)</title>', webpage, 'title')
33         title = re.sub(
34             r'(?:^[Vv]ideo[Mm]ega\.tv\s-\s*|\s*-\svideomega\.tv$)', '', title)
35         thumbnail = self._search_regex(
36             r'<video[^>]+?poster="([^"]+)"', webpage, 'thumbnail', fatal=False)
37         video_url = self._search_regex(
38             r'<source[^>]+?src="([^"]+)"', webpage, 'video URL')
39
40         return {
41             'id': video_id,
42             'title': title,
43             'url': video_url,
44             'thumbnail': thumbnail,
45             'http_headers': {
46                 'Referer': url,
47             },
48         }