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.

125 lines
4.1 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. from .common import InfoExtractor
  4. from ..utils import (
  5. int_or_none,
  6. float_or_none,
  7. qualities,
  8. ExtractorError,
  9. )
  10. class GfycatIE(InfoExtractor):
  11. _VALID_URL = r'https?://(?:(?:www|giant|thumbs)\.)?gfycat\.com/(?:ru/|ifr/|gifs/detail/)?(?P<id>[^-/?#\.]+)'
  12. _TESTS = [{
  13. 'url': 'http://gfycat.com/DeadlyDecisiveGermanpinscher',
  14. 'info_dict': {
  15. 'id': 'DeadlyDecisiveGermanpinscher',
  16. 'ext': 'mp4',
  17. 'title': 'Ghost in the Shell',
  18. 'timestamp': 1410656006,
  19. 'upload_date': '20140914',
  20. 'uploader': 'anonymous',
  21. 'duration': 10.4,
  22. 'view_count': int,
  23. 'like_count': int,
  24. 'dislike_count': int,
  25. 'categories': list,
  26. 'age_limit': 0,
  27. }
  28. }, {
  29. 'url': 'http://gfycat.com/ifr/JauntyTimelyAmazontreeboa',
  30. 'info_dict': {
  31. 'id': 'JauntyTimelyAmazontreeboa',
  32. 'ext': 'mp4',
  33. 'title': 'JauntyTimelyAmazontreeboa',
  34. 'timestamp': 1411720126,
  35. 'upload_date': '20140926',
  36. 'uploader': 'anonymous',
  37. 'duration': 3.52,
  38. 'view_count': int,
  39. 'like_count': int,
  40. 'dislike_count': int,
  41. 'categories': list,
  42. 'age_limit': 0,
  43. }
  44. }, {
  45. 'url': 'https://gfycat.com/ru/RemarkableDrearyAmurstarfish',
  46. 'only_matching': True
  47. }, {
  48. 'url': 'https://gfycat.com/gifs/detail/UnconsciousLankyIvorygull',
  49. 'only_matching': True
  50. }, {
  51. 'url': 'https://gfycat.com/acceptablehappygoluckyharborporpoise-baseball',
  52. 'only_matching': True
  53. }, {
  54. 'url': 'https://thumbs.gfycat.com/acceptablehappygoluckyharborporpoise-size_restricted.gif',
  55. 'only_matching': True
  56. }, {
  57. 'url': 'https://giant.gfycat.com/acceptablehappygoluckyharborporpoise.mp4',
  58. 'only_matching': True
  59. }]
  60. def _real_extract(self, url):
  61. video_id = self._match_id(url)
  62. gfy = self._download_json(
  63. 'https://api.gfycat.com/v1/gfycats/%s' % video_id,
  64. video_id, 'Downloading video info')
  65. if 'error' in gfy:
  66. raise ExtractorError('Gfycat said: ' + gfy['error'], expected=True)
  67. gfy = gfy['gfyItem']
  68. title = gfy.get('title') or gfy['gfyName']
  69. description = gfy.get('description')
  70. timestamp = int_or_none(gfy.get('createDate'))
  71. uploader = gfy.get('userName')
  72. view_count = int_or_none(gfy.get('views'))
  73. like_count = int_or_none(gfy.get('likes'))
  74. dislike_count = int_or_none(gfy.get('dislikes'))
  75. age_limit = 18 if gfy.get('nsfw') == '1' else 0
  76. width = int_or_none(gfy.get('width'))
  77. height = int_or_none(gfy.get('height'))
  78. fps = int_or_none(gfy.get('frameRate'))
  79. num_frames = int_or_none(gfy.get('numFrames'))
  80. duration = float_or_none(num_frames, fps) if num_frames and fps else None
  81. categories = gfy.get('tags') or gfy.get('extraLemmas') or []
  82. FORMATS = ('gif', 'webm', 'mp4')
  83. quality = qualities(FORMATS)
  84. formats = []
  85. for format_id in FORMATS:
  86. video_url = gfy.get('%sUrl' % format_id)
  87. if not video_url:
  88. continue
  89. filesize = int_or_none(gfy.get('%sSize' % format_id))
  90. formats.append({
  91. 'url': video_url,
  92. 'format_id': format_id,
  93. 'width': width,
  94. 'height': height,
  95. 'fps': fps,
  96. 'filesize': filesize,
  97. 'quality': quality(format_id),
  98. })
  99. self._sort_formats(formats)
  100. return {
  101. 'id': video_id,
  102. 'title': title,
  103. 'description': description,
  104. 'timestamp': timestamp,
  105. 'uploader': uploader,
  106. 'duration': duration,
  107. 'view_count': view_count,
  108. 'like_count': like_count,
  109. 'dislike_count': dislike_count,
  110. 'categories': categories,
  111. 'age_limit': age_limit,
  112. 'formats': formats,
  113. }