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.

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