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.

154 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 ..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/(?!(?:a|gallery|(?:t(?:opic)?|r)/[^/]+)/)(?P<id>[a-zA-Z0-9]+)'
  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. },
  19. }, {
  20. 'url': 'https://imgur.com/A61SaA1',
  21. 'only_matching': True,
  22. }, {
  23. 'url': 'https://i.imgur.com/crGpqCV.mp4',
  24. 'only_matching': True,
  25. }, {
  26. # no title
  27. 'url': 'https://i.imgur.com/jxBXAMC.gifv',
  28. 'only_matching': True,
  29. }]
  30. def _real_extract(self, url):
  31. video_id = self._match_id(url)
  32. webpage = self._download_webpage(
  33. 'https://i.imgur.com/{id}.gifv'.format(id=video_id), video_id)
  34. width = int_or_none(self._og_search_property(
  35. 'video:width', webpage, default=None))
  36. height = int_or_none(self._og_search_property(
  37. 'video:height', webpage, default=None))
  38. video_elements = self._search_regex(
  39. r'(?s)<div class="video-elements">(.*?)</div>',
  40. webpage, 'video elements', default=None)
  41. if not video_elements:
  42. raise ExtractorError(
  43. 'No sources found for video %s. Maybe an image?' % video_id,
  44. expected=True)
  45. formats = []
  46. for m in re.finditer(r'<source\s+src="(?P<src>[^"]+)"\s+type="(?P<type>[^"]+)"', video_elements):
  47. formats.append({
  48. 'format_id': m.group('type').partition('/')[2],
  49. 'url': self._proto_relative_url(m.group('src')),
  50. 'ext': mimetype2ext(m.group('type')),
  51. 'width': width,
  52. 'height': height,
  53. 'http_headers': {
  54. 'User-Agent': 'youtube-dl (like wget)',
  55. },
  56. })
  57. gif_json = self._search_regex(
  58. r'(?s)var\s+videoItem\s*=\s*(\{.*?\})',
  59. webpage, 'GIF code', fatal=False)
  60. if gif_json:
  61. gifd = self._parse_json(
  62. gif_json, video_id, transform_source=js_to_json)
  63. formats.append({
  64. 'format_id': 'gif',
  65. 'preference': -10,
  66. 'width': width,
  67. 'height': height,
  68. 'ext': 'gif',
  69. 'acodec': 'none',
  70. 'vcodec': 'gif',
  71. 'container': 'gif',
  72. 'url': self._proto_relative_url(gifd['gifUrl']),
  73. 'filesize': gifd.get('size'),
  74. 'http_headers': {
  75. 'User-Agent': 'youtube-dl (like wget)',
  76. },
  77. })
  78. self._sort_formats(formats)
  79. return {
  80. 'id': video_id,
  81. 'formats': formats,
  82. 'title': self._og_search_title(webpage, default=video_id),
  83. }
  84. class ImgurGalleryIE(InfoExtractor):
  85. IE_NAME = 'imgur:gallery'
  86. _VALID_URL = r'https?://(?:i\.)?imgur\.com/(?:gallery|(?:t(?:opic)?|r)/[^/]+)/(?P<id>[a-zA-Z0-9]+)'
  87. _TESTS = [{
  88. 'url': 'http://imgur.com/gallery/Q95ko',
  89. 'info_dict': {
  90. 'id': 'Q95ko',
  91. 'title': 'Adding faces make every GIF better',
  92. },
  93. 'playlist_count': 25,
  94. }, {
  95. 'url': 'http://imgur.com/topic/Aww/ll5Vk',
  96. 'only_matching': True,
  97. }, {
  98. 'url': 'https://imgur.com/gallery/YcAQlkx',
  99. 'info_dict': {
  100. 'id': 'YcAQlkx',
  101. 'ext': 'mp4',
  102. 'title': 'Classic Steve Carell gif...cracks me up everytime....damn the repost downvotes....',
  103. }
  104. }, {
  105. 'url': 'http://imgur.com/topic/Funny/N8rOudd',
  106. 'only_matching': True,
  107. }, {
  108. 'url': 'http://imgur.com/r/aww/VQcQPhM',
  109. 'only_matching': True,
  110. }]
  111. def _real_extract(self, url):
  112. gallery_id = self._match_id(url)
  113. data = self._download_json(
  114. 'https://imgur.com/gallery/%s.json' % gallery_id,
  115. gallery_id)['data']['image']
  116. if data.get('is_album'):
  117. entries = [
  118. self.url_result('http://imgur.com/%s' % image['hash'], ImgurIE.ie_key(), image['hash'])
  119. for image in data['album_images']['images'] if image.get('hash')]
  120. return self.playlist_result(entries, gallery_id, data.get('title'), data.get('description'))
  121. return self.url_result('http://imgur.com/%s' % gallery_id, ImgurIE.ie_key(), gallery_id)
  122. class ImgurAlbumIE(ImgurGalleryIE):
  123. IE_NAME = 'imgur:album'
  124. _VALID_URL = r'https?://(?:i\.)?imgur\.com/a/(?P<id>[a-zA-Z0-9]+)'
  125. _TESTS = [{
  126. 'url': 'http://imgur.com/a/j6Orj',
  127. 'info_dict': {
  128. 'id': 'j6Orj',
  129. 'title': 'A Literary Analysis of "Star Wars: The Force Awakens"',
  130. },
  131. 'playlist_count': 12,
  132. }]