From b895a4543ecef4fb7f4fd027aa10bd79c5e2893f Mon Sep 17 00:00:00 2001 From: Zykino Date: Thu, 3 Mar 2022 16:25:49 +0100 Subject: [PATCH] Add progress for peertube plugin --- prismedia/plugins/platforms/peertube.py | 79 +++++++++++++------------ 1 file changed, 40 insertions(+), 39 deletions(-) diff --git a/prismedia/plugins/platforms/peertube.py b/prismedia/plugins/platforms/peertube.py index 05a2913..81b45c8 100644 --- a/prismedia/plugins/platforms/peertube.py +++ b/prismedia/plugins/platforms/peertube.py @@ -20,6 +20,7 @@ from clint.textui.progress import Bar as ProgressBar from yapsy.PluginManager import PluginManagerSingleton logger = logging.getLogger('Prismedia') +upload_finished = False class Peertube(pi.IPlatformPlugin): @@ -327,11 +328,11 @@ class Peertube(pi.IPlatformPlugin): exit(1) encoder = MultipartEncoder(fields) - # if options.get('--quiet'): - multipart_data = encoder - # else: - # progress_callback = create_callback(encoder, options.get('--progress')) - # multipart_data = MultipartEncoderMonitor(encoder, progress_callback) + if options.get('--quiet'): + multipart_data = encoder + else: + progress_callback = self.create_callback(encoder, options.get('--progress')) + multipart_data = MultipartEncoderMonitor(encoder, progress_callback) headers = { 'Content-Type': multipart_data.content_type @@ -359,40 +360,40 @@ class Peertube(pi.IPlatformPlugin): '%s') % response) exit(1) - # upload_finished = False - # def create_callback(encoder, progress_type): - # upload_size_MB = encoder.len * (1 / (1024 * 1024)) - # - # if progress_type is None or "percentage" in progress_type.lower(): - # progress_lambda = lambda x: int((x / encoder.len) * 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("Peertube: Unknown progress type `" + progress_type + "`") - # exit(1) - # - # bar = ProgressBar(expected_size=progress_lambda(encoder.len), label=f"Peertube upload progress ({upload_size_MB:.2f}MB) ", filled_char='=') - # - # def callback(monitor): - # # 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(monitor.bytes_read) - # - # bar.show(progress) - # - # if monitor.bytes_read == encoder.len: - # 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("Peertube: Upload finish, Processing…") - # - # return callback + + def create_callback(self, encoder, progress_type): + upload_size_MB = encoder.len * (1 / (1024 * 1024)) + + if progress_type is None or "percentage" in progress_type.lower(): + progress_lambda = lambda x: int((x / encoder.len) * 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("Peertube: Unknown progress type `" + progress_type + "`") + exit(1) + + bar = ProgressBar(expected_size=progress_lambda(encoder.len), label=f"Peertube upload progress ({upload_size_MB:.2f}MB) ", filled_char='=') + + def callback(monitor): + # 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(monitor.bytes_read) + + bar.show(progress) + + if monitor.bytes_read == encoder.len: + 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("Peertube: Upload finish, Processing…") + + return callback def heartbeat(self): """