Browse Source

Add new options --quiet and --print-url for an easier scripting use of prismedia, cf #29

pull/51/head
LecygneNoir 3 years ago
parent
commit
1c441bf67a
3 changed files with 57 additions and 16 deletions
  1. +7
    -0
      prismedia/pt_upload.py
  2. +41
    -14
      prismedia/upload.py
  3. +9
    -2
      prismedia/yt_upload.py

+ 7
- 0
prismedia/pt_upload.py View File

@ -274,6 +274,10 @@ def upload_video(oauth, secret, options):
" if you want to create it")
exit(1)
logger_stdout = None
if options.get('--print-url'):
logger_stdout = logging.getLogger('stdoutlogs')
multipart_data = MultipartEncoder(fields)
headers = {
@ -291,6 +295,9 @@ 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'
logger_stdout.info(template_stdout % (url, uuid))
# Upload is successful we may set playlist
if options.get('--playlist'):
set_playlist(oauth, url, video_id, playlist_id)

+ 41
- 14
prismedia/upload.py View File

@ -11,7 +11,7 @@ Usage:
prismedia --version
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)
-d, --description=STRING Description of the video. (default: default description)
-t, --tags=STRING Tags for the video. comma separated.
@ -45,11 +45,16 @@ Options:
If the playlist is not found, spawn an error except if --playlistCreate is set.
--playlistCreate Create the playlist if not exists. (default do not create)
Only relevant if --playlist is set.
--log=STRING Log level, between debug, info, warning, error, critical (default to info)
--debug (Deprecated) Alias for --log=DEBUG. Ignored if --log is set
-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 --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
--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,
@ -220,6 +225,32 @@ def _optionnalOrStrict(key, scope, error):
return True
def configureLogs(options):
if options.get('--quiet'):
# We need to set both log level in the same time
logger.setLevel(50)
ch.setLevel(50)
elif options.get('--log'):
numeric_level = getattr(logging, options["--log"], None)
# We need to set both log level in the same time
logger.setLevel(numeric_level)
ch.setLevel(numeric_level)
elif options.get('--debug'):
logger.warning("DEPRECATION: --debug is deprecated, please use --log=debug instead")
logger.setLevel(10)
ch.setLevel(10)
def configureStdoutLogs():
logger_stdout = logging.getLogger('stdoutlogs')
logger_stdout.setLevel(logging.INFO)
ch_stdout = logging.StreamHandler(stream=sys.stdout)
ch_stdout.setLevel(logging.INFO)
formatter_stdout = logging.Formatter('%(message)s')
ch_stdout.setFormatter(formatter_stdout)
logger_stdout.addHandler(ch_stdout)
def main():
options = docopt(__doc__, version=VERSION)
@ -230,6 +261,8 @@ def main():
validateLogLevel,
error="Log level not recognized")
),
Optional('--quiet', default=False): bool,
Optional('--debug'): bool,
Optional('--withNFO', default=False): bool,
Optional('--withThumbnail', default=False): bool,
Optional('--withName', default=False): bool,
@ -306,7 +339,6 @@ def main():
validatePublish,
error="DATE should be the form YYYY-MM-DDThh:mm:ss and has to be in the future")
),
Optional('--debug'): bool,
Optional('--cca'): bool,
Optional('--disable-comments'): bool,
Optional('--nsfw'): bool,
@ -317,6 +349,7 @@ 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
@ -325,20 +358,11 @@ def main():
# We need to validate early options first as withNFO and logs options should be prioritized
try:
options = earlyoptionSchema.validate(options)
configureLogs(options)
except SchemaError as e:
logger.critical(e)
exit(1)
if options.get('--log'):
numeric_level = getattr(logging, options["--log"], None)
# We need to set both log level in the same time
logger.setLevel(numeric_level)
ch.setLevel(numeric_level)
elif options.get('--debug'):
logger.warning("DEPRECATION: --debug is deprecated, please use --log=debug instead")
logger.setLevel(10)
ch.setLevel(10)
options = utils.parseNFO(options)
# Once NFO are loaded, we need to revalidate strict options in case some were in NFO
@ -357,6 +381,9 @@ def main():
logger.critical(e)
exit(1)
if options.get('--print-url'):
configureStdoutLogs()
logger.debug("Python " + sys.version)
logger.debug(options)

+ 9
- 2
prismedia/yt_upload.py View File

@ -148,13 +148,17 @@ 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')
video_id = resumable_upload(insert_request, 'video', 'insert', logger_stdout)
# If we get a video_id, upload is successful and we are able to set thumbnail
if video_id and options.get('--thumbnail'):
@ -265,7 +269,7 @@ 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):
def resumable_upload(request, resource, method, logger_stdout=None):
response = None
error = None
retry = 0
@ -279,6 +283,9 @@ def resumable_upload(request, resource, method):
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'
logger_stdout.info(template_stdout % response['id'])
return response['id']
elif method != 'insert' or "id" not in response:
logger.info('Youtube: Thumbnail was successfully set.')

Loading…
Cancel
Save