gfycat.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import datetime
  4. from .common import InfoExtractor
  5. class GfycatIE(InfoExtractor):
  6. _VALID_URL = r'https?://(?:www\.)?gfycat\.com/(?P<id>[^/?#]+)'
  7. _TESTS = [{
  8. 'url': 'http://gfycat.com/DeadlyDecisiveGermanpinscher',
  9. 'info_dict': {
  10. 'id': 'DeadlyDecisiveGermanpinscher',
  11. 'title': 'Ghost in the Shell',
  12. 'ext': 'mp4',
  13. 'upload_date': '20140913'
  14. }
  15. },{
  16. 'url': 'http://gfycat.com/pleasinghilariouskusimanse',
  17. 'info_dict': {
  18. 'id': 'pleasinghilariouskusimanse',
  19. 'title': 'PleasingHilariousKusimanse',
  20. 'ext': 'webm',
  21. 'upload_date': '20150412'
  22. }
  23. },{
  24. 'url': 'http://gfycat.com/requiredunkemptbuzzard',
  25. 'info_dict': {
  26. 'id': 'requiredunkemptbuzzard',
  27. 'title': 'Headshot!',
  28. 'ext': 'gif',
  29. 'upload_date': '20150130'
  30. }
  31. }]
  32. def _real_extract(self, url):
  33. video_id = self._match_id(url)
  34. json = self._download_json("http://gfycat.com/cajax/get/" + video_id, video_id, 'Downloading video info')['gfyItem']
  35. # Title
  36. # Use user title first, else fallback to url formated name
  37. if json['title']:
  38. video_title = json['title']
  39. else:
  40. video_title = json['gfyName']
  41. # Formats
  42. # Pref: mp4, webm, gif
  43. formats = [{
  44. 'format_id': 'mp4',
  45. 'ext': 'mp4',
  46. 'url': json['mp4Url'],
  47. 'width': json['width'],
  48. 'height': json['height'],
  49. 'fps': json['frameRate'],
  50. 'filesize': json['mp4Size'],
  51. 'preference': '-1'
  52. }, {
  53. 'format_id': 'webm',
  54. 'ext': 'webm',
  55. 'url': json['webmUrl'],
  56. 'width': json['width'],
  57. 'height': json['height'],
  58. 'fps': json['frameRate'],
  59. 'filesize': json['webmSize'],
  60. 'preference': 0
  61. }, {
  62. 'format_id': 'gif',
  63. 'ext': 'gif',
  64. 'url': json['gifUrl'],
  65. 'width': json['width'],
  66. 'height': json['height'],
  67. 'fps': json['frameRate'],
  68. 'filesize': json['gifSize'],
  69. 'preference': 1
  70. }]
  71. self._sort_formats(formats)
  72. # Date
  73. date = datetime.datetime.fromtimestamp(
  74. int(json['createDate'])
  75. ).strftime('%Y%m%d')
  76. # Length
  77. duration = json['numFrames'] / json['frameRate']
  78. # Age limit
  79. # 1 = nsfw / 0 = sfw
  80. if json['nsfw'] == 1:
  81. age_limit = 18
  82. else:
  83. age_limit = 0
  84. return {
  85. 'id': video_id,
  86. 'title': video_title,
  87. 'formats': formats,
  88. 'creator': json['userName'],
  89. 'description': json['description'],
  90. 'upload_date': date,
  91. 'categories': json['tags'],
  92. 'age_limit': age_limit,
  93. 'duration': duration,
  94. 'view_count': json['views']
  95. }