Merge branch 'shahid' of https://github.com/remitamine/youtube-dl into remitamine...
[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/(?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         fields = re.findall(r'type="hidden" name="(.+?)"\s* value="?(.+?)">', orig_webpage)
68         data = dict(fields)
69
70         post = compat_urllib_parse.urlencode(data)
71         headers = {
72             b'Content-Type': b'application/x-www-form-urlencoded',
73         }
74         req = compat_urllib_request.Request(url, post, headers)
75         webpage = self._download_webpage(
76             req, video_id, note='Downloading video page ...')
77
78         title = os.path.splitext(data['fname'])[0]
79
80         # Could be several links with different quality
81         links = re.findall(r'"file" : "?(.+?)",', webpage)
82         # Assume the links are ordered in quality
83         formats = [{
84             'url': l,
85             'quality': i,
86         } for i, l in enumerate(links)]
87         self._sort_formats(formats)
88
89         return {
90             'id': video_id,
91             'title': title,
92             'formats': formats,
93         }