1 from __future__ import unicode_literals
3 from .common import InfoExtractor
14 class NineGagIE(InfoExtractor):
16 _VALID_URL = r'https?://(?:www\.)?9gag\.com/gag/(?P<id>[^/?&#]+)'
19 'url': 'https://9gag.com/gag/ae5Ag7B',
23 'title': 'Capybara Agility Training',
24 'upload_date': '20191108',
25 'timestamp': 1573237208,
26 'categories': ['Awesome'],
27 'tags': ['Weimaraner', 'American Pit Bull Terrier'],
35 'url': 'https://9gag.com/gag/av5nvyb',
36 'only_matching': True,
39 def _real_extract(self, url):
40 post_id = self._match_id(url)
41 post = self._download_json(
42 'https://9gag.com/v1/post', post_id, query={
46 if post.get('type') != 'Animated':
48 'The given url does not contain a video',
51 title = unescapeHTML(post['title'])
56 for key, image in (post.get('images') or {}).items():
57 image_url = url_or_none(image.get('url'))
60 ext = determine_ext(image_url)
61 image_id = key.strip('image')
64 'width': int_or_none(image.get('width')),
65 'height': int_or_none(image.get('height')),
67 if ext in ('jpg', 'png'):
68 webp_url = image.get('webpUrl')
72 'id': image_id + '-webp',
80 thumbnails.append(common)
81 elif ext in ('webm', 'mp4'):
83 duration = int_or_none(image.get('duration'))
84 common['acodec'] = 'none' if image.get('hasAudio') == 0 else None
85 for vcodec in ('vp8', 'vp9', 'h265'):
86 c_url = image.get(vcodec + 'Url')
91 'format_id': image_id + '-' + vcodec,
98 'format_id': image_id,
100 formats.append(common)
101 self._sort_formats(formats)
103 section = try_get(post, lambda x: x['postSection']['name'])
106 post_tags = post.get('tags')
109 for tag in post_tags:
110 tag_key = tag.get('key')
115 get_count = lambda x: int_or_none(post.get(x + 'Count'))
120 'timestamp': int_or_none(post.get('creationTs')),
121 'duration': duration,
123 'thumbnails': thumbnails,
124 'like_count': get_count('upVote'),
125 'dislike_count': get_count('downVote'),
126 'comment_count': get_count('comments'),
127 'age_limit': 18 if post.get('nsfw') == 1 else None,
128 'categories': [section] if section else None,