mirror of
https://git.lecygnenoir.info/LecygneNoir/prismedia.git
synced 2025-10-03 17:39:16 +02:00
Add thumbnail support for youtube
This commit is contained in:
parent
b442f15b17
commit
4803069178
1 changed files with 27 additions and 7 deletions
|
@ -20,6 +20,7 @@ from googleapiclient.errors import HttpError
|
|||
from googleapiclient.http import MediaFileUpload
|
||||
from google_auth_oauthlib.flow import InstalledAppFlow
|
||||
|
||||
|
||||
import utils
|
||||
|
||||
logging.basicConfig(format='%(asctime)s %(message)s', level=logging.INFO)
|
||||
|
@ -126,25 +127,44 @@ def initialize_upload(youtube, options):
|
|||
body=body,
|
||||
media_body=MediaFileUpload(path, chunksize=-1, resumable=True)
|
||||
)
|
||||
resumable_upload(insert_request)
|
||||
video_id = resumable_upload(insert_request, 'video', 'insert')
|
||||
|
||||
# If we get a video_id, upload is successful and we are able to set thumbnail
|
||||
if video_id and options.get('--thumbnail'):
|
||||
set_thumbnail(youtube, options.get('--thumbnail'), videoId=video_id)
|
||||
|
||||
|
||||
def set_thumbnail(youtube, media_file, **kwargs):
|
||||
kwargs = utils.remove_empty_kwargs(**kwargs) # See full sample for function
|
||||
request = youtube.thumbnails().set(
|
||||
media_body=MediaFileUpload(media_file, chunksize=-1,
|
||||
resumable=True),
|
||||
**kwargs
|
||||
)
|
||||
|
||||
# See full sample for function
|
||||
return resumable_upload(request, 'thumbnail', 'set')
|
||||
|
||||
|
||||
# This method implements an exponential backoff strategy to resume a
|
||||
# failed upload.
|
||||
def resumable_upload(request):
|
||||
def resumable_upload(request, resource, method):
|
||||
response = None
|
||||
error = None
|
||||
retry = 0
|
||||
while response is None:
|
||||
try:
|
||||
logging.info('Youtube : Uploading file...')
|
||||
template = 'Youtube: Uploading %s...'
|
||||
logging.info(template % resource)
|
||||
status, response = request.next_chunk()
|
||||
if response is not None:
|
||||
if 'id' in response:
|
||||
template = ('Youtube : Video was successfully '
|
||||
'uploaded.\n'
|
||||
'Watch it at https://youtu.be/%s (post-encoding could take some time)')
|
||||
if method == 'insert' and 'id' in response:
|
||||
logging.info('Youtube : Video was successfully uploaded.')
|
||||
template = 'Youtube: Watch it at https://youtu.be/%s (post-encoding could take some time)'
|
||||
logging.info(template % response['id'])
|
||||
return response['id']
|
||||
elif method != 'insert' or "id" not in response:
|
||||
logging.info('Youtube: Thumbnail was successfully set.')
|
||||
else:
|
||||
template = ('Youtube : The upload failed with an '
|
||||
'unexpected response: %s')
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue