gfycat.py 3.8 KB

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