]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/imgur.py
[extractor] Deprecate `_sort_formats`
[yt-dlp.git] / yt_dlp / extractor / imgur.py
CommitLineData
3bf57053
PH
1import re
2
3from .common import InfoExtractor
4from ..utils import (
5 int_or_none,
6 js_to_json,
7 mimetype2ext,
1a13940c 8 ExtractorError,
3bf57053
PH
9)
10
b88ba053 11
3bf57053 12class 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
3bf57053
PH
87 return {
88 'id': video_id,
89 'formats': formats,
6f5c1807 90 'title': self._og_search_title(webpage, default=video_id),
3bf57053 91 }
8875b3d5
S
92
93
5f47a60c
RA
94class 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]+)'
8875b3d5 97
774ce355 98 _TESTS = [{
8875b3d5
S
99 'url': 'http://imgur.com/gallery/Q95ko',
100 'info_dict': {
101 'id': 'Q95ko',
5f47a60c 102 'title': 'Adding faces make every GIF better',
8875b3d5
S
103 },
104 'playlist_count': 25,
774ce355 105 }, {
5f47a60c 106 'url': 'http://imgur.com/topic/Aww/ll5Vk',
774ce355
S
107 'only_matching': True,
108 }, {
5f47a60c
RA
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',
774ce355
S
120 'only_matching': True,
121 }]
8875b3d5
S
122
123 def _real_extract(self, url):
5f47a60c
RA
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
6368e2e6 139class ImgurAlbumIE(ImgurGalleryIE): # XXX: Do not subclass from concrete IE
5f47a60c
RA
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 }]