You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

147 lines
5.0 KiB

10 years ago
10 years ago
10 years ago
10 years ago
  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|topic/[^/]+)/)?(?P<id>[a-zA-Z0-9]{6,})(?:[/?#&]+|\.[a-z]+)?$'
  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. 'url': 'http://imgur.com/topic/Funny/N8rOudd',
  39. 'only_matching': True,
  40. }]
  41. def _real_extract(self, url):
  42. video_id = self._match_id(url)
  43. webpage = self._download_webpage(
  44. compat_urlparse.urljoin(url, video_id), video_id)
  45. width = int_or_none(self._og_search_property(
  46. 'video:width', webpage, default=None))
  47. height = int_or_none(self._og_search_property(
  48. 'video:height', webpage, default=None))
  49. video_elements = self._search_regex(
  50. r'(?s)<div class="video-elements">(.*?)</div>',
  51. webpage, 'video elements', default=None)
  52. if not video_elements:
  53. raise ExtractorError(
  54. 'No sources found for video %s. Maybe an image?' % video_id,
  55. expected=True)
  56. formats = []
  57. for m in re.finditer(r'<source\s+src="(?P<src>[^"]+)"\s+type="(?P<type>[^"]+)"', video_elements):
  58. formats.append({
  59. 'format_id': m.group('type').partition('/')[2],
  60. 'url': self._proto_relative_url(m.group('src')),
  61. 'ext': mimetype2ext(m.group('type')),
  62. 'acodec': 'none',
  63. 'width': width,
  64. 'height': height,
  65. 'http_headers': {
  66. 'User-Agent': 'youtube-dl (like wget)',
  67. },
  68. })
  69. gif_json = self._search_regex(
  70. r'(?s)var\s+videoItem\s*=\s*(\{.*?\})',
  71. webpage, 'GIF code', fatal=False)
  72. if gif_json:
  73. gifd = self._parse_json(
  74. gif_json, video_id, transform_source=js_to_json)
  75. formats.append({
  76. 'format_id': 'gif',
  77. 'preference': -10,
  78. 'width': width,
  79. 'height': height,
  80. 'ext': 'gif',
  81. 'acodec': 'none',
  82. 'vcodec': 'gif',
  83. 'container': 'gif',
  84. 'url': self._proto_relative_url(gifd['gifUrl']),
  85. 'filesize': gifd.get('size'),
  86. 'http_headers': {
  87. 'User-Agent': 'youtube-dl (like wget)',
  88. },
  89. })
  90. self._sort_formats(formats)
  91. return {
  92. 'id': video_id,
  93. 'formats': formats,
  94. 'description': self._og_search_description(webpage),
  95. 'title': self._og_search_title(webpage),
  96. }
  97. class ImgurAlbumIE(InfoExtractor):
  98. _VALID_URL = r'https?://(?:i\.)?imgur\.com/(?:(?:a|gallery|topic/[^/]+)/)?(?P<id>[a-zA-Z0-9]{5})(?:[/?#&]+)?$'
  99. _TESTS = [{
  100. 'url': 'http://imgur.com/gallery/Q95ko',
  101. 'info_dict': {
  102. 'id': 'Q95ko',
  103. },
  104. 'playlist_count': 25,
  105. }, {
  106. 'url': 'http://imgur.com/a/j6Orj',
  107. 'only_matching': True,
  108. }, {
  109. 'url': 'http://imgur.com/topic/Aww/ll5Vk',
  110. 'only_matching': True,
  111. }]
  112. def _real_extract(self, url):
  113. album_id = self._match_id(url)
  114. album_images = self._download_json(
  115. 'http://imgur.com/gallery/%s/album_images/hit.json?all=true' % album_id,
  116. album_id, fatal=False)
  117. if album_images:
  118. data = album_images.get('data')
  119. if data and isinstance(data, dict):
  120. images = data.get('images')
  121. if images and isinstance(images, list):
  122. entries = [
  123. self.url_result('http://imgur.com/%s' % image['hash'])
  124. for image in images if image.get('hash')]
  125. return self.playlist_result(entries, album_id)
  126. # Fallback to single video
  127. return self.url_result('http://imgur.com/%s' % album_id, ImgurIE.ie_key())