2013-12-05 14:29:08 +01:00
|
|
|
from .common import InfoExtractor
|
2021-01-16 18:12:05 +01:00
|
|
|
from ..utils import (
|
|
|
|
ExtractorError,
|
2021-02-19 21:44:36 +01:00
|
|
|
determine_ext,
|
2021-01-16 18:12:05 +01:00
|
|
|
int_or_none,
|
2022-08-07 22:21:53 +02:00
|
|
|
traverse_obj,
|
2021-02-19 21:44:36 +01:00
|
|
|
unescapeHTML,
|
2021-01-16 18:12:05 +01:00
|
|
|
url_or_none,
|
|
|
|
)
|
2013-12-05 14:29:08 +01:00
|
|
|
|
|
|
|
|
|
|
|
class NineGagIE(InfoExtractor):
|
|
|
|
IE_NAME = '9gag'
|
2022-08-07 22:21:53 +02:00
|
|
|
IE_DESC = '9GAG'
|
2021-01-16 18:12:05 +01:00
|
|
|
_VALID_URL = r'https?://(?:www\.)?9gag\.com/gag/(?P<id>[^/?&#]+)'
|
2013-12-05 14:29:08 +01:00
|
|
|
|
2021-02-19 21:44:36 +01:00
|
|
|
_TESTS = [{
|
2021-01-16 18:12:05 +01:00
|
|
|
'url': 'https://9gag.com/gag/ae5Ag7B',
|
2015-09-22 21:55:16 +02:00
|
|
|
'info_dict': {
|
2021-01-16 18:12:05 +01:00
|
|
|
'id': 'ae5Ag7B',
|
2022-08-07 22:21:53 +02:00
|
|
|
'ext': 'webm',
|
2021-01-16 18:12:05 +01:00
|
|
|
'title': 'Capybara Agility Training',
|
|
|
|
'upload_date': '20191108',
|
|
|
|
'timestamp': 1573237208,
|
2022-08-07 22:21:53 +02:00
|
|
|
'thumbnail': 'https://img-9gag-fun.9cache.com/photo/ae5Ag7B_460s.jpg',
|
2021-01-16 18:12:05 +01:00
|
|
|
'categories': ['Awesome'],
|
2022-08-07 22:21:53 +02:00
|
|
|
'tags': ['Awesome'],
|
2021-01-16 18:12:05 +01:00
|
|
|
'duration': 44,
|
|
|
|
'like_count': int,
|
|
|
|
'dislike_count': int,
|
|
|
|
'comment_count': int,
|
2024-06-12 01:09:58 +02:00
|
|
|
},
|
2021-02-19 21:44:36 +01:00
|
|
|
}, {
|
|
|
|
# HTML escaped title
|
|
|
|
'url': 'https://9gag.com/gag/av5nvyb',
|
|
|
|
'only_matching': True,
|
2022-08-07 22:21:53 +02:00
|
|
|
}, {
|
|
|
|
# Non Anonymous Uploader
|
|
|
|
'url': 'https://9gag.com/gag/ajgp66G',
|
|
|
|
'info_dict': {
|
|
|
|
'id': 'ajgp66G',
|
|
|
|
'ext': 'webm',
|
|
|
|
'title': 'Master Shifu! Or Splinter! You decide:',
|
|
|
|
'upload_date': '20220806',
|
|
|
|
'timestamp': 1659803411,
|
|
|
|
'thumbnail': 'https://img-9gag-fun.9cache.com/photo/ajgp66G_460s.jpg',
|
|
|
|
'categories': ['Funny'],
|
|
|
|
'tags': ['Funny'],
|
|
|
|
'duration': 26,
|
|
|
|
'like_count': int,
|
|
|
|
'dislike_count': int,
|
|
|
|
'comment_count': int,
|
|
|
|
'uploader': 'Peter Klaus',
|
|
|
|
'uploader_id': 'peterklaus12',
|
|
|
|
'uploader_url': 'https://9gag.com/u/peterklaus12',
|
2024-06-12 01:09:58 +02:00
|
|
|
},
|
2021-02-19 21:44:36 +01:00
|
|
|
}]
|
2013-12-05 14:29:08 +01:00
|
|
|
|
|
|
|
def _real_extract(self, url):
|
2021-01-16 18:12:05 +01:00
|
|
|
post_id = self._match_id(url)
|
|
|
|
post = self._download_json(
|
|
|
|
'https://9gag.com/v1/post', post_id, query={
|
2024-06-12 01:09:58 +02:00
|
|
|
'id': post_id,
|
2021-01-16 18:12:05 +01:00
|
|
|
})['data']['post']
|
|
|
|
|
|
|
|
if post.get('type') != 'Animated':
|
|
|
|
raise ExtractorError(
|
|
|
|
'The given url does not contain a video',
|
|
|
|
expected=True)
|
|
|
|
|
|
|
|
duration = None
|
|
|
|
formats = []
|
|
|
|
thumbnails = []
|
|
|
|
for key, image in (post.get('images') or {}).items():
|
|
|
|
image_url = url_or_none(image.get('url'))
|
|
|
|
if not image_url:
|
|
|
|
continue
|
|
|
|
ext = determine_ext(image_url)
|
|
|
|
image_id = key.strip('image')
|
|
|
|
common = {
|
|
|
|
'url': image_url,
|
|
|
|
'width': int_or_none(image.get('width')),
|
|
|
|
'height': int_or_none(image.get('height')),
|
|
|
|
}
|
|
|
|
if ext in ('jpg', 'png'):
|
|
|
|
webp_url = image.get('webpUrl')
|
|
|
|
if webp_url:
|
|
|
|
t = common.copy()
|
|
|
|
t.update({
|
|
|
|
'id': image_id + '-webp',
|
|
|
|
'url': webp_url,
|
|
|
|
})
|
|
|
|
thumbnails.append(t)
|
|
|
|
common.update({
|
|
|
|
'id': image_id,
|
|
|
|
'ext': ext,
|
|
|
|
})
|
|
|
|
thumbnails.append(common)
|
|
|
|
elif ext in ('webm', 'mp4'):
|
|
|
|
if not duration:
|
|
|
|
duration = int_or_none(image.get('duration'))
|
|
|
|
common['acodec'] = 'none' if image.get('hasAudio') == 0 else None
|
|
|
|
for vcodec in ('vp8', 'vp9', 'h265'):
|
|
|
|
c_url = image.get(vcodec + 'Url')
|
|
|
|
if not c_url:
|
|
|
|
continue
|
|
|
|
c_f = common.copy()
|
|
|
|
c_f.update({
|
|
|
|
'format_id': image_id + '-' + vcodec,
|
|
|
|
'url': c_url,
|
|
|
|
'vcodec': vcodec,
|
|
|
|
})
|
|
|
|
formats.append(c_f)
|
|
|
|
common.update({
|
|
|
|
'ext': ext,
|
|
|
|
'format_id': image_id,
|
|
|
|
})
|
|
|
|
formats.append(common)
|
2013-12-05 14:29:08 +01:00
|
|
|
|
2022-08-07 22:21:53 +02:00
|
|
|
section = traverse_obj(post, ('postSection', 'name'))
|
2013-12-05 14:29:08 +01:00
|
|
|
|
2021-01-16 18:12:05 +01:00
|
|
|
tags = None
|
|
|
|
post_tags = post.get('tags')
|
|
|
|
if post_tags:
|
|
|
|
tags = []
|
|
|
|
for tag in post_tags:
|
|
|
|
tag_key = tag.get('key')
|
|
|
|
if not tag_key:
|
|
|
|
continue
|
|
|
|
tags.append(tag_key)
|
2014-04-15 14:49:38 +02:00
|
|
|
|
2013-12-05 14:29:08 +01:00
|
|
|
return {
|
2021-01-16 18:12:05 +01:00
|
|
|
'id': post_id,
|
2022-08-07 22:21:53 +02:00
|
|
|
'title': unescapeHTML(post.get('title')),
|
2021-01-16 18:12:05 +01:00
|
|
|
'timestamp': int_or_none(post.get('creationTs')),
|
|
|
|
'duration': duration,
|
2022-08-07 22:21:53 +02:00
|
|
|
'uploader': traverse_obj(post, ('creator', 'fullName')),
|
|
|
|
'uploader_id': traverse_obj(post, ('creator', 'username')),
|
|
|
|
'uploader_url': url_or_none(traverse_obj(post, ('creator', 'profileUrl'))),
|
2021-01-16 18:12:05 +01:00
|
|
|
'formats': formats,
|
|
|
|
'thumbnails': thumbnails,
|
2022-08-07 22:21:53 +02:00
|
|
|
'like_count': int_or_none(post.get('upVoteCount')),
|
|
|
|
'dislike_count': int_or_none(post.get('downVoteCount')),
|
|
|
|
'comment_count': int_or_none(post.get('commentsCount')),
|
2021-01-16 18:12:05 +01:00
|
|
|
'age_limit': 18 if post.get('nsfw') == 1 else None,
|
|
|
|
'categories': [section] if section else None,
|
|
|
|
'tags': tags,
|
2013-12-05 14:29:08 +01:00
|
|
|
}
|