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