]>
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): |
e1a0b3b8 | 16 | _VALID_URL = r'https?://(?:i\.)?imgur\.com/(?:(?:gallery|(?:topic|r)/[^/]+)/)?(?P<id>[a-zA-Z0-9]{6,})(?:[/?#&]+|\.[a-z]+)?$' |
3bf57053 PH |
17 | |
18 | _TESTS = [{ | |
19 | 'url': 'https://i.imgur.com/A61SaA1.gifv', | |
20 | 'info_dict': { | |
21 | 'id': 'A61SaA1', | |
22 | 'ext': 'mp4', | |
5e9a033e | 23 | 'title': 're:Imgur GIF$|MRW gifv is up and running without any bugs$', |
dbee18b5 | 24 | 'description': 'Imgur: The most awesome images on the Internet.', |
3bf57053 | 25 | }, |
1a13940c JB |
26 | }, { |
27 | 'url': 'https://imgur.com/A61SaA1', | |
28 | 'info_dict': { | |
29 | 'id': 'A61SaA1', | |
30 | 'ext': 'mp4', | |
5e9a033e | 31 | 'title': 're:Imgur GIF$|MRW gifv is up and running without any bugs$', |
dbee18b5 | 32 | 'description': 'Imgur: The most awesome images on the Internet.', |
1a13940c | 33 | }, |
dbee18b5 AK |
34 | }, { |
35 | 'url': 'https://imgur.com/gallery/YcAQlkx', | |
36 | 'info_dict': { | |
37 | 'id': 'YcAQlkx', | |
38 | 'ext': 'mp4', | |
39 | 'title': 'Classic Steve Carell gif...cracks me up everytime....damn the repost downvotes....', | |
40 | 'description': 'Imgur: The most awesome images on the Internet.' | |
41 | ||
42 | } | |
774ce355 S |
43 | }, { |
44 | 'url': 'http://imgur.com/topic/Funny/N8rOudd', | |
45 | 'only_matching': True, | |
e1a0b3b8 YCH |
46 | }, { |
47 | 'url': 'http://imgur.com/r/aww/VQcQPhM', | |
48 | 'only_matching': True, | |
3bf57053 PH |
49 | }] |
50 | ||
51 | def _real_extract(self, url): | |
52 | video_id = self._match_id(url) | |
96b96909 S |
53 | webpage = self._download_webpage( |
54 | compat_urlparse.urljoin(url, video_id), video_id) | |
3bf57053 | 55 | |
c2a453b4 S |
56 | width = int_or_none(self._og_search_property( |
57 | 'video:width', webpage, default=None)) | |
58 | height = int_or_none(self._og_search_property( | |
59 | 'video:height', webpage, default=None)) | |
3bf57053 | 60 | |
b88ba053 | 61 | video_elements = self._search_regex( |
3bf57053 | 62 | r'(?s)<div class="video-elements">(.*?)</div>', |
b88ba053 | 63 | webpage, 'video elements', default=None) |
9e2d7dca JB |
64 | if not video_elements: |
65 | raise ExtractorError( | |
b88ba053 PH |
66 | 'No sources found for video %s. Maybe an image?' % video_id, |
67 | expected=True) | |
9e2d7dca | 68 | |
3bf57053 PH |
69 | formats = [] |
70 | for m in re.finditer(r'<source\s+src="(?P<src>[^"]+)"\s+type="(?P<type>[^"]+)"', video_elements): | |
71 | formats.append({ | |
72 | 'format_id': m.group('type').partition('/')[2], | |
73 | 'url': self._proto_relative_url(m.group('src')), | |
74 | 'ext': mimetype2ext(m.group('type')), | |
75 | 'acodec': 'none', | |
76 | 'width': width, | |
77 | 'height': height, | |
78 | 'http_headers': { | |
79 | 'User-Agent': 'youtube-dl (like wget)', | |
80 | }, | |
81 | }) | |
82 | ||
83 | gif_json = self._search_regex( | |
84 | r'(?s)var\s+videoItem\s*=\s*(\{.*?\})', | |
85 | webpage, 'GIF code', fatal=False) | |
86 | if gif_json: | |
87 | gifd = self._parse_json( | |
88 | gif_json, video_id, transform_source=js_to_json) | |
89 | formats.append({ | |
90 | 'format_id': 'gif', | |
91 | 'preference': -10, | |
92 | 'width': width, | |
93 | 'height': height, | |
94 | 'ext': 'gif', | |
95 | 'acodec': 'none', | |
96 | 'vcodec': 'gif', | |
97 | 'container': 'gif', | |
98 | 'url': self._proto_relative_url(gifd['gifUrl']), | |
99 | 'filesize': gifd.get('size'), | |
100 | 'http_headers': { | |
101 | 'User-Agent': 'youtube-dl (like wget)', | |
102 | }, | |
103 | }) | |
104 | ||
105 | self._sort_formats(formats) | |
106 | ||
107 | return { | |
108 | 'id': video_id, | |
109 | 'formats': formats, | |
110 | 'description': self._og_search_description(webpage), | |
111 | 'title': self._og_search_title(webpage), | |
112 | } | |
8875b3d5 S |
113 | |
114 | ||
115 | class ImgurAlbumIE(InfoExtractor): | |
774ce355 | 116 | _VALID_URL = r'https?://(?:i\.)?imgur\.com/(?:(?:a|gallery|topic/[^/]+)/)?(?P<id>[a-zA-Z0-9]{5})(?:[/?#&]+)?$' |
8875b3d5 | 117 | |
774ce355 | 118 | _TESTS = [{ |
8875b3d5 S |
119 | 'url': 'http://imgur.com/gallery/Q95ko', |
120 | 'info_dict': { | |
121 | 'id': 'Q95ko', | |
122 | }, | |
123 | 'playlist_count': 25, | |
774ce355 S |
124 | }, { |
125 | 'url': 'http://imgur.com/a/j6Orj', | |
126 | 'only_matching': True, | |
127 | }, { | |
128 | 'url': 'http://imgur.com/topic/Aww/ll5Vk', | |
129 | 'only_matching': True, | |
130 | }] | |
8875b3d5 S |
131 | |
132 | def _real_extract(self, url): | |
133 | album_id = self._match_id(url) | |
134 | ||
774ce355 S |
135 | album_images = self._download_json( |
136 | 'http://imgur.com/gallery/%s/album_images/hit.json?all=true' % album_id, | |
137 | album_id, fatal=False) | |
138 | ||
139 | if album_images: | |
140 | data = album_images.get('data') | |
141 | if data and isinstance(data, dict): | |
142 | images = data.get('images') | |
143 | if images and isinstance(images, list): | |
144 | entries = [ | |
145 | self.url_result('http://imgur.com/%s' % image['hash']) | |
146 | for image in images if image.get('hash')] | |
147 | return self.playlist_result(entries, album_id) | |
148 | ||
149 | # Fallback to single video | |
150 | return self.url_result('http://imgur.com/%s' % album_id, ImgurIE.ie_key()) |