imgur.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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/(?P<id>[a-zA-Z0-9]+)(?:\.mp4|\.gifv)?'
  12. _TESTS = [{
  13. 'url': 'https://i.imgur.com/A61SaA1.gifv',
  14. 'info_dict': {
  15. 'id': 'A61SaA1',
  16. 'ext': 'mp4',
  17. 'title': 'MRW gifv is up and running without any bugs',
  18. 'description': 'The Internet\'s visual storytelling community. Explore, share, and discuss the best visual stories the Internet has to offer.',
  19. },
  20. }, {
  21. 'url': 'https://imgur.com/A61SaA1',
  22. 'info_dict': {
  23. 'id': 'A61SaA1',
  24. 'ext': 'mp4',
  25. 'title': 'MRW gifv is up and running without any bugs',
  26. 'description': 'The Internet\'s visual storytelling community. Explore, share, and discuss the best visual stories the Internet has to offer.',
  27. },
  28. }]
  29. def _real_extract(self, url):
  30. video_id = self._match_id(url)
  31. webpage = self._download_webpage(url, video_id)
  32. sources = re.findall(r'<source src="([^"]+)" type="([^"]+)"', webpage)
  33. if not sources:
  34. raise ExtractorError(
  35. 'No sources found for video %s' % video_id, expected=True)
  36. width = int_or_none(self._search_regex(
  37. r'<param name="width" value="([0-9]+)"',
  38. webpage, 'width', fatal=False))
  39. height = int_or_none(self._search_regex(
  40. r'<param name="height" value="([0-9]+)"',
  41. webpage, 'height', fatal=False))
  42. formats = []
  43. video_elements = self._search_regex(
  44. r'(?s)<div class="video-elements">(.*?)</div>',
  45. webpage, 'video elements')
  46. formats = []
  47. for m in re.finditer(r'<source\s+src="(?P<src>[^"]+)"\s+type="(?P<type>[^"]+)"', video_elements):
  48. formats.append({
  49. 'format_id': m.group('type').partition('/')[2],
  50. 'url': self._proto_relative_url(m.group('src')),
  51. 'ext': mimetype2ext(m.group('type')),
  52. 'acodec': 'none',
  53. 'width': width,
  54. 'height': height,
  55. 'http_headers': {
  56. 'User-Agent': 'youtube-dl (like wget)',
  57. },
  58. })
  59. gif_json = self._search_regex(
  60. r'(?s)var\s+videoItem\s*=\s*(\{.*?\})',
  61. webpage, 'GIF code', fatal=False)
  62. if gif_json:
  63. gifd = self._parse_json(
  64. gif_json, video_id, transform_source=js_to_json)
  65. formats.append({
  66. 'format_id': 'gif',
  67. 'preference': -10,
  68. 'width': width,
  69. 'height': height,
  70. 'ext': 'gif',
  71. 'acodec': 'none',
  72. 'vcodec': 'gif',
  73. 'container': 'gif',
  74. 'url': self._proto_relative_url(gifd['gifUrl']),
  75. 'filesize': gifd.get('size'),
  76. 'http_headers': {
  77. 'User-Agent': 'youtube-dl (like wget)',
  78. },
  79. })
  80. self._sort_formats(formats)
  81. return {
  82. 'id': video_id,
  83. 'formats': formats,
  84. 'description': self._og_search_description(webpage),
  85. 'title': self._og_search_title(webpage),
  86. }