Improve extraction (Closes #7918)
[youtube-dl] / 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/)?(?P<id>[a-zA-Z0-9]{6,})'
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
45     def _real_extract(self, url):
46         video_id = self._match_id(url)
47         webpage = self._download_webpage(
48             compat_urlparse.urljoin(url, video_id), video_id)
49
50         width = int_or_none(self._search_regex(
51             r'<param name="width" value="([0-9]+)"',
52             webpage, 'width', fatal=False))
53         height = int_or_none(self._search_regex(
54             r'<param name="height" value="([0-9]+)"',
55             webpage, 'height', fatal=False))
56
57         video_elements = self._search_regex(
58             r'(?s)<div class="video-elements">(.*?)</div>',
59             webpage, 'video elements', default=None)
60         if not video_elements:
61             raise ExtractorError(
62                 'No sources found for video %s. Maybe an image?' % video_id,
63                 expected=True)
64
65         formats = []
66         for m in re.finditer(r'<source\s+src="(?P<src>[^"]+)"\s+type="(?P<type>[^"]+)"', video_elements):
67             formats.append({
68                 'format_id': m.group('type').partition('/')[2],
69                 'url': self._proto_relative_url(m.group('src')),
70                 'ext': mimetype2ext(m.group('type')),
71                 'acodec': 'none',
72                 'width': width,
73                 'height': height,
74                 'http_headers': {
75                     'User-Agent': 'youtube-dl (like wget)',
76                 },
77             })
78
79         gif_json = self._search_regex(
80             r'(?s)var\s+videoItem\s*=\s*(\{.*?\})',
81             webpage, 'GIF code', fatal=False)
82         if gif_json:
83             gifd = self._parse_json(
84                 gif_json, video_id, transform_source=js_to_json)
85             formats.append({
86                 'format_id': 'gif',
87                 'preference': -10,
88                 'width': width,
89                 'height': height,
90                 'ext': 'gif',
91                 'acodec': 'none',
92                 'vcodec': 'gif',
93                 'container': 'gif',
94                 'url': self._proto_relative_url(gifd['gifUrl']),
95                 'filesize': gifd.get('size'),
96                 'http_headers': {
97                     'User-Agent': 'youtube-dl (like wget)',
98                 },
99             })
100
101         self._sort_formats(formats)
102
103         return {
104             'id': video_id,
105             'formats': formats,
106             'description': self._og_search_description(webpage),
107             'title': self._og_search_title(webpage),
108         }
109
110
111 class ImgurAlbumIE(InfoExtractor):
112     _VALID_URL = r'https?://(?:i\.)?imgur\.com/(gallery/)?(?P<id>[a-zA-Z0-9]{5})(?![a-zA-Z0-9])'
113
114     _TEST = {
115         'url': 'http://imgur.com/gallery/Q95ko',
116         'info_dict': {
117             'id': 'Q95ko',
118         },
119         'playlist_count': 25,
120     }
121
122     def _real_extract(self, url):
123         album_id = self._match_id(url)
124
125         album_img_data = self._download_json(
126             'http://imgur.com/gallery/%s/album_images/hit.json?all=true' % album_id, album_id)['data']
127
128         if len(album_img_data) == 0:
129             return self.url_result('http://imgur.com/%s' % album_id)
130         else:
131             album_images = album_img_data['images']
132             entries = [
133                 self.url_result('http://imgur.com/%s' % image['hash'])
134                 for image in album_images if image.get('hash')]
135
136         return self.playlist_result(entries, album_id)