]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/imgur.py
New upstream version 2020.05.08
[youtubedl] / youtube_dl / extractor / imgur.py
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,
10 ExtractorError,
11 )
12
13
14 class ImgurIE(InfoExtractor):
15 _VALID_URL = r'https?://(?:i\.)?imgur\.com/(?!(?:a|gallery|(?:t(?:opic)?|r)/[^/]+)/)(?P<id>[a-zA-Z0-9]+)'
16
17 _TESTS = [{
18 'url': 'https://i.imgur.com/A61SaA1.gifv',
19 'info_dict': {
20 'id': 'A61SaA1',
21 'ext': 'mp4',
22 'title': 're:Imgur GIF$|MRW gifv is up and running without any bugs$',
23 },
24 }, {
25 'url': 'https://imgur.com/A61SaA1',
26 'only_matching': True,
27 }, {
28 'url': 'https://i.imgur.com/crGpqCV.mp4',
29 'only_matching': True,
30 }, {
31 # no title
32 'url': 'https://i.imgur.com/jxBXAMC.gifv',
33 'only_matching': True,
34 }]
35
36 def _real_extract(self, url):
37 video_id = self._match_id(url)
38 webpage = self._download_webpage(
39 'https://i.imgur.com/{id}.gifv'.format(id=video_id), video_id)
40
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))
45
46 video_elements = self._search_regex(
47 r'(?s)<div class="video-elements">(.*?)</div>',
48 webpage, 'video elements', default=None)
49 if not video_elements:
50 raise ExtractorError(
51 'No sources found for video %s. Maybe an image?' % video_id,
52 expected=True)
53
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')),
60 'width': width,
61 'height': height,
62 'http_headers': {
63 'User-Agent': 'youtube-dl (like wget)',
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',
75 'preference': -10,
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': {
85 'User-Agent': 'youtube-dl (like wget)',
86 },
87 })
88
89 self._sort_formats(formats)
90
91 return {
92 'id': video_id,
93 'formats': formats,
94 'title': self._og_search_title(webpage, default=video_id),
95 }
96
97
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]+)'
101
102 _TESTS = [{
103 'url': 'http://imgur.com/gallery/Q95ko',
104 'info_dict': {
105 'id': 'Q95ko',
106 'title': 'Adding faces make every GIF better',
107 },
108 'playlist_count': 25,
109 }, {
110 'url': 'http://imgur.com/topic/Aww/ll5Vk',
111 'only_matching': True,
112 }, {
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',
124 'only_matching': True,
125 }]
126
127 def _real_extract(self, url):
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 }]