[youtube] Skip unsupported adaptive stream type (#18804)
[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 ..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
32     def _real_extract(self, url):
33         video_id = self._match_id(url)
34         webpage = self._download_webpage(
35             'https://i.imgur.com/{id}.gifv'.format(id=video_id), video_id)
36
37         width = int_or_none(self._og_search_property(
38             'video:width', webpage, default=None))
39         height = int_or_none(self._og_search_property(
40             'video:height', webpage, default=None))
41
42         video_elements = self._search_regex(
43             r'(?s)<div class="video-elements">(.*?)</div>',
44             webpage, 'video elements', default=None)
45         if not video_elements:
46             raise ExtractorError(
47                 'No sources found for video %s. Maybe an image?' % video_id,
48                 expected=True)
49
50         formats = []
51         for m in re.finditer(r'<source\s+src="(?P<src>[^"]+)"\s+type="(?P<type>[^"]+)"', video_elements):
52             formats.append({
53                 'format_id': m.group('type').partition('/')[2],
54                 'url': self._proto_relative_url(m.group('src')),
55                 'ext': mimetype2ext(m.group('type')),
56                 'width': width,
57                 'height': height,
58                 'http_headers': {
59                     'User-Agent': 'youtube-dl (like wget)',
60                 },
61             })
62
63         gif_json = self._search_regex(
64             r'(?s)var\s+videoItem\s*=\s*(\{.*?\})',
65             webpage, 'GIF code', fatal=False)
66         if gif_json:
67             gifd = self._parse_json(
68                 gif_json, video_id, transform_source=js_to_json)
69             formats.append({
70                 'format_id': 'gif',
71                 'preference': -10,
72                 'width': width,
73                 'height': height,
74                 'ext': 'gif',
75                 'acodec': 'none',
76                 'vcodec': 'gif',
77                 'container': 'gif',
78                 'url': self._proto_relative_url(gifd['gifUrl']),
79                 'filesize': gifd.get('size'),
80                 'http_headers': {
81                     'User-Agent': 'youtube-dl (like wget)',
82                 },
83             })
84
85         self._sort_formats(formats)
86
87         return {
88             'id': video_id,
89             'formats': formats,
90             'title': self._og_search_title(webpage),
91         }
92
93
94 class 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]+)'
97
98     _TESTS = [{
99         'url': 'http://imgur.com/gallery/Q95ko',
100         'info_dict': {
101             'id': 'Q95ko',
102             'title': 'Adding faces make every GIF better',
103         },
104         'playlist_count': 25,
105     }, {
106         'url': 'http://imgur.com/topic/Aww/ll5Vk',
107         'only_matching': True,
108     }, {
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',
120         'only_matching': True,
121     }]
122
123     def _real_extract(self, url):
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
139 class ImgurAlbumIE(ImgurGalleryIE):
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     }]