[9gag] Code clean up

This commit is contained in:
DrWursterich 2019-11-09 03:18:49 +01:00
parent 9406e8cc39
commit 2cf467f133

View File

@ -5,6 +5,9 @@ import re
from .common import InfoExtractor from .common import InfoExtractor
from ..utils import ( from ..utils import (
determine_ext, determine_ext,
url_or_none,
int_or_none,
float_or_none,
ExtractorError ExtractorError
) )
@ -52,14 +55,16 @@ class NineGagIE(InfoExtractor):
video_id = self._match_id(url) video_id = self._match_id(url)
webpage = self._download_webpage(url, video_id) webpage = self._download_webpage(url, video_id)
rawJsonData = self._search_regex( rawJsonData = self._search_regex(
r'window._config\s*=\s*JSON.parse\("({.+?})"\);', r'window._config\s*=\s*JSON.parse\(["\']({.+?})["\']\);',
webpage, webpage,
'data') 'data')
rawJsonData = rawJsonData.replace('\\"', '"').replace('\\\\/', '/') rawJsonData = rawJsonData.replace('\\"', '"').replace('\\\\/', '/')
data = self._parse_json(rawJsonData, video_id)['data']['post'] data = self._parse_json(rawJsonData, video_id)['data']['post']
if data['type'] != 'Animated': if data['type'] != 'Animated':
raise ExtractorError('The given url does not contain a video', expected=True) raise ExtractorError(
'The given url does not contain a video',
expected=True)
duration = None duration = None
formats = [] formats = []
@ -67,14 +72,16 @@ class NineGagIE(InfoExtractor):
for key in data['images']: for key in data['images']:
image = data['images'][key] image = data['images'][key]
if 'duration' in image and duration is None: if 'duration' in image and duration is None:
duration = image['duration'] duration = int_or_none(image['duration'])
url = image['url'] url = url_or_none(image.get('url'))
if url == None:
continue
ext = determine_ext(url) ext = determine_ext(url)
if ext == 'jpg' or ext == 'png': if ext == 'jpg' or ext == 'png':
thumbnail = { thumbnail = {
'url': url, 'url': url,
'width': image['width'], 'width': float_or_none(image.get('width')),
'height': image['height'] 'height': float_or_none(image.get('height'))
} }
thumbnails.append(thumbnail) thumbnails.append(thumbnail)
elif ext == 'webm' or ext == 'mp4': elif ext == 'webm' or ext == 'mp4':
@ -82,27 +89,33 @@ class NineGagIE(InfoExtractor):
'format_id': re.sub(r'.*_([^\.]+).(.*)', r'\1_\2', url), 'format_id': re.sub(r'.*_([^\.]+).(.*)', r'\1_\2', url),
'ext': ext, 'ext': ext,
'url': url, 'url': url,
'width': image['width'], 'width': float_or_none(image.get('width')),
'height': image['height'] 'height': float_or_none(image.get('height'))
}) })
section = re.sub(r'\\[^\\]{5}', '', data['postSection']['name']) 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 tags = None
if 'tags' in data: if 'tags' in data:
tags = [] tags = []
for tag in data['tags']: for tag in data.get('tags') or []:
tags.append(tag['key']) tags.append(tag.get('key'))
return { return {
'id': video_id, 'id': video_id,
'title': data['title'], 'title': data['title'],
'timestamp': data['creationTs'], 'timestamp': int_or_none(data.get('creationTs')),
'duration': duration, 'duration': duration,
'formats': formats, 'formats': formats,
'thumbnails': thumbnails, 'thumbnails': thumbnails,
'like_count': data['upVoteCount'], 'like_count': int_or_none(data.get('upVoteCount')),
'dislike_count': data['downVoteCount'], 'dislike_count': int_or_none(data.get('downVoteCount')),
'comment_count': data['commentsCount'], 'comment_count': int_or_none(data.get('commentsCount')),
'age_limit': data['nsfw'] * 18, 'age_limit': age_limit,
'categories': [section], 'categories': [section],
'tags': tags, 'tags': tags,
'is_live': False 'is_live': False