imgur.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. from __future__ import unicode_literals
  2. import re
  3. from .common import InfoExtractor
  4. from ..utils import (
  5. int_or_none,
  6. js_to_json,
  7. mimetype2ext,
  8. ExtractorError,
  9. )
  10. class ImgurIE(InfoExtractor):
  11. _VALID_URL = r'https?://(?:i\.)?imgur\.com/(?!(?:a|gallery|(?:t(?:opic)?|r)/[^/]+)/)(?P<id>[a-zA-Z0-9]+)'
  12. _TESTS = [{
  13. 'url': 'https://i.imgur.com/A61SaA1.gifv',
  14. 'info_dict': {
  15. 'id': 'A61SaA1',
  16. 'ext': 'mp4',
  17. 'title': 're:Imgur GIF$|MRW gifv is up and running without any bugs$',
  18. },
  19. }, {
  20. 'url': 'https://imgur.com/A61SaA1',
  21. 'only_matching': True,
  22. }, {
  23. 'url': 'https://i.imgur.com/crGpqCV.mp4',
  24. 'only_matching': True,
  25. }]
  26. def _real_extract(self, url):
  27. video_id = self._match_id(url)
  28. webpage = self._download_webpage(
  29. 'https://i.imgur.com/{id}.gifv'.format(id=video_id), video_id)
  30. width = int_or_none(self._og_search_property(
  31. 'video:width', webpage, default=None))
  32. height = int_or_none(self._og_search_property(
  33. 'video:height', webpage, default=None))
  34. video_elements = self._search_regex(
  35. r'(?s)<div class="video-elements">(.*?)</div>',
  36. webpage, 'video elements', default=None)
  37. if not video_elements:
  38. raise ExtractorError(
  39. 'No sources found for video %s. Maybe an image?' % video_id,
  40. expected=True)
  41. formats = []
  42. for m in re.finditer(r'<source\s+src="(?P<src>[^"]+)"\s+type="(?P<type>[^"]+)"', video_elements):
  43. formats.append({
  44. 'format_id': m.group('type').partition('/')[2],
  45. 'url': self._proto_relative_url(m.group('src')),
  46. 'ext': mimetype2ext(m.group('type')),
  47. 'width': width,
  48. 'height': height,
  49. 'http_headers': {
  50. 'User-Agent': 'youtube-dl (like wget)',
  51. },
  52. })
  53. gif_json = self._search_regex(
  54. r'(?s)var\s+videoItem\s*=\s*(\{.*?\})',
  55. webpage, 'GIF code', fatal=False)
  56. if gif_json:
  57. gifd = self._parse_json(
  58. gif_json, video_id, transform_source=js_to_json)
  59. formats.append({
  60. 'format_id': 'gif',
  61. 'preference': -10,
  62. 'width': width,
  63. 'height': height,
  64. 'ext': 'gif',
  65. 'acodec': 'none',
  66. 'vcodec': 'gif',
  67. 'container': 'gif',
  68. 'url': self._proto_relative_url(gifd['gifUrl']),
  69. 'filesize': gifd.get('size'),
  70. 'http_headers': {
  71. 'User-Agent': 'youtube-dl (like wget)',
  72. },
  73. })
  74. self._sort_formats(formats)
  75. return {
  76. 'id': video_id,
  77. 'formats': formats,
  78. 'title': self._og_search_title(webpage),
  79. }
  80. class ImgurGalleryIE(InfoExtractor):
  81. IE_NAME = 'imgur:gallery'
  82. _VALID_URL = r'https?://(?:i\.)?imgur\.com/(?:gallery|(?:t(?:opic)?|r)/[^/]+)/(?P<id>[a-zA-Z0-9]+)'
  83. _TESTS = [{
  84. 'url': 'http://imgur.com/gallery/Q95ko',
  85. 'info_dict': {
  86. 'id': 'Q95ko',
  87. 'title': 'Adding faces make every GIF better',
  88. },
  89. 'playlist_count': 25,
  90. }, {
  91. 'url': 'http://imgur.com/topic/Aww/ll5Vk',
  92. 'only_matching': True,
  93. }, {
  94. 'url': 'https://imgur.com/gallery/YcAQlkx',
  95. 'info_dict': {
  96. 'id': 'YcAQlkx',
  97. 'ext': 'mp4',
  98. 'title': 'Classic Steve Carell gif...cracks me up everytime....damn the repost downvotes....',
  99. }
  100. }, {
  101. 'url': 'http://imgur.com/topic/Funny/N8rOudd',
  102. 'only_matching': True,
  103. }, {
  104. 'url': 'http://imgur.com/r/aww/VQcQPhM',
  105. 'only_matching': True,
  106. }]
  107. def _real_extract(self, url):
  108. gallery_id = self._match_id(url)
  109. data = self._download_json(
  110. 'https://imgur.com/gallery/%s.json' % gallery_id,
  111. gallery_id)['data']['image']
  112. if data.get('is_album'):
  113. entries = [
  114. self.url_result('http://imgur.com/%s' % image['hash'], ImgurIE.ie_key(), image['hash'])
  115. for image in data['album_images']['images'] if image.get('hash')]
  116. return self.playlist_result(entries, gallery_id, data.get('title'), data.get('description'))
  117. return self.url_result('http://imgur.com/%s' % gallery_id, ImgurIE.ie_key(), gallery_id)
  118. class ImgurAlbumIE(ImgurGalleryIE):
  119. IE_NAME = 'imgur:album'
  120. _VALID_URL = r'https?://(?:i\.)?imgur\.com/a/(?P<id>[a-zA-Z0-9]+)'
  121. _TESTS = [{
  122. 'url': 'http://imgur.com/a/j6Orj',
  123. 'info_dict': {
  124. 'id': 'j6Orj',
  125. 'title': 'A Literary Analysis of "Star Wars: The Force Awakens"',
  126. },
  127. 'playlist_count': 12,
  128. }]