imgur.py 3.3 KB

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