imgur.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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/(?:(?:gallery|(?:topic|r)/[^/]+)/)?(?P<id>[a-zA-Z0-9]{6,})(?:[/?#&]+|\.[a-z]+)?$'
  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. 'description': 'Imgur: The magic of the Internet',
  19. },
  20. }, {
  21. 'url': 'https://imgur.com/A61SaA1',
  22. 'info_dict': {
  23. 'id': 'A61SaA1',
  24. 'ext': 'mp4',
  25. 'title': 're:Imgur GIF$|MRW gifv is up and running without any bugs$',
  26. 'description': 'Imgur: The magic of the Internet',
  27. },
  28. }, {
  29. 'url': 'https://imgur.com/gallery/YcAQlkx',
  30. 'info_dict': {
  31. 'id': 'YcAQlkx',
  32. 'ext': 'mp4',
  33. 'title': 'Classic Steve Carell gif...cracks me up everytime....damn the repost downvotes....',
  34. }
  35. }, {
  36. 'url': 'http://imgur.com/topic/Funny/N8rOudd',
  37. 'only_matching': True,
  38. }, {
  39. 'url': 'http://imgur.com/r/aww/VQcQPhM',
  40. 'only_matching': True,
  41. }]
  42. def _real_extract(self, url):
  43. video_id = self._match_id(url)
  44. gifv_url = 'https://i.imgur.com/{id}.gifv'.format(id=video_id)
  45. webpage = self._download_webpage(gifv_url, video_id)
  46. width = int_or_none(self._og_search_property(
  47. 'video:width', webpage, default=None))
  48. height = int_or_none(self._og_search_property(
  49. 'video:height', webpage, default=None))
  50. video_elements = self._search_regex(
  51. r'(?s)<div class="video-elements">(.*?)</div>',
  52. webpage, 'video elements', default=None)
  53. if not video_elements:
  54. raise ExtractorError(
  55. 'No sources found for video %s. Maybe an image?' % video_id,
  56. expected=True)
  57. formats = []
  58. for m in re.finditer(r'<source\s+src="(?P<src>[^"]+)"\s+type="(?P<type>[^"]+)"', video_elements):
  59. formats.append({
  60. 'format_id': m.group('type').partition('/')[2],
  61. 'url': self._proto_relative_url(m.group('src')),
  62. 'ext': mimetype2ext(m.group('type')),
  63. 'acodec': 'none',
  64. 'width': width,
  65. 'height': height,
  66. 'http_headers': {
  67. 'User-Agent': 'youtube-dl (like wget)',
  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. self._sort_formats(formats)
  92. return {
  93. 'id': video_id,
  94. 'formats': formats,
  95. 'description': self._og_search_description(webpage, default=None),
  96. 'title': self._og_search_title(webpage),
  97. }
  98. class ImgurAlbumIE(InfoExtractor):
  99. _VALID_URL = r'https?://(?:i\.)?imgur\.com/(?:(?:a|gallery|topic/[^/]+)/)?(?P<id>[a-zA-Z0-9]{5})(?:[/?#&]+)?$'
  100. _TESTS = [{
  101. 'url': 'http://imgur.com/gallery/Q95ko',
  102. 'info_dict': {
  103. 'id': 'Q95ko',
  104. },
  105. 'playlist_count': 25,
  106. }, {
  107. 'url': 'http://imgur.com/a/j6Orj',
  108. 'only_matching': True,
  109. }, {
  110. 'url': 'http://imgur.com/topic/Aww/ll5Vk',
  111. 'only_matching': True,
  112. }]
  113. def _real_extract(self, url):
  114. album_id = self._match_id(url)
  115. album_images = self._download_json(
  116. 'http://imgur.com/gallery/%s/album_images/hit.json?all=true' % album_id,
  117. album_id, fatal=False)
  118. if album_images:
  119. data = album_images.get('data')
  120. if data and isinstance(data, dict):
  121. images = data.get('images')
  122. if images and isinstance(images, list):
  123. entries = [
  124. self.url_result('http://imgur.com/%s' % image['hash'])
  125. for image in images if image.get('hash')]
  126. return self.playlist_result(entries, album_id)
  127. # Fallback to single video
  128. return self.url_result('http://imgur.com/%s' % album_id, ImgurIE.ie_key())