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