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