]>
Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/imgur.py
   1 from __future__ 
import unicode_literals
 
   5 from .common 
import InfoExtractor
 
  14 class ImgurIE(InfoExtractor
): 
  15     _VALID_URL 
= r
'https?://(?:i\.)?imgur\.com/(?!(?:a|gallery|(?:t(?:opic)?|r)/[^/]+)/)(?P<id>[a-zA-Z0-9]+)' 
  18         'url': 'https://i.imgur.com/A61SaA1.gifv', 
  22             'title': 're:Imgur GIF$|MRW gifv is up and running without any bugs$', 
  25         'url': 'https://imgur.com/A61SaA1', 
  26         'only_matching': True, 
  28         'url': 'https://i.imgur.com/crGpqCV.mp4', 
  29         'only_matching': True, 
  32         'url': 'https://i.imgur.com/jxBXAMC.gifv', 
  33         'only_matching': True, 
  36     def _real_extract(self
, url
): 
  37         video_id 
= self
._match
_id
(url
) 
  38         webpage 
= self
._download
_webpage
( 
  39             'https://i.imgur.com/{id}.gifv'.format(id=video_id
), video_id
) 
  41         width 
= int_or_none(self
._og
_search
_property
( 
  42             'video:width', webpage
, default
=None)) 
  43         height 
= int_or_none(self
._og
_search
_property
( 
  44             'video:height', webpage
, default
=None)) 
  46         video_elements 
= self
._search
_regex
( 
  47             r
'(?s)<div class="video-elements">(.*?)</div>', 
  48             webpage
, 'video elements', default
=None) 
  49         if not video_elements
: 
  51                 'No sources found for video %s. Maybe an image?' % video_id
, 
  55         for m 
in re
.finditer(r
'<source\s+src="(?P<src>[^"]+)"\s+type="(?P<type>[^"]+)"', video_elements
): 
  57                 'format_id': m
.group('type').partition('/')[2], 
  58                 'url': self
._proto
_relative
_url
(m
.group('src')), 
  59                 'ext': mimetype2ext(m
.group('type')), 
  63                     'User-Agent': 'youtube-dl (like wget)', 
  67         gif_json 
= self
._search
_regex
( 
  68             r
'(?s)var\s+videoItem\s*=\s*(\{.*?\})', 
  69             webpage
, 'GIF code', fatal
=False) 
  71             gifd 
= self
._parse
_json
( 
  72                 gif_json
, video_id
, transform_source
=js_to_json
) 
  82                 'url': self
._proto
_relative
_url
(gifd
['gifUrl']), 
  83                 'filesize': gifd
.get('size'), 
  85                     'User-Agent': 'youtube-dl (like wget)', 
  89         self
._sort
_formats
(formats
) 
  94             'title': self
._og
_search
_title
(webpage
, default
=video_id
), 
  98 class ImgurGalleryIE(InfoExtractor
): 
  99     IE_NAME 
= 'imgur:gallery' 
 100     _VALID_URL 
= r
'https?://(?:i\.)?imgur\.com/(?:gallery|(?:t(?:opic)?|r)/[^/]+)/(?P<id>[a-zA-Z0-9]+)' 
 103         'url': 'http://imgur.com/gallery/Q95ko', 
 106             'title': 'Adding faces make every GIF better', 
 108         'playlist_count': 25, 
 110         'url': 'http://imgur.com/topic/Aww/ll5Vk', 
 111         'only_matching': True, 
 113         'url': 'https://imgur.com/gallery/YcAQlkx', 
 117             'title': 'Classic Steve Carell gif...cracks me up everytime....damn the repost downvotes....', 
 120         'url': 'http://imgur.com/topic/Funny/N8rOudd', 
 121         'only_matching': True, 
 123         'url': 'http://imgur.com/r/aww/VQcQPhM', 
 124         'only_matching': True, 
 127     def _real_extract(self
, url
): 
 128         gallery_id 
= self
._match
_id
(url
) 
 130         data 
= self
._download
_json
( 
 131             'https://imgur.com/gallery/%s.json' % gallery_id
, 
 132             gallery_id
)['data']['image'] 
 134         if data
.get('is_album'): 
 136                 self
.url_result('http://imgur.com/%s' % image
['hash'], ImgurIE
.ie_key(), image
['hash']) 
 137                 for image 
in data
['album_images']['images'] if image
.get('hash')] 
 138             return self
.playlist_result(entries
, gallery_id
, data
.get('title'), data
.get('description')) 
 140         return self
.url_result('http://imgur.com/%s' % gallery_id
, ImgurIE
.ie_key(), gallery_id
) 
 143 class ImgurAlbumIE(ImgurGalleryIE
): 
 144     IE_NAME 
= 'imgur:album' 
 145     _VALID_URL 
= r
'https?://(?:i\.)?imgur\.com/a/(?P<id>[a-zA-Z0-9]+)' 
 148         'url': 'http://imgur.com/a/j6Orj', 
 151             'title': 'A Literary Analysis of "Star Wars: The Force Awakens"', 
 153         'playlist_count': 12,