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.

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