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.

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