[patreon] minimize reponse size and extract uploader_id and filesize

This commit is contained in:
Remita Amine 2019-11-05 14:04:17 +01:00
parent e9b95167af
commit b77c3949e8

View file

@ -6,7 +6,11 @@ from ..utils import (
clean_html, clean_html,
determine_ext, determine_ext,
int_or_none, int_or_none,
KNOWN_EXTENSIONS,
mimetype2ext,
parse_iso8601, parse_iso8601,
str_or_none,
try_get,
) )
@ -24,6 +28,7 @@ class PatreonIE(InfoExtractor):
'thumbnail': 're:^https?://.*$', 'thumbnail': 're:^https?://.*$',
'timestamp': 1406473987, 'timestamp': 1406473987,
'upload_date': '20140727', 'upload_date': '20140727',
'uploader_id': '87145',
}, },
}, { }, {
'url': 'http://www.patreon.com/creation?hid=754133', 'url': 'http://www.patreon.com/creation?hid=754133',
@ -90,7 +95,13 @@ class PatreonIE(InfoExtractor):
def _real_extract(self, url): def _real_extract(self, url):
video_id = self._match_id(url) video_id = self._match_id(url)
post = self._download_json( post = self._download_json(
'https://www.patreon.com/api/posts/' + video_id, video_id) 'https://www.patreon.com/api/posts/' + video_id, video_id, query={
'fields[media]': 'download_url,mimetype,size_bytes',
'fields[post]': 'comment_count,content,embed,image,like_count,post_file,published_at,title',
'fields[user]': 'full_name,url',
'json-api-use-default-includes': 'false',
'include': 'media,user',
})
attributes = post['data']['attributes'] attributes = post['data']['attributes']
title = attributes['title'].strip() title = attributes['title'].strip()
image = attributes.get('image') or {} image = attributes.get('image') or {}
@ -104,33 +115,42 @@ class PatreonIE(InfoExtractor):
'comment_count': int_or_none(attributes.get('comment_count')), 'comment_count': int_or_none(attributes.get('comment_count')),
} }
def add_file(file_data):
file_url = file_data.get('url')
if file_url:
info.update({
'url': file_url,
'ext': determine_ext(file_data.get('name'), 'mp3'),
})
for i in post.get('included', []): for i in post.get('included', []):
i_type = i.get('type') i_type = i.get('type')
if i_type == 'attachment': if i_type == 'media':
add_file(i.get('attributes') or {}) media_attributes = i.get('attributes') or {}
download_url = media_attributes.get('download_url')
ext = mimetype2ext(media_attributes.get('mimetype'))
if download_url and ext in KNOWN_EXTENSIONS:
info.update({
'ext': ext,
'filesize': int_or_none(media_attributes.get('size_bytes')),
'url': download_url,
})
elif i_type == 'user': elif i_type == 'user':
user_attributes = i.get('attributes') user_attributes = i.get('attributes')
if user_attributes: if user_attributes:
info.update({ info.update({
'uploader': user_attributes.get('full_name'), 'uploader': user_attributes.get('full_name'),
'uploader_id': str_or_none(i.get('id')),
'uploader_url': user_attributes.get('url'), 'uploader_url': user_attributes.get('url'),
}) })
if not info.get('url'): if not info.get('url'):
add_file(attributes.get('post_file') or {}) embed_url = try_get(attributes, lambda x: x['embed']['url'])
if embed_url:
info.update({
'_type': 'url',
'url': embed_url,
})
if not info.get('url'): if not info.get('url'):
info.update({ post_file = attributes['post_file']
'_type': 'url', ext = determine_ext(post_file.get('name'))
'url': attributes['embed']['url'], if ext in KNOWN_EXTENSIONS:
}) info.update({
'ext': ext,
'url': post_file['url'],
})
return info return info