mirror of
https://git.lecygnenoir.info/LecygneNoir/prismedia.git
synced 2025-10-04 18:09: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 googleapiclient.http import MediaFileUpload
|
||||||
from google_auth_oauthlib.flow import InstalledAppFlow
|
from google_auth_oauthlib.flow import InstalledAppFlow
|
||||||
|
|
||||||
|
|
||||||
import utils
|
import utils
|
||||||
|
|
||||||
logging.basicConfig(format='%(asctime)s %(message)s', level=logging.INFO)
|
logging.basicConfig(format='%(asctime)s %(message)s', level=logging.INFO)
|
||||||
|
@ -126,25 +127,44 @@ def initialize_upload(youtube, options):
|
||||||
body=body,
|
body=body,
|
||||||
media_body=MediaFileUpload(path, chunksize=-1, resumable=True)
|
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
|
# This method implements an exponential backoff strategy to resume a
|
||||||
# failed upload.
|
# failed upload.
|
||||||
def resumable_upload(request):
|
def resumable_upload(request, resource, method):
|
||||||
response = None
|
response = None
|
||||||
error = None
|
error = None
|
||||||
retry = 0
|
retry = 0
|
||||||
while response is None:
|
while response is None:
|
||||||
try:
|
try:
|
||||||
logging.info('Youtube : Uploading file...')
|
template = 'Youtube: Uploading %s...'
|
||||||
|
logging.info(template % resource)
|
||||||
status, response = request.next_chunk()
|
status, response = request.next_chunk()
|
||||||
if response is not None:
|
if response is not None:
|
||||||
if 'id' in response:
|
if method == 'insert' and 'id' in response:
|
||||||
template = ('Youtube : Video was successfully '
|
logging.info('Youtube : Video was successfully uploaded.')
|
||||||
'uploaded.\n'
|
template = 'Youtube: Watch it at https://youtu.be/%s (post-encoding could take some time)'
|
||||||
'Watch it at https://youtu.be/%s (post-encoding could take some time)')
|
|
||||||
logging.info(template % response['id'])
|
logging.info(template % response['id'])
|
||||||
|
return response['id']
|
||||||
|
elif method != 'insert' or "id" not in response:
|
||||||
|
logging.info('Youtube: Thumbnail was successfully set.')
|
||||||
else:
|
else:
|
||||||
template = ('Youtube : The upload failed with an '
|
template = ('Youtube : The upload failed with an '
|
||||||
'unexpected response: %s')
|
'unexpected response: %s')
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue