]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/imgur.py
d23489dcff4464c1294d9c6f5dca30aa480ad3a8
[youtubedl] / youtube_dl / extractor / imgur.py
1 from __future__ import unicode_literals
2
3 import re
4
5 from .common import InfoExtractor
6 from ..compat import compat_urlparse
7 from ..utils import (
8 int_or_none,
9 js_to_json,
10 mimetype2ext,
11 ExtractorError,
12 )
13
14
15 class ImgurIE(InfoExtractor):
16 _VALID_URL = r'https?://(?:i\.)?imgur\.com/(?:(?:gallery|topic/[^/]+)/)?(?P<id>[a-zA-Z0-9]{6,})(?:[/?#&]+|\.[a-z]+)?$'
17
18 _TESTS = [{
19 'url': 'https://i.imgur.com/A61SaA1.gifv',
20 'info_dict': {
21 'id': 'A61SaA1',
22 'ext': 'mp4',
23 'title': 're:Imgur GIF$|MRW gifv is up and running without any bugs$',
24 'description': 'Imgur: The most awesome images on the Internet.',
25 },
26 }, {
27 'url': 'https://imgur.com/A61SaA1',
28 'info_dict': {
29 'id': 'A61SaA1',
30 'ext': 'mp4',
31 'title': 're:Imgur GIF$|MRW gifv is up and running without any bugs$',
32 'description': 'Imgur: The most awesome images on the Internet.',
33 },
34 }, {
35 'url': 'https://imgur.com/gallery/YcAQlkx',
36 'info_dict': {
37 'id': 'YcAQlkx',
38 'ext': 'mp4',
39 'title': 'Classic Steve Carell gif...cracks me up everytime....damn the repost downvotes....',
40 'description': 'Imgur: The most awesome images on the Internet.'
41
42 }
43 }, {
44 'url': 'http://imgur.com/topic/Funny/N8rOudd',
45 'only_matching': True,
46 }]
47
48 def _real_extract(self, url):
49 video_id = self._match_id(url)
50 webpage = self._download_webpage(
51 compat_urlparse.urljoin(url, video_id), video_id)
52
53 width = int_or_none(self._og_search_property(
54 'video:width', webpage, default=None))
55 height = int_or_none(self._og_search_property(
56 'video:height', webpage, default=None))
57
58 video_elements = self._search_regex(
59 r'(?s)<div class="video-elements">(.*?)</div>',
60 webpage, 'video elements', default=None)
61 if not video_elements:
62 raise ExtractorError(
63 'No sources found for video %s. Maybe an image?' % video_id,
64 expected=True)
65
66 formats = []
67 for m in re.finditer(r'<source\s+src="(?P<src>[^"]+)"\s+type="(?P<type>[^"]+)"', video_elements):
68 formats.append({
69 'format_id': m.group('type').partition('/')[2],
70 'url': self._proto_relative_url(m.group('src')),
71 'ext': mimetype2ext(m.group('type')),
72 'acodec': 'none',
73 'width': width,
74 'height': height,
75 'http_headers': {
76 'User-Agent': 'youtube-dl (like wget)',
77 },
78 })
79
80 gif_json = self._search_regex(
81 r'(?s)var\s+videoItem\s*=\s*(\{.*?\})',
82 webpage, 'GIF code', fatal=False)
83 if gif_json:
84 gifd = self._parse_json(
85 gif_json, video_id, transform_source=js_to_json)
86 formats.append({
87 'format_id': 'gif',
88 'preference': -10,
89 'width': width,
90 'height': height,
91 'ext': 'gif',
92 'acodec': 'none',
93 'vcodec': 'gif',
94 'container': 'gif',
95 'url': self._proto_relative_url(gifd['gifUrl']),
96 'filesize': gifd.get('size'),
97 'http_headers': {
98 'User-Agent': 'youtube-dl (like wget)',
99 },
100 })
101
102 self._sort_formats(formats)
103
104 return {
105 'id': video_id,
106 'formats': formats,
107 'description': self._og_search_description(webpage),
108 'title': self._og_search_title(webpage),
109 }
110
111
112 class ImgurAlbumIE(InfoExtractor):
113 _VALID_URL = r'https?://(?:i\.)?imgur\.com/(?:(?:a|gallery|topic/[^/]+)/)?(?P<id>[a-zA-Z0-9]{5})(?:[/?#&]+)?$'
114
115 _TESTS = [{
116 'url': 'http://imgur.com/gallery/Q95ko',
117 'info_dict': {
118 'id': 'Q95ko',
119 },
120 'playlist_count': 25,
121 }, {
122 'url': 'http://imgur.com/a/j6Orj',
123 'only_matching': True,
124 }, {
125 'url': 'http://imgur.com/topic/Aww/ll5Vk',
126 'only_matching': True,
127 }]
128
129 def _real_extract(self, url):
130 album_id = self._match_id(url)
131
132 album_images = self._download_json(
133 'http://imgur.com/gallery/%s/album_images/hit.json?all=true' % album_id,
134 album_id, fatal=False)
135
136 if album_images:
137 data = album_images.get('data')
138 if data and isinstance(data, dict):
139 images = data.get('images')
140 if images and isinstance(images, list):
141 entries = [
142 self.url_result('http://imgur.com/%s' % image['hash'])
143 for image in images if image.get('hash')]
144 return self.playlist_result(entries, album_id)
145
146 # Fallback to single video
147 return self.url_result('http://imgur.com/%s' % album_id, ImgurIE.ie_key())