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.

90 lines
2.8 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. )
  9. class GfycatIE(InfoExtractor):
  10. _VALID_URL = r'https?://(?:www\.)?gfycat\.com/(?P<id>[^/?#]+)'
  11. _TEST = {
  12. 'url': 'http://gfycat.com/DeadlyDecisiveGermanpinscher',
  13. 'info_dict': {
  14. 'id': 'DeadlyDecisiveGermanpinscher',
  15. 'ext': 'mp4',
  16. 'title': 'Ghost in the Shell',
  17. 'timestamp': 1410656006,
  18. 'upload_date': '20140914',
  19. 'uploader': 'anonymous',
  20. 'duration': 10.4,
  21. 'view_count': int,
  22. 'like_count': int,
  23. 'dislike_count': int,
  24. 'categories': list,
  25. 'age_limit': 0,
  26. }
  27. }
  28. def _real_extract(self, url):
  29. video_id = self._match_id(url)
  30. gfy = self._download_json(
  31. 'http://gfycat.com/cajax/get/%s' % video_id,
  32. video_id, 'Downloading video info')['gfyItem']
  33. title = gfy.get('title') or gfy['gfyName']
  34. description = gfy.get('description')
  35. timestamp = int_or_none(gfy.get('createDate'))
  36. uploader = gfy.get('userName')
  37. view_count = int_or_none(gfy.get('views'))
  38. like_count = int_or_none(gfy.get('likes'))
  39. dislike_count = int_or_none(gfy.get('dislikes'))
  40. age_limit = 18 if gfy.get('nsfw') == '1' else 0
  41. width = int_or_none(gfy.get('width'))
  42. height = int_or_none(gfy.get('height'))
  43. fps = int_or_none(gfy.get('frameRate'))
  44. num_frames = int_or_none(gfy.get('numFrames'))
  45. duration = float_or_none(num_frames, fps) if num_frames and fps else None
  46. categories = gfy.get('tags') or gfy.get('extraLemmas') or []
  47. FORMATS = ('gif', 'webm', 'mp4')
  48. quality = qualities(FORMATS)
  49. formats = []
  50. for format_id in FORMATS:
  51. video_url = gfy.get('%sUrl' % format_id)
  52. if not video_url:
  53. continue
  54. filesize = gfy.get('%sSize' % format_id)
  55. formats.append({
  56. 'url': video_url,
  57. 'format_id': format_id,
  58. 'width': width,
  59. 'height': height,
  60. 'fps': fps,
  61. 'filesize': filesize,
  62. 'quality': quality(format_id),
  63. })
  64. self._sort_formats(formats)
  65. return {
  66. 'id': video_id,
  67. 'title': title,
  68. 'description': description,
  69. 'timestamp': timestamp,
  70. 'uploader': uploader,
  71. 'duration': duration,
  72. 'view_count': view_count,
  73. 'like_count': like_count,
  74. 'dislike_count': dislike_count,
  75. 'categories': categories,
  76. 'age_limit': age_limit,
  77. 'formats': formats,
  78. }