]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/imgur.py
d692ea79ab493174038c9649445e6a592a86687c
[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/(?P<id>[a-zA-Z0-9]+)'
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': 're:The origin of the Internet\'s most viral images$|The Internet\'s visual storytelling community\. Explore, share, and discuss the best visual stories the Internet has to offer\.$',
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': 're:The origin of the Internet\'s most viral images$|The Internet\'s visual storytelling community\. Explore, share, and discuss the best visual stories the Internet has to offer\.$',
33 },
34 }]
35
36 def _real_extract(self, url):
37 video_id = self._match_id(url)
38 webpage = self._download_webpage(
39 compat_urlparse.urljoin(url, video_id), video_id)
40
41 width = int_or_none(self._search_regex(
42 r'<param name="width" value="([0-9]+)"',
43 webpage, 'width', fatal=False))
44 height = int_or_none(self._search_regex(
45 r'<param name="height" value="([0-9]+)"',
46 webpage, 'height', fatal=False))
47
48 video_elements = self._search_regex(
49 r'(?s)<div class="video-elements">(.*?)</div>',
50 webpage, 'video elements', default=None)
51 if not video_elements:
52 raise ExtractorError(
53 'No sources found for video %s. Maybe an image?' % video_id,
54 expected=True)
55
56 formats = []
57 for m in re.finditer(r'<source\s+src="(?P<src>[^"]+)"\s+type="(?P<type>[^"]+)"', video_elements):
58 formats.append({
59 'format_id': m.group('type').partition('/')[2],
60 'url': self._proto_relative_url(m.group('src')),
61 'ext': mimetype2ext(m.group('type')),
62 'acodec': 'none',
63 'width': width,
64 'height': height,
65 'http_headers': {
66 'User-Agent': 'youtube-dl (like wget)',
67 },
68 })
69
70 gif_json = self._search_regex(
71 r'(?s)var\s+videoItem\s*=\s*(\{.*?\})',
72 webpage, 'GIF code', fatal=False)
73 if gif_json:
74 gifd = self._parse_json(
75 gif_json, video_id, transform_source=js_to_json)
76 formats.append({
77 'format_id': 'gif',
78 'preference': -10,
79 'width': width,
80 'height': height,
81 'ext': 'gif',
82 'acodec': 'none',
83 'vcodec': 'gif',
84 'container': 'gif',
85 'url': self._proto_relative_url(gifd['gifUrl']),
86 'filesize': gifd.get('size'),
87 'http_headers': {
88 'User-Agent': 'youtube-dl (like wget)',
89 },
90 })
91
92 self._sort_formats(formats)
93
94 return {
95 'id': video_id,
96 'formats': formats,
97 'description': self._og_search_description(webpage),
98 'title': self._og_search_title(webpage),
99 }