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.

113 lines
3.6 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\.)?gfycat\.com/(?: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/gifs/detail/UnconsciousLankyIvorygull',
  46. 'only_matching': True
  47. }]
  48. def _real_extract(self, url):
  49. video_id = self._match_id(url)
  50. gfy = self._download_json(
  51. 'https://api.gfycat.com/v1/gfycats/%s' % video_id,
  52. video_id, 'Downloading video info')
  53. if 'error' in gfy:
  54. raise ExtractorError('Gfycat said: ' + gfy['error'], expected=True)
  55. gfy = gfy['gfyItem']
  56. title = gfy.get('title') or gfy['gfyName']
  57. description = gfy.get('description')
  58. timestamp = int_or_none(gfy.get('createDate'))
  59. uploader = gfy.get('userName')
  60. view_count = int_or_none(gfy.get('views'))
  61. like_count = int_or_none(gfy.get('likes'))
  62. dislike_count = int_or_none(gfy.get('dislikes'))
  63. age_limit = 18 if gfy.get('nsfw') == '1' else 0
  64. width = int_or_none(gfy.get('width'))
  65. height = int_or_none(gfy.get('height'))
  66. fps = int_or_none(gfy.get('frameRate'))
  67. num_frames = int_or_none(gfy.get('numFrames'))
  68. duration = float_or_none(num_frames, fps) if num_frames and fps else None
  69. categories = gfy.get('tags') or gfy.get('extraLemmas') or []
  70. FORMATS = ('gif', 'webm', 'mp4')
  71. quality = qualities(FORMATS)
  72. formats = []
  73. for format_id in FORMATS:
  74. video_url = gfy.get('%sUrl' % format_id)
  75. if not video_url:
  76. continue
  77. filesize = int_or_none(gfy.get('%sSize' % format_id))
  78. formats.append({
  79. 'url': video_url,
  80. 'format_id': format_id,
  81. 'width': width,
  82. 'height': height,
  83. 'fps': fps,
  84. 'filesize': filesize,
  85. 'quality': quality(format_id),
  86. })
  87. self._sort_formats(formats)
  88. return {
  89. 'id': video_id,
  90. 'title': title,
  91. 'description': description,
  92. 'timestamp': timestamp,
  93. 'uploader': uploader,
  94. 'duration': duration,
  95. 'view_count': view_count,
  96. 'like_count': like_count,
  97. 'dislike_count': dislike_count,
  98. 'categories': categories,
  99. 'age_limit': age_limit,
  100. 'formats': formats,
  101. }