From 379aef1dd8be0db58ab23329603a830edf30f417 Mon Sep 17 00:00:00 2001 From: LecygneNoir Date: Mon, 14 Sep 2020 14:19:59 +0200 Subject: [PATCH] Following discussion in #29, rename print-url option and add a batch options --- README.md | 32 ++++++++++++++++++++++++++++++-- prismedia/pt_upload.py | 9 ++++++--- prismedia/upload.py | 24 ++++++++++++++++-------- prismedia/yt_upload.py | 17 +++++++++-------- 4 files changed, 61 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 23c9319..f20ea67 100644 --- a/README.md +++ b/README.md @@ -122,9 +122,8 @@ Use --help to get all available options: ``` Options: - -f, --file=STRING Path to the video file to upload in mp4 + -f, --file=STRING Path to the video file to upload in mp4. This is the only mandatory option. --name=NAME Name of the video to upload. (default to video filename) - --debug Trigger some debug information like options used (default: no) -d, --description=STRING Description of the video. (default: default description) -t, --tags=STRING Tags for the video. comma separated. WARN: tags with punctuation (!, ', ", ?, ...) @@ -160,6 +159,34 @@ Options: -h --help Show this help. --version Show version. +Logging options + -q --quiet Suppress any log except Critical (alias for --log=critical). + --log=STRING Log level, between debug, info, warning, error, critical. Ignored if --quiet is set (default to info) + -u --url-only Display generated URL after upload directly on stdout, implies --quiet + --batch Display generated URL after upload with platform information for easier parsing. Implies --quiet + Be careful --batch and --url-only are mutually exclusives. + --debug (Deprecated) Alias for --log=debug. Ignored if --log is set + +Strict options: + Strict options allow you to force some option to be present when uploading a video. It's useful to be sure you do not + forget something when uploading a video, for example if you use multiples NFO. You may force the presence of description, + tags, thumbnail, ... + All strict option are optionals and are provided only to avoid errors when uploading :-) + All strict options can be specified in NFO directly, the only strict option mandatory on cli is --withNFO + All strict options are off by default + + --withNFO Prevent the upload without a NFO, either specified via cli or found in the directory + --withThumbnail Prevent the upload without a thumbnail + --withName Prevent the upload if no name are found + --withDescription Prevent the upload without description + --withTags Prevent the upload without tags + --withPlaylist Prevent the upload if no playlist + --withPublishAt Prevent the upload if no schedule + --withPlatform Prevent the upload if at least one platform is not specified + --withCategory Prevent the upload if no category + --withLanguage Prevent upload if no language + --withChannel Prevent upload if no channel + Categories: Category is the type of video you upload. Default is films. Here are available categories from Peertube and Youtube: @@ -174,6 +201,7 @@ Languages: Here are available languages from Peertube and Youtube: Arabic, English, French, German, Hindi, Italian, Japanese, Korean, Mandarin, Portuguese, Punjabi, Russian, Spanish + ``` ## Enhanced use of NFO diff --git a/prismedia/pt_upload.py b/prismedia/pt_upload.py index 3e0eec7..b95e550 100644 --- a/prismedia/pt_upload.py +++ b/prismedia/pt_upload.py @@ -5,6 +5,7 @@ import os import mimetypes import json import logging +import sys import datetime import pytz from os.path import splitext, basename, abspath @@ -275,7 +276,7 @@ def upload_video(oauth, secret, options): exit(1) logger_stdout = None - if options.get('--print-url'): + if options.get('--url-only') or options.get('--batch'): logger_stdout = logging.getLogger('stdoutlogs') multipart_data = MultipartEncoder(fields) @@ -295,9 +296,11 @@ def upload_video(oauth, secret, options): logger.info('Peertube : Video was successfully uploaded.') template = 'Peertube: Watch it at %s/videos/watch/%s.' logger.info(template % (url, uuid)) - if logger_stdout: - template_stdout = '%s/videos/watch/%s' + template_stdout = '%s/videos/watch/%s' + if options.get('--url-only'): logger_stdout.info(template_stdout % (url, uuid)) + elif options.get('--batch'): + logger_stdout.info("Peertube: " + template_stdout % (url, uuid)) # Upload is successful we may set playlist if options.get('--playlist'): set_playlist(oauth, url, video_id, playlist_id) diff --git a/prismedia/upload.py b/prismedia/upload.py index 82d3aff..d91cafa 100755 --- a/prismedia/upload.py +++ b/prismedia/upload.py @@ -51,8 +51,9 @@ Options: Logging options -q --quiet Suppress any log except Critical (alias for --log=critical). --log=STRING Log level, between debug, info, warning, error, critical. Ignored if --quiet is set (default to info) - -u --print-url Display generated URL after upload directly on stdout in addition to the usual logging. - May be used in conjunction with --quiet for batch scripting + -u --url-only Display generated URL after upload directly on stdout, implies --quiet + --batch Display generated URL after upload with platform information for easier parsing. Implies --quiet + Be careful --batch and --url-only are mutually exclusives. --debug (Deprecated) Alias for --log=debug. Ignored if --log is set Strict options: @@ -226,6 +227,13 @@ def _optionnalOrStrict(key, scope, error): def configureLogs(options): + if options.get('--batch') and options.get('--url-only'): + logger.critical("Prismedia: Please use either --batch OR --url-only, not both.") + exit(1) + # batch and url-only implies quiet + if options.get('--batch') or options.get('--url-only'): + options['--quiet'] = True + if options.get('--quiet'): # We need to set both log level in the same time logger.setLevel(50) @@ -246,11 +254,11 @@ def configureStdoutLogs(): logger_stdout.setLevel(logging.INFO) ch_stdout = logging.StreamHandler(stream=sys.stdout) ch_stdout.setLevel(logging.INFO) + # Default stdout logs is url only formatter_stdout = logging.Formatter('%(message)s') ch_stdout.setFormatter(formatter_stdout) logger_stdout.addHandler(ch_stdout) - def main(): options = docopt(__doc__, version=VERSION) @@ -263,6 +271,8 @@ def main(): ), Optional('--quiet', default=False): bool, Optional('--debug'): bool, + Optional('--url-only', default=False): bool, + Optional('--batch', default=False): bool, Optional('--withNFO', default=False): bool, Optional('--withThumbnail', default=False): bool, Optional('--withName', default=False): bool, @@ -349,7 +359,6 @@ def main(): Optional('--channelCreate'): bool, Optional('--playlist'): Or(None, str), Optional('--playlistCreate'): bool, - Optional('--print-url', default=False): bool, '--help': bool, '--version': bool, # This allow to return all other options for further use: https://github.com/keleshev/schema#extra-keys @@ -363,6 +372,9 @@ def main(): logger.critical(e) exit(1) + if options.get('--url-only') or options.get('--batch'): + configureStdoutLogs() + options = utils.parseNFO(options) # Once NFO are loaded, we need to revalidate strict options in case some were in NFO @@ -381,9 +393,6 @@ def main(): logger.critical(e) exit(1) - if options.get('--print-url'): - configureStdoutLogs() - logger.debug("Python " + sys.version) logger.debug(options) @@ -394,6 +403,5 @@ def main(): if __name__ == '__main__': - import warnings logger.warning("DEPRECATION: use 'python -m prismedia', not 'python -m prismedia.upload'") main() diff --git a/prismedia/yt_upload.py b/prismedia/yt_upload.py index 1c70b62..5ed432d 100644 --- a/prismedia/yt_upload.py +++ b/prismedia/yt_upload.py @@ -148,17 +148,13 @@ def initialize_upload(youtube, options): else: playlist_id = "" - logger_stdout = None - if options.get('--print-url'): - logger_stdout = logging.getLogger('stdoutlogs') - # Call the API's videos.insert method to create and upload the video. insert_request = youtube.videos().insert( part=','.join(list(body.keys())), body=body, media_body=MediaFileUpload(path, chunksize=-1, resumable=True) ) - video_id = resumable_upload(insert_request, 'video', 'insert', logger_stdout) + video_id = resumable_upload(insert_request, 'video', 'insert', options) # If we get a video_id, upload is successful and we are able to set thumbnail if video_id and options.get('--thumbnail'): @@ -269,10 +265,13 @@ def set_playlist(youtube, playlist_id, video_id): # This method implements an exponential backoff strategy to resume a # failed upload. -def resumable_upload(request, resource, method, logger_stdout=None): +def resumable_upload(request, resource, method, options): response = None error = None retry = 0 + logger_stdout = None + if options.get('--url-only') or options.get('--batch'): + logger_stdout = logging.getLogger('stdoutlogs') while response is None: try: template = 'Youtube: Uploading %s...' @@ -283,9 +282,11 @@ def resumable_upload(request, resource, method, logger_stdout=None): logger.info('Youtube : Video was successfully uploaded.') template = 'Youtube: Watch it at https://youtu.be/%s (post-encoding could take some time)' logger.info(template % response['id']) - if logger_stdout: - template_stdout = 'https://youtu.be/%s' + template_stdout = 'https://youtu.be/%s' + if options.get('--url-only'): logger_stdout.info(template_stdout % response['id']) + elif options.get('--batch'): + logger_stdout.info("Youtube: " + template_stdout % response['id']) return response['id'] elif method != 'insert' or "id" not in response: logger.info('Youtube: Thumbnail was successfully set.')