[moniker] Add support for builtin embedded videos (Closes #7244)
[youtube-dl] / youtube_dl / extractor / moniker.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import os.path
5 import re
6
7 from .common import InfoExtractor
8 from ..compat import (
9     compat_urllib_parse,
10     compat_urllib_request,
11 )
12 from ..utils import (
13     ExtractorError,
14     remove_start,
15 )
16
17
18 class MonikerIE(InfoExtractor):
19     IE_DESC = 'allmyvideos.net and vidspot.net'
20     _VALID_URL = r'https?://(?:www\.)?(?:allmyvideos|vidspot)\.net/(?:(?:2|v)/v-)?(?P<id>[a-zA-Z0-9_-]+)'
21
22     _TESTS = [{
23         'url': 'http://allmyvideos.net/jih3nce3x6wn',
24         'md5': '710883dee1bfc370ecf9fa6a89307c88',
25         'info_dict': {
26             'id': 'jih3nce3x6wn',
27             'ext': 'mp4',
28             'title': 'youtube-dl test video',
29         },
30     }, {
31         'url': 'http://allmyvideos.net/embed-jih3nce3x6wn',
32         'md5': '710883dee1bfc370ecf9fa6a89307c88',
33         'info_dict': {
34             'id': 'jih3nce3x6wn',
35             'ext': 'mp4',
36             'title': 'youtube-dl test video',
37         },
38     }, {
39         'url': 'http://vidspot.net/l2ngsmhs8ci5',
40         'md5': '710883dee1bfc370ecf9fa6a89307c88',
41         'info_dict': {
42             'id': 'l2ngsmhs8ci5',
43             'ext': 'mp4',
44             'title': 'youtube-dl test video',
45         },
46     }, {
47         'url': 'https://www.vidspot.net/l2ngsmhs8ci5',
48         'only_matching': True,
49     }]
50
51     def _real_extract(self, url):
52         orig_video_id = self._match_id(url)
53         video_id = remove_start(orig_video_id, 'embed-')
54         url = url.replace(orig_video_id, video_id)
55         assert re.match(self._VALID_URL, url) is not None
56         orig_webpage = self._download_webpage(url, video_id)
57
58         if '>File Not Found<' in orig_webpage:
59             raise ExtractorError('Video %s does not exist' % video_id, expected=True)
60
61         error = self._search_regex(
62             r'class="err">([^<]+)<', orig_webpage, 'error', default=None)
63         if error:
64             raise ExtractorError(
65                 '%s returned error: %s' % (self.IE_NAME, error), expected=True)
66
67         builtin_url = self._search_regex(
68             r'<iframe[^>]+src=(["\'])(?P<url>.+?/builtin-.+?)\1',
69             orig_webpage, 'builtin URL', default=None, group='url')
70
71         if builtin_url:
72             req = compat_urllib_request.Request(builtin_url)
73             req.add_header('Referer', url)
74             webpage = self._download_webpage(req, video_id, 'Downloading builtin page')
75             title = self._og_search_title(orig_webpage).strip()
76             description = self._og_search_description(orig_webpage).strip()
77         else:
78             fields = re.findall(r'type="hidden" name="(.+?)"\s* value="?(.+?)">', orig_webpage)
79             data = dict(fields)
80
81             post = compat_urllib_parse.urlencode(data)
82             headers = {
83                 b'Content-Type': b'application/x-www-form-urlencoded',
84             }
85             req = compat_urllib_request.Request(url, post, headers)
86             webpage = self._download_webpage(
87                 req, video_id, note='Downloading video page ...')
88
89             title = os.path.splitext(data['fname'])[0]
90             description = None
91
92         # Could be several links with different quality
93         links = re.findall(r'"file" : "?(.+?)",', webpage)
94         # Assume the links are ordered in quality
95         formats = [{
96             'url': l,
97             'quality': i,
98         } for i, l in enumerate(links)]
99         self._sort_formats(formats)
100
101         return {
102             'id': video_id,
103             'title': title,
104             'description': description,
105             'formats': formats,
106         }