]>
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): |
5f47a60c | 15 | _VALID_URL = r'https?://(?:i\.)?imgur\.com/(?!(?:a|gallery|(?:t(?:opic)?|r)/[^/]+)/)(?P<id>[a-zA-Z0-9]+)' |
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$', |
3bf57053 | 23 | }, |
1a13940c JB |
24 | }, { |
25 | 'url': 'https://imgur.com/A61SaA1', | |
e1a0b3b8 | 26 | 'only_matching': True, |
905eef2b JW |
27 | }, { |
28 | 'url': 'https://i.imgur.com/crGpqCV.mp4', | |
29 | 'only_matching': True, | |
6f5c1807 | 30 | }, { |
31 | # no title | |
32 | 'url': 'https://i.imgur.com/jxBXAMC.gifv', | |
33 | 'only_matching': True, | |
3bf57053 PH |
34 | }] |
35 | ||
36 | def _real_extract(self, url): | |
37 | video_id = self._match_id(url) | |
5f47a60c RA |
38 | webpage = self._download_webpage( |
39 | 'https://i.imgur.com/{id}.gifv'.format(id=video_id), video_id) | |
3bf57053 | 40 | |
c2a453b4 S |
41 | width = int_or_none(self._og_search_property( |
42 | 'video:width', webpage, default=None)) | |
43 | height = int_or_none(self._og_search_property( | |
44 | 'video:height', webpage, default=None)) | |
3bf57053 | 45 | |
b88ba053 | 46 | video_elements = self._search_regex( |
3bf57053 | 47 | r'(?s)<div class="video-elements">(.*?)</div>', |
b88ba053 | 48 | webpage, 'video elements', default=None) |
9e2d7dca JB |
49 | if not video_elements: |
50 | raise ExtractorError( | |
b88ba053 PH |
51 | 'No sources found for video %s. Maybe an image?' % video_id, |
52 | expected=True) | |
9e2d7dca | 53 | |
3bf57053 PH |
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')), | |
3bf57053 PH |
60 | 'width': width, |
61 | 'height': height, | |
62 | 'http_headers': { | |
7a5c1cfe | 63 | 'User-Agent': 'yt-dlp (like wget)', |
3bf57053 PH |
64 | }, |
65 | }) | |
66 | ||
67 | gif_json = self._search_regex( | |
68 | r'(?s)var\s+videoItem\s*=\s*(\{.*?\})', | |
69 | webpage, 'GIF code', fatal=False) | |
70 | if gif_json: | |
71 | gifd = self._parse_json( | |
72 | gif_json, video_id, transform_source=js_to_json) | |
73 | formats.append({ | |
74 | 'format_id': 'gif', | |
f983b875 | 75 | 'preference': -10, # gifs are worse than videos |
3bf57053 PH |
76 | 'width': width, |
77 | 'height': height, | |
78 | 'ext': 'gif', | |
79 | 'acodec': 'none', | |
80 | 'vcodec': 'gif', | |
81 | 'container': 'gif', | |
82 | 'url': self._proto_relative_url(gifd['gifUrl']), | |
83 | 'filesize': gifd.get('size'), | |
84 | 'http_headers': { | |
7a5c1cfe | 85 | 'User-Agent': 'yt-dlp (like wget)', |
3bf57053 PH |
86 | }, |
87 | }) | |
88 | ||
89 | self._sort_formats(formats) | |
90 | ||
91 | return { | |
92 | 'id': video_id, | |
93 | 'formats': formats, | |
6f5c1807 | 94 | 'title': self._og_search_title(webpage, default=video_id), |
3bf57053 | 95 | } |
8875b3d5 S |
96 | |
97 | ||
5f47a60c RA |
98 | class ImgurGalleryIE(InfoExtractor): |
99 | IE_NAME = 'imgur:gallery' | |
100 | _VALID_URL = r'https?://(?:i\.)?imgur\.com/(?:gallery|(?:t(?:opic)?|r)/[^/]+)/(?P<id>[a-zA-Z0-9]+)' | |
8875b3d5 | 101 | |
774ce355 | 102 | _TESTS = [{ |
8875b3d5 S |
103 | 'url': 'http://imgur.com/gallery/Q95ko', |
104 | 'info_dict': { | |
105 | 'id': 'Q95ko', | |
5f47a60c | 106 | 'title': 'Adding faces make every GIF better', |
8875b3d5 S |
107 | }, |
108 | 'playlist_count': 25, | |
774ce355 | 109 | }, { |
5f47a60c | 110 | 'url': 'http://imgur.com/topic/Aww/ll5Vk', |
774ce355 S |
111 | 'only_matching': True, |
112 | }, { | |
5f47a60c RA |
113 | 'url': 'https://imgur.com/gallery/YcAQlkx', |
114 | 'info_dict': { | |
115 | 'id': 'YcAQlkx', | |
116 | 'ext': 'mp4', | |
117 | 'title': 'Classic Steve Carell gif...cracks me up everytime....damn the repost downvotes....', | |
118 | } | |
119 | }, { | |
120 | 'url': 'http://imgur.com/topic/Funny/N8rOudd', | |
121 | 'only_matching': True, | |
122 | }, { | |
123 | 'url': 'http://imgur.com/r/aww/VQcQPhM', | |
774ce355 S |
124 | 'only_matching': True, |
125 | }] | |
8875b3d5 S |
126 | |
127 | def _real_extract(self, url): | |
5f47a60c RA |
128 | gallery_id = self._match_id(url) |
129 | ||
130 | data = self._download_json( | |
131 | 'https://imgur.com/gallery/%s.json' % gallery_id, | |
132 | gallery_id)['data']['image'] | |
133 | ||
134 | if data.get('is_album'): | |
135 | entries = [ | |
136 | self.url_result('http://imgur.com/%s' % image['hash'], ImgurIE.ie_key(), image['hash']) | |
137 | for image in data['album_images']['images'] if image.get('hash')] | |
138 | return self.playlist_result(entries, gallery_id, data.get('title'), data.get('description')) | |
139 | ||
140 | return self.url_result('http://imgur.com/%s' % gallery_id, ImgurIE.ie_key(), gallery_id) | |
141 | ||
142 | ||
143 | class ImgurAlbumIE(ImgurGalleryIE): | |
144 | IE_NAME = 'imgur:album' | |
145 | _VALID_URL = r'https?://(?:i\.)?imgur\.com/a/(?P<id>[a-zA-Z0-9]+)' | |
146 | ||
147 | _TESTS = [{ | |
148 | 'url': 'http://imgur.com/a/j6Orj', | |
149 | 'info_dict': { | |
150 | 'id': 'j6Orj', | |
151 | 'title': 'A Literary Analysis of "Star Wars: The Force Awakens"', | |
152 | }, | |
153 | 'playlist_count': 12, | |
154 | }] |