imgur.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. from __future__ import unicode_literals
  2. import re
  3. from .common import InfoExtractor
  4. from ..compat import compat_urlparse
  5. from ..utils import (
  6. int_or_none,
  7. js_to_json,
  8. mimetype2ext,
  9. ExtractorError,
  10. )
  11. class ImgurIE(InfoExtractor):
  12. _VALID_URL = r'https?://(?:i\.)?imgur\.com/(gallery/)?(?P<id>[a-zA-Z0-9]{6,})'
  13. _TESTS = [{
  14. 'url': 'https://i.imgur.com/A61SaA1.gifv',
  15. 'info_dict': {
  16. 'id': 'A61SaA1',
  17. 'ext': 'mp4',
  18. 'title': 're:Imgur GIF$|MRW gifv is up and running without any bugs$',
  19. 'description': 'Imgur: The most awesome images on the Internet.',
  20. },
  21. }, {
  22. 'url': 'https://imgur.com/A61SaA1',
  23. 'info_dict': {
  24. 'id': 'A61SaA1',
  25. 'ext': 'mp4',
  26. 'title': 're:Imgur GIF$|MRW gifv is up and running without any bugs$',
  27. 'description': 'Imgur: The most awesome images on the Internet.',
  28. },
  29. }, {
  30. 'url': 'https://imgur.com/gallery/YcAQlkx',
  31. 'info_dict': {
  32. 'id': 'YcAQlkx',
  33. 'ext': 'mp4',
  34. 'title': 'Classic Steve Carell gif...cracks me up everytime....damn the repost downvotes....',
  35. 'description': 'Imgur: The most awesome images on the Internet.'
  36. }
  37. }]
  38. def _real_extract(self, url):
  39. video_id = self._match_id(url)
  40. webpage = self._download_webpage(
  41. compat_urlparse.urljoin(url, video_id), video_id)
  42. width = int_or_none(self._search_regex(
  43. r'<param name="width" value="([0-9]+)"',
  44. webpage, 'width', fatal=False))
  45. height = int_or_none(self._search_regex(
  46. r'<param name="height" value="([0-9]+)"',
  47. webpage, 'height', fatal=False))
  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. formats = []
  56. for m in re.finditer(r'<source\s+src="(?P<src>[^"]+)"\s+type="(?P<type>[^"]+)"', video_elements):
  57. formats.append({
  58. 'format_id': m.group('type').partition('/')[2],
  59. 'url': self._proto_relative_url(m.group('src')),
  60. 'ext': mimetype2ext(m.group('type')),
  61. 'acodec': 'none',
  62. 'width': width,
  63. 'height': height,
  64. 'http_headers': {
  65. 'User-Agent': 'youtube-dl (like wget)',
  66. },
  67. })
  68. gif_json = self._search_regex(
  69. r'(?s)var\s+videoItem\s*=\s*(\{.*?\})',
  70. webpage, 'GIF code', fatal=False)
  71. if gif_json:
  72. gifd = self._parse_json(
  73. gif_json, video_id, transform_source=js_to_json)
  74. formats.append({
  75. 'format_id': 'gif',
  76. 'preference': -10,
  77. 'width': width,
  78. 'height': height,
  79. 'ext': 'gif',
  80. 'acodec': 'none',
  81. 'vcodec': 'gif',
  82. 'container': 'gif',
  83. 'url': self._proto_relative_url(gifd['gifUrl']),
  84. 'filesize': gifd.get('size'),
  85. 'http_headers': {
  86. 'User-Agent': 'youtube-dl (like wget)',
  87. },
  88. })
  89. self._sort_formats(formats)
  90. return {
  91. 'id': video_id,
  92. 'formats': formats,
  93. 'description': self._og_search_description(webpage),
  94. 'title': self._og_search_title(webpage),
  95. }
  96. class ImgurAlbumIE(InfoExtractor):
  97. _VALID_URL = r'https?://(?:i\.)?imgur\.com/(gallery/)?(?P<id>[a-zA-Z0-9]{5})(?![a-zA-Z0-9])'
  98. _TEST = {
  99. 'url': 'http://imgur.com/gallery/Q95ko',
  100. 'info_dict': {
  101. 'id': 'Q95ko',
  102. },
  103. 'playlist_count': 25,
  104. }
  105. def _real_extract(self, url):
  106. album_id = self._match_id(url)
  107. album_img_data = self._download_json(
  108. 'http://imgur.com/gallery/%s/album_images/hit.json?all=true' % album_id, album_id)['data']
  109. if len(album_img_data) == 0:
  110. return self.url_result('http://imgur.com/%s' % album_id)
  111. else:
  112. album_images = album_img_data['images']
  113. entries = [
  114. self.url_result('http://imgur.com/%s' % image['hash'])
  115. for image in album_images if image.get('hash')]
  116. return self.playlist_result(entries, album_id)