youtube-dl/youtube_dl/extractor/ninegag.py

149 lines
4.8 KiB
Python
Raw Normal View History

2014-01-28 18:55:06 +01:00
from __future__ import unicode_literals
2013-12-05 14:29:08 +01:00
import re
from .common import InfoExtractor
2021-01-19 10:21:37 +01:00
from ..utils import (
determine_ext,
url_or_none,
int_or_none,
float_or_none,
ExtractorError
)
2013-12-05 14:29:08 +01:00
class NineGagIE(InfoExtractor):
IE_NAME = '9gag'
2021-01-19 10:21:37 +01:00
_VALID_URL = r'https?://(?:www\.)?9gag\.com/gag/(?P<id>[a-zA-Z0-9]+)'
2013-12-05 14:29:08 +01:00
2014-04-11 01:25:24 +02:00
_TESTS = [{
2021-01-19 10:21:37 +01:00
'url': 'https://9gag.com/gag/an5Qz5b',
2015-09-22 21:56:26 +02:00
'info_dict': {
2021-01-19 10:21:37 +01:00
'id': 'an5Qz5b',
'ext': 'webm',
'title': 'Dogs playing tetherball',
'upload_date': '20191108',
'timestamp': 1573243994,
'age_limit': 0,
'categories': [
'Wholesome'
],
'tags': [
'Dog'
]
}
2015-09-22 21:55:16 +02:00
}, {
2021-01-19 10:21:37 +01:00
'url': 'https://9gag.com/gag/ae5Ag7B',
2015-09-22 21:55:16 +02:00
'info_dict': {
2021-01-19 10:21:37 +01:00
'id': 'ae5Ag7B',
'ext': 'webm',
'title': 'Capybara Agility Training',
'upload_date': '20191108',
'timestamp': 1573237208,
'age_limit': 0,
'categories': [
'Awesome'
],
'tags': [
'Weimaraner',
'American Pit Bull Terrier'
]
}
2014-04-11 01:25:24 +02:00
}]
2015-09-22 21:58:47 +02:00
2021-01-19 10:21:37 +01:00
_EXTERNAL_VIDEO_PROVIDERS = {
'Youtube': 'https://youtube.com/watch?v=%s'
}
2013-12-05 14:29:08 +01:00
def _real_extract(self, url):
2021-01-19 10:21:37 +01:00
video_id = self._match_id(url)
webpage = self._download_webpage(url, video_id)
rawJsonData = self._search_regex(
r'window._config\s*=\s*JSON.parse\(["\']({.+?})["\']\);',
webpage,
'data')
rawJsonData = rawJsonData.replace('\\"', '"').replace('\\\\/', '/')
data = self._parse_json(rawJsonData, video_id)['data']['post']
if data['type'] == 'Video':
vid = data['video']['id']
ie_key = data['video']['source'].capitalize()
return {
'_type': 'url_transparent',
'url': self._EXTERNAL_VIDEO_PROVIDERS[ie_key] % vid,
'ie_key': ie_key,
'id': vid,
'duration': data['video'].get('duration'),
'start_time': data['video'].get('startTs')
}
2013-12-05 14:29:08 +01:00
2021-01-19 10:21:37 +01:00
if data['type'] == 'EmbedVideo':
vid = data['video']['id']
ie_key = data['video']['source'].capitalize()
return {
'_type': 'url_transparent',
'url': data['video']['embedUrl'],
#'ie_key': vid,
'start_time': data['video'].get('startTs')
}
2013-12-05 14:29:08 +01:00
2021-01-19 10:21:37 +01:00
if data['type'] != 'Animated':
raise ExtractorError(
'The given url does not contain a video',
expected=True)
2014-04-15 14:49:38 +02:00
2021-01-19 10:21:37 +01:00
duration = None
formats = []
thumbnails = []
for key in data['images']:
image = data['images'][key]
if 'duration' in image and duration is None:
duration = int_or_none(image['duration'])
url = url_or_none(image.get('url'))
if url == None:
continue
ext = determine_ext(url)
if ext == 'jpg' or ext == 'png':
thumbnail = {
'url': url,
'width': float_or_none(image.get('width')),
'height': float_or_none(image.get('height'))
}
thumbnails.append(thumbnail)
elif ext == 'webm' or ext == 'mp4':
formats.append({
'format_id': re.sub(r'.*_([^\.]+).(.*)', r'\1_\2', url),
'ext': ext,
'url': url,
'width': float_or_none(image.get('width')),
'height': float_or_none(image.get('height'))
})
section = None
postSection = data.get('postSection')
if postSection != None and 'name' in postSection:
section = re.sub(r'\\[^\\]{5}', '', postSection['name'])
age_limit = int_or_none(data.get('nsfw'))
if age_limit != None:
age_limit = age_limit * 18
tags = None
if 'tags' in data:
tags = []
for tag in data.get('tags') or []:
tags.append(tag.get('key'))
2013-12-05 14:29:08 +01:00
return {
'id': video_id,
2021-01-19 10:21:37 +01:00
'title': data['title'],
'timestamp': int_or_none(data.get('creationTs')),
'duration': duration,
'formats': formats,
'thumbnails': thumbnails,
'like_count': int_or_none(data.get('upVoteCount')),
'dislike_count': int_or_none(data.get('downVoteCount')),
'comment_count': int_or_none(data.get('commentsCount')),
'age_limit': age_limit,
'categories': [section],
'tags': tags,
'is_live': False
2013-12-05 14:29:08 +01:00
}