diff --git a/prismedia/yt_upload.py b/prismedia/yt_upload.py index e8809c5..e0f3aee 100644 --- a/prismedia/yt_upload.py +++ b/prismedia/yt_upload.py @@ -15,6 +15,7 @@ import datetime import pytz import logging from tzlocal import get_localzone +from clint.textui.progress import Bar as ProgressBar from googleapiclient.discovery import build from googleapiclient.errors import HttpError @@ -137,7 +138,7 @@ def initialize_upload(youtube, options): } } - # If peertubeAt exists, use instead of publishAt + # If youtubeAt exists, use instead of publishAt if options.get('--youtubeAt'): publishAt = options.get('--youtubeAt') elif options.get('--publishAt'): @@ -168,7 +169,7 @@ def initialize_upload(youtube, options): insert_request = youtube.videos().insert( part=','.join(list(body.keys())), body=body, - media_body=MediaFileUpload(path, chunksize=-1, resumable=True) + media_body=MediaFileUpload(path, chunksize=-1, resumable=True) # 256 * 1024 * 1024 ) video_id = resumable_upload(insert_request, 'video', 'insert', options) @@ -295,13 +296,26 @@ def resumable_upload(request, resource, method, options): error = None retry = 0 logger_stdout = None + if options.get('--url-only') or options.get('--batch'): logger_stdout = logging.getLogger('stdoutlogs') + + template = 'Youtube: Uploading %s...' + logger.info(template % resource) + + # progress_callback = create_callback(100, "percentage") # options.get('--progress')) + while response is None: try: - template = 'Youtube: Uploading %s...' - logger.info(template % resource) status, response = request.next_chunk() + + # https://googleapis.github.io/google-api-python-client/docs/media.html + if status: + # progress_callback(int(status.progress() * 100.0)) + print("Uploaded %d%%." % int(status.progress() * 100)) + else: + print("No status but a loop has been done") + if response is not None: if method == 'insert' and 'id' in response: logger.info('Youtube: Video was successfully uploaded.') @@ -342,6 +356,41 @@ def resumable_upload(request, resource, method, options): time.sleep(sleep_seconds) +# upload_finished = False +def create_callback(filesize, progress_type): + upload_size_MB = filesize * (1 / (1024 * 1024)) + + if progress_type is None or "percentage" in progress_type.lower(): + progress_lambda = lambda x: int((x / filesize) * 100) # Default to percentage + elif "bigfile" in progress_type.lower(): + progress_lambda = lambda x: x * (1 / (1024 * 1024)) # MB + elif "accurate" in progress_type.lower(): + progress_lambda = lambda x: x * (1 / (1024)) # kB + else: + # Should not happen outside of development when adding partly a progress type + logger.critical("Youtube: Unknown progress type `" + progress_type + "`") + exit(1) + + bar = ProgressBar(expected_size=progress_lambda(filesize), label=f"Youtube upload progress ({upload_size_MB:.2f}MB) ", filled_char='=') + + def callback(current_position): + # We want the condition to capture the varible from the parent scope, not a local variable that is created after + # global upload_finished + progress = progress_lambda(current_position) + + bar.show(progress) + + if current_position == filesize: + # if not upload_finished: + # # We get two time in the callback with both bytes equals, skip the first + # upload_finished = True + # else: + # Print a blank line to not (partly) override the progress bar + print() + logger.info("Youtube: Upload finish, Processing…") + + return callback + def hearthbeat(): """Use the minimums credits possibles of the API so google does not readuce to 0 the allowed credits. This apparently happens after 90 days without any usage of credits.