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