logo

youtube-dl

[mirror] Download/Watch videos from video hostersgit clone https://hacktivis.me/git/mirror/youtube-dl.git

ninegag.py (4248B)


  1. from __future__ import unicode_literals
  2. from .common import InfoExtractor
  3. from ..utils import (
  4. ExtractorError,
  5. determine_ext,
  6. int_or_none,
  7. try_get,
  8. unescapeHTML,
  9. url_or_none,
  10. )
  11. class NineGagIE(InfoExtractor):
  12. IE_NAME = '9gag'
  13. _VALID_URL = r'https?://(?:www\.)?9gag\.com/gag/(?P<id>[^/?&#]+)'
  14. _TESTS = [{
  15. 'url': 'https://9gag.com/gag/ae5Ag7B',
  16. 'info_dict': {
  17. 'id': 'ae5Ag7B',
  18. 'ext': 'mp4',
  19. 'title': 'Capybara Agility Training',
  20. 'upload_date': '20191108',
  21. 'timestamp': 1573237208,
  22. 'categories': ['Awesome'],
  23. 'tags': ['Weimaraner', 'American Pit Bull Terrier'],
  24. 'duration': 44,
  25. 'like_count': int,
  26. 'dislike_count': int,
  27. 'comment_count': int,
  28. }
  29. }, {
  30. # HTML escaped title
  31. 'url': 'https://9gag.com/gag/av5nvyb',
  32. 'only_matching': True,
  33. }]
  34. def _real_extract(self, url):
  35. post_id = self._match_id(url)
  36. post = self._download_json(
  37. 'https://9gag.com/v1/post', post_id, query={
  38. 'id': post_id
  39. })['data']['post']
  40. if post.get('type') != 'Animated':
  41. raise ExtractorError(
  42. 'The given url does not contain a video',
  43. expected=True)
  44. title = unescapeHTML(post['title'])
  45. duration = None
  46. formats = []
  47. thumbnails = []
  48. for key, image in (post.get('images') or {}).items():
  49. image_url = url_or_none(image.get('url'))
  50. if not image_url:
  51. continue
  52. ext = determine_ext(image_url)
  53. image_id = key.strip('image')
  54. common = {
  55. 'url': image_url,
  56. 'width': int_or_none(image.get('width')),
  57. 'height': int_or_none(image.get('height')),
  58. }
  59. if ext in ('jpg', 'png'):
  60. webp_url = image.get('webpUrl')
  61. if webp_url:
  62. t = common.copy()
  63. t.update({
  64. 'id': image_id + '-webp',
  65. 'url': webp_url,
  66. })
  67. thumbnails.append(t)
  68. common.update({
  69. 'id': image_id,
  70. 'ext': ext,
  71. })
  72. thumbnails.append(common)
  73. elif ext in ('webm', 'mp4'):
  74. if not duration:
  75. duration = int_or_none(image.get('duration'))
  76. common['acodec'] = 'none' if image.get('hasAudio') == 0 else None
  77. for vcodec in ('vp8', 'vp9', 'h265'):
  78. c_url = image.get(vcodec + 'Url')
  79. if not c_url:
  80. continue
  81. c_f = common.copy()
  82. c_f.update({
  83. 'format_id': image_id + '-' + vcodec,
  84. 'url': c_url,
  85. 'vcodec': vcodec,
  86. })
  87. formats.append(c_f)
  88. common.update({
  89. 'ext': ext,
  90. 'format_id': image_id,
  91. })
  92. formats.append(common)
  93. self._sort_formats(formats)
  94. section = try_get(post, lambda x: x['postSection']['name'])
  95. tags = None
  96. post_tags = post.get('tags')
  97. if post_tags:
  98. tags = []
  99. for tag in post_tags:
  100. tag_key = tag.get('key')
  101. if not tag_key:
  102. continue
  103. tags.append(tag_key)
  104. get_count = lambda x: int_or_none(post.get(x + 'Count'))
  105. return {
  106. 'id': post_id,
  107. 'title': title,
  108. 'timestamp': int_or_none(post.get('creationTs')),
  109. 'duration': duration,
  110. 'formats': formats,
  111. 'thumbnails': thumbnails,
  112. 'like_count': get_count('upVote'),
  113. 'dislike_count': get_count('downVote'),
  114. 'comment_count': get_count('comments'),
  115. 'age_limit': 18 if post.get('nsfw') == 1 else None,
  116. 'categories': [section] if section else None,
  117. 'tags': tags,
  118. }