7c0c4e50e7c00d5bf66b30ee77deabc562fe2f07
[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         'url': 'http://vidspot.net/2/v-ywDf99',
51         'md5': '5f8254ce12df30479428b0152fb8e7ba',
52         'info_dict': {
53             'id': 'ywDf99',
54             'ext': 'mp4',
55             'title': 'IL FAIT LE MALIN EN PORSHE CAYENNE ( mais pas pour longtemps)',
56             'description': 'IL FAIT LE MALIN EN PORSHE CAYENNE.',
57         },
58     }, {
59         'url': 'http://allmyvideos.net/v/v-HXZm5t',
60         'only_matching': True,
61     }]
62
63     def _real_extract(self, url):
64         orig_video_id = self._match_id(url)
65         video_id = remove_start(orig_video_id, 'embed-')
66         url = url.replace(orig_video_id, video_id)
67         assert re.match(self._VALID_URL, url) is not None
68         orig_webpage = self._download_webpage(url, video_id)
69
70         if '>File Not Found<' in orig_webpage:
71             raise ExtractorError('Video %s does not exist' % video_id, expected=True)
72
73         error = self._search_regex(
74             r'class="err">([^<]+)<', orig_webpage, 'error', default=None)
75         if error:
76             raise ExtractorError(
77                 '%s returned error: %s' % (self.IE_NAME, error), expected=True)
78
79         builtin_url = self._search_regex(
80             r'<iframe[^>]+src=(["\'])(?P<url>.+?/builtin-.+?)\1',
81             orig_webpage, 'builtin URL', default=None, group='url')
82
83         if builtin_url:
84             req = compat_urllib_request.Request(builtin_url)
85             req.add_header('Referer', url)
86             webpage = self._download_webpage(req, video_id, 'Downloading builtin page')
87             title = self._og_search_title(orig_webpage).strip()
88             description = self._og_search_description(orig_webpage).strip()
89         else:
90             fields = re.findall(r'type="hidden" name="(.+?)"\s* value="?(.+?)">', orig_webpage)
91             data = dict(fields)
92
93             post = compat_urllib_parse.urlencode(data)
94             headers = {
95                 b'Content-Type': b'application/x-www-form-urlencoded',
96             }
97             req = compat_urllib_request.Request(url, post, headers)
98             webpage = self._download_webpage(
99                 req, video_id, note='Downloading video page ...')
100
101             title = os.path.splitext(data['fname'])[0]
102             description = None
103
104         # Could be several links with different quality
105         links = re.findall(r'"file" : "?(.+?)",', webpage)
106         # Assume the links are ordered in quality
107         formats = [{
108             'url': l,
109             'quality': i,
110         } for i, l in enumerate(links)]
111         self._sort_formats(formats)
112
113         return {
114             'id': video_id,
115             'title': title,
116             'description': description,
117             'formats': formats,
118         }