gfycat.py 3.5 KB

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