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