[imgur] improve error check for non-video URLs
[youtube-dl] / youtube_dl / extractor / imgur.py
1 from __future__ import unicode_literals
2
3 import re
4
5 from .common import InfoExtractor
6 from ..utils import (
7     int_or_none,
8     str_or_none,
9     js_to_json,
10     mimetype2ext,
11     ExtractorError,
12 )
13
14 class ImgurIE(InfoExtractor):
15     _VALID_URL = r'https?://(?:i\.)?imgur\.com/(?P<id>[a-zA-Z0-9]+)(?:\.mp4|\.gifv)?'
16
17     _TESTS = [{
18         'url': 'https://i.imgur.com/A61SaA1.gifv',
19         'info_dict': {
20             'id': 'A61SaA1',
21             'ext': 'mp4',
22             'title': 'MRW gifv is up and running without any bugs',
23             'description': 'The Internet\'s visual storytelling community. Explore, share, and discuss the best visual stories the Internet has to offer.',
24         },
25     }, {
26         'url': 'https://imgur.com/A61SaA1',
27         'info_dict': {
28             'id': 'A61SaA1',
29             'ext': 'mp4',
30             'title': 'MRW gifv is up and running without any bugs',
31             'description': 'The Internet\'s visual storytelling community. Explore, share, and discuss the best visual stories the Internet has to offer.',
32         },
33     }]
34
35     def _real_extract(self, url):
36         video_id = self._match_id(url)
37         webpage = self._download_webpage(url, video_id)
38
39         width = int_or_none(self._search_regex(
40             r'<param name="width" value="([0-9]+)"',
41             webpage, 'width', fatal=False))
42         height = int_or_none(self._search_regex(
43             r'<param name="height" value="([0-9]+)"',
44             webpage, 'height', fatal=False))
45
46         video_elements = str_or_none(self._search_regex(
47             r'(?s)<div class="video-elements">(.*?)</div>',
48             webpage, 'video elements', fatal=False))
49         if not video_elements:
50             raise ExtractorError(
51                 'No sources found for video %s' % video_id, expected=True)
52
53         formats = []
54         for m in re.finditer(r'<source\s+src="(?P<src>[^"]+)"\s+type="(?P<type>[^"]+)"', video_elements):
55             formats.append({
56                 'format_id': m.group('type').partition('/')[2],
57                 'url': self._proto_relative_url(m.group('src')),
58                 'ext': mimetype2ext(m.group('type')),
59                 'acodec': 'none',
60                 'width': width,
61                 'height': height,
62                 'http_headers': {
63                     'User-Agent': 'youtube-dl (like wget)',
64                 },
65             })
66
67         gif_json = self._search_regex(
68             r'(?s)var\s+videoItem\s*=\s*(\{.*?\})',
69             webpage, 'GIF code', fatal=False)
70         if gif_json:
71             gifd = self._parse_json(
72                 gif_json, video_id, transform_source=js_to_json)
73             formats.append({
74                 'format_id': 'gif',
75                 'preference': -10,
76                 'width': width,
77                 'height': height,
78                 'ext': 'gif',
79                 'acodec': 'none',
80                 'vcodec': 'gif',
81                 'container': 'gif',
82                 'url': self._proto_relative_url(gifd['gifUrl']),
83                 'filesize': gifd.get('size'),
84                 'http_headers': {
85                     'User-Agent': 'youtube-dl (like wget)',
86                 },
87             })
88
89         self._sort_formats(formats)
90
91         return {
92             'id': video_id,
93             'formats': formats,
94             'description': self._og_search_description(webpage),
95             'title': self._og_search_title(webpage),
96         }