]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/imgur.py
New upstream version 2018.06.18
[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/(?:(?:gallery|(?:topic|r)/[^/]+)/)?(?P<id>[a-zA-Z0-9]{6,})(?:[/?#&]+|\.[a-z]+)?$'
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 'description': 'Imgur: The magic of the Internet',
24 },
25 }, {
26 'url': 'https://imgur.com/A61SaA1',
27 'info_dict': {
28 'id': 'A61SaA1',
29 'ext': 'mp4',
30 'title': 're:Imgur GIF$|MRW gifv is up and running without any bugs$',
31 'description': 'Imgur: The magic of the Internet',
32 },
33 }, {
34 'url': 'https://imgur.com/gallery/YcAQlkx',
35 'info_dict': {
36 'id': 'YcAQlkx',
37 'ext': 'mp4',
38 'title': 'Classic Steve Carell gif...cracks me up everytime....damn the repost downvotes....',
39 }
40 }, {
41 'url': 'http://imgur.com/topic/Funny/N8rOudd',
42 'only_matching': True,
43 }, {
44 'url': 'http://imgur.com/r/aww/VQcQPhM',
45 'only_matching': True,
46 }]
47
48 def _real_extract(self, url):
49 video_id = self._match_id(url)
50 gifv_url = 'https://i.imgur.com/{id}.gifv'.format(id=video_id)
51 webpage = self._download_webpage(gifv_url, 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, default=None),
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())