]>
Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/imgur.py
ecc958a1717d35ad37cc68c3b44147f3d0db6161
1 from __future__
import unicode_literals
5 from .common
import InfoExtractor
14 class ImgurIE(InfoExtractor
):
15 _VALID_URL
= r
'https?://(?:i\.)?imgur\.com/(?:(?:gallery|(?:topic|r)/[^/]+)/)?(?P<id>[a-zA-Z0-9]{6,})(?:[/?#&]+|\.[a-z0-9]+)?$'
18 'url': 'https://i.imgur.com/A61SaA1.gifv',
22 'title': 're:Imgur GIF$|MRW gifv is up and running without any bugs$',
23 'description': 'Imgur: The magic of the Internet',
26 'url': 'https://imgur.com/A61SaA1',
30 'title': 're:Imgur GIF$|MRW gifv is up and running without any bugs$',
31 'description': 'Imgur: The magic of the Internet',
34 'url': 'https://imgur.com/gallery/YcAQlkx',
38 'title': 'Classic Steve Carell gif...cracks me up everytime....damn the repost downvotes....',
41 'url': 'http://imgur.com/topic/Funny/N8rOudd',
42 'only_matching': True,
44 'url': 'http://imgur.com/r/aww/VQcQPhM',
45 'only_matching': True,
47 'url': 'https://i.imgur.com/crGpqCV.mp4',
48 'only_matching': True,
51 def _real_extract(self
, url
):
52 video_id
= self
._match
_id
(url
)
53 gifv_url
= 'https://i.imgur.com/{id}.gifv'.format(id=video_id
)
54 webpage
= self
._download
_webpage
(gifv_url
, video_id
)
56 width
= int_or_none(self
._og
_search
_property
(
57 'video:width', webpage
, default
=None))
58 height
= int_or_none(self
._og
_search
_property
(
59 'video:height', webpage
, default
=None))
61 video_elements
= self
._search
_regex
(
62 r
'(?s)<div class="video-elements">(.*?)</div>',
63 webpage
, 'video elements', default
=None)
64 if not video_elements
:
66 'No sources found for video %s. Maybe an image?' % video_id
,
70 for m
in re
.finditer(r
'<source\s+src="(?P<src>[^"]+)"\s+type="(?P<type>[^"]+)"', video_elements
):
72 'format_id': m
.group('type').partition('/')[2],
73 'url': self
._proto
_relative
_url
(m
.group('src')),
74 'ext': mimetype2ext(m
.group('type')),
79 'User-Agent': 'youtube-dl (like wget)',
83 gif_json
= self
._search
_regex
(
84 r
'(?s)var\s+videoItem\s*=\s*(\{.*?\})',
85 webpage
, 'GIF code', fatal
=False)
87 gifd
= self
._parse
_json
(
88 gif_json
, video_id
, transform_source
=js_to_json
)
98 'url': self
._proto
_relative
_url
(gifd
['gifUrl']),
99 'filesize': gifd
.get('size'),
101 'User-Agent': 'youtube-dl (like wget)',
105 self
._sort
_formats
(formats
)
110 'description': self
._og
_search
_description
(webpage
, default
=None),
111 'title': self
._og
_search
_title
(webpage
),
115 class ImgurAlbumIE(InfoExtractor
):
116 _VALID_URL
= r
'https?://(?:i\.)?imgur\.com/(?:(?:a|gallery|topic/[^/]+)/)?(?P<id>[a-zA-Z0-9]{5})(?:[/?#&]+)?$'
119 'url': 'http://imgur.com/gallery/Q95ko',
123 'playlist_count': 25,
125 'url': 'http://imgur.com/a/j6Orj',
126 'only_matching': True,
128 'url': 'http://imgur.com/topic/Aww/ll5Vk',
129 'only_matching': True,
132 def _real_extract(self
, url
):
133 album_id
= self
._match
_id
(url
)
135 album_images
= self
._download
_json
(
136 'http://imgur.com/gallery/%s/album_images/hit.json?all=true' % album_id
,
137 album_id
, fatal
=False)
140 data
= album_images
.get('data')
141 if data
and isinstance(data
, dict):
142 images
= data
.get('images')
143 if images
and isinstance(images
, list):
145 self
.url_result('http://imgur.com/%s' % image
['hash'])
146 for image
in images
if image
.get('hash')]
147 return self
.playlist_result(entries
, album_id
)
149 # Fallback to single video
150 return self
.url_result('http://imgur.com/%s' % album_id
, ImgurIE
.ie_key())