]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/imgur.py
bff6ed57f5b0c878c3f2900380b1ee2a30fde2fb
[yt-dlp.git] / yt_dlp / extractor / imgur.py
1 import re
2
3 from .common import InfoExtractor
4 from ..utils import (
5 int_or_none,
6 js_to_json,
7 mimetype2ext,
8 ExtractorError,
9 )
10
11
12 class ImgurIE(InfoExtractor):
13 _VALID_URL = r'https?://(?:i\.)?imgur\.com/(?!(?:a|gallery|(?:t(?:opic)?|r)/[^/]+)/)(?P<id>[a-zA-Z0-9]+)'
14
15 _TESTS = [{
16 'url': 'https://i.imgur.com/A61SaA1.gifv',
17 'info_dict': {
18 'id': 'A61SaA1',
19 'ext': 'mp4',
20 'title': 're:Imgur GIF$|MRW gifv is up and running without any bugs$',
21 },
22 }, {
23 'url': 'https://imgur.com/A61SaA1',
24 'only_matching': True,
25 }, {
26 'url': 'https://i.imgur.com/crGpqCV.mp4',
27 'only_matching': True,
28 }, {
29 # no title
30 'url': 'https://i.imgur.com/jxBXAMC.gifv',
31 'only_matching': True,
32 }]
33
34 def _real_extract(self, url):
35 video_id = self._match_id(url)
36 webpage = self._download_webpage(
37 'https://i.imgur.com/{id}.gifv'.format(id=video_id), video_id)
38
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))
43
44 video_elements = self._search_regex(
45 r'(?s)<div class="video-elements">(.*?)</div>',
46 webpage, 'video elements', default=None)
47 if not video_elements:
48 raise ExtractorError(
49 'No sources found for video %s. Maybe an image?' % video_id,
50 expected=True)
51
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')),
58 'width': width,
59 'height': height,
60 'http_headers': {
61 'User-Agent': 'yt-dlp (like wget)',
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',
73 'preference': -10, # gifs are worse than videos
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': {
83 'User-Agent': 'yt-dlp (like wget)',
84 },
85 })
86
87 return {
88 'id': video_id,
89 'formats': formats,
90 'title': self._og_search_title(webpage, default=video_id),
91 }
92
93
94 class ImgurGalleryIE(InfoExtractor):
95 IE_NAME = 'imgur:gallery'
96 _VALID_URL = r'https?://(?:i\.)?imgur\.com/(?:gallery|(?:t(?:opic)?|r)/[^/]+)/(?P<id>[a-zA-Z0-9]+)'
97
98 _TESTS = [{
99 'url': 'http://imgur.com/gallery/Q95ko',
100 'info_dict': {
101 'id': 'Q95ko',
102 'title': 'Adding faces make every GIF better',
103 },
104 'playlist_count': 25,
105 }, {
106 'url': 'http://imgur.com/topic/Aww/ll5Vk',
107 'only_matching': True,
108 }, {
109 'url': 'https://imgur.com/gallery/YcAQlkx',
110 'info_dict': {
111 'id': 'YcAQlkx',
112 'ext': 'mp4',
113 'title': 'Classic Steve Carell gif...cracks me up everytime....damn the repost downvotes....',
114 }
115 }, {
116 'url': 'http://imgur.com/topic/Funny/N8rOudd',
117 'only_matching': True,
118 }, {
119 'url': 'http://imgur.com/r/aww/VQcQPhM',
120 'only_matching': True,
121 }]
122
123 def _real_extract(self, url):
124 gallery_id = self._match_id(url)
125
126 data = self._download_json(
127 'https://imgur.com/gallery/%s.json' % gallery_id,
128 gallery_id)['data']['image']
129
130 if data.get('is_album'):
131 entries = [
132 self.url_result('http://imgur.com/%s' % image['hash'], ImgurIE.ie_key(), image['hash'])
133 for image in data['album_images']['images'] if image.get('hash')]
134 return self.playlist_result(entries, gallery_id, data.get('title'), data.get('description'))
135
136 return self.url_result('http://imgur.com/%s' % gallery_id, ImgurIE.ie_key(), gallery_id)
137
138
139 class ImgurAlbumIE(ImgurGalleryIE): # XXX: Do not subclass from concrete IE
140 IE_NAME = 'imgur:album'
141 _VALID_URL = r'https?://(?:i\.)?imgur\.com/a/(?P<id>[a-zA-Z0-9]+)'
142
143 _TESTS = [{
144 'url': 'http://imgur.com/a/j6Orj',
145 'info_dict': {
146 'id': 'j6Orj',
147 'title': 'A Literary Analysis of "Star Wars: The Force Awakens"',
148 },
149 'playlist_count': 12,
150 }]