From 0ddbca711cc0ba3a0c367f36bc7994f32fd2ff42 Mon Sep 17 00:00:00 2001 From: LecygneNoir Date: Sun, 6 May 2018 20:53:30 +0200 Subject: [PATCH] add verbose logging with date --- lib/pt_upload.py | 20 ++++++++++++-------- lib/utils.py | 38 +++++++++++++++++++++++--------------- lib/yt_upload.py | 22 ++++++++++++++-------- nfo_example.txt | 3 ++- prismedia_upload.py | 18 +++++++++++------- 5 files changed, 62 insertions(+), 39 deletions(-) diff --git a/lib/pt_upload.py b/lib/pt_upload.py index 968b9ae..997a50f 100644 --- a/lib/pt_upload.py +++ b/lib/pt_upload.py @@ -4,6 +4,7 @@ import os import mimetypes import json +import logging from os.path import splitext, basename, abspath from ConfigParser import RawConfigParser @@ -75,7 +76,8 @@ def upload_video(oauth, secret, options): continue # Tag more than 30 chars crashes Peertube, so exit and check tags if len(strtag) >= 30: - exit("Sorry, Peertube does not support tag with more than 30 characters, please reduce your tag size") + logging.warning("Sorry, Peertube does not support tag with more than 30 characters, please reduce your tag size") + exit(1) # If Mastodon compatibility is enabled, clean tags from special characters if options.get('--mt'): strtag = utils.mastodonTag(strtag) @@ -120,10 +122,11 @@ def upload_video(oauth, secret, options): idvideo = str(jresponse['id']) template = ('Peertube : Video was successfully uploaded.\n' 'Watch it at %s/videos/watch/%s.') - print(template % (url, uuid)) + logging.info(template % (url, uuid)) else: - exit(('Peertube : The upload failed with an unexpected response: ' - '%s') % response) + logging.error(('Peertube : The upload failed with an unexpected response: ' + '%s') % response) + exit(1) if options.get('--publishAt'): utils.publishAt(str(options.get('--publishAt')), oauth, url, idvideo) @@ -134,15 +137,16 @@ def run(options): try: secret.read(PEERTUBE_SECRETS_FILE) except Exception as e: - exit("Error loading " + str(PEERTUBE_SECRETS_FILE) + ": " + str(e)) + logging.error("Error loading " + str(PEERTUBE_SECRETS_FILE) + ": " + str(e)) + exit(1) insecure_transport = secret.get('peertube', 'OAUTHLIB_INSECURE_TRANSPORT') os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = insecure_transport oauth = get_authenticated_service(secret) try: - print('Peertube : Uploading file...') + logging.info('Peertube : Uploading file...') upload_video(oauth, secret, options) except Exception as e: if hasattr(e, 'message'): - print("Error: " + str(e.message)) + logging.error("Error: " + str(e.message)) else: - print("Error: " + str(e)) + logging.error("Error: " + str(e)) diff --git a/lib/utils.py b/lib/utils.py index eac5568..18f7f3e 100644 --- a/lib/utils.py +++ b/lib/utils.py @@ -6,6 +6,7 @@ from os.path import dirname, splitext, basename, isfile from os import devnull from subprocess import check_call, CalledProcessError, STDOUT import unicodedata +import logging ### CATEGORIES ### YOUTUBE_CATEGORY = { @@ -102,40 +103,44 @@ def loadNFO(options): video_directory = dirname(options.get('--file')) + "/" if options.get('--nfo'): try: - print "Using " + options.get('--nfo') + " as NFO, loading..." + logging.info("Using " + options.get('--nfo') + " as NFO, loading...") if isfile(options.get('--nfo')): nfo = RawConfigParser() nfo.read(options.get('--nfo')) return nfo else: - exit("Given NFO file does not exist, please check your path.") + logging.error("Given NFO file does not exist, please check your path.") + exit(1) except Exception as e: - exit("Problem with NFO file: " + str(e)) + logging.error("Problem with NFO file: " + str(e)) + exit(1) else: if options.get('--name'): nfo_file = video_directory + options.get('--name') + ".txt" print nfo_file if isfile(nfo_file): try: - print "Using " + nfo_file + " as NFO, loading..." + logging.info("Using " + nfo_file + " as NFO, loading...") nfo = RawConfigParser() nfo.read(nfo_file) return nfo except Exception as e: - exit("Problem with NFO file: " + str(e)) + logging.error("Problem with NFO file: " + str(e)) + exit(1) # if --nfo and --name does not exist, use --file as default video_file = splitext(basename(options.get('--file')))[0] nfo_file = video_directory + video_file + ".txt" if isfile(nfo_file): try: - print "Using " + nfo_file + " as NFO, loading..." + logging.info("Using " + nfo_file + " as NFO, loading...") nfo = RawConfigParser() nfo.read(nfo_file) return nfo except Exception as e: - exit("Problem with nfo file: " + str(e)) - print "No suitable NFO found, skipping." + logging.error("Problem with nfo file: " + str(e)) + exit(1) + logging.info("No suitable NFO found, skipping.") return False @@ -155,7 +160,8 @@ def parseNFO(options): except NoOptionError: continue except NoSectionError: - exit("Given NFO file miss section [video], please check syntax of your NFO.") + logging.error("Given NFO file miss section [video], please check syntax of your NFO.") + exit(1) return options @@ -168,12 +174,14 @@ def publishAt(publishAt, oauth, url, idvideo): FNULL = open(devnull, 'w') check_call(["at", "-V"], stdout=FNULL, stderr=STDOUT) except CalledProcessError: - exit("You need to install the atd daemon to use the publishAt option.") + logging.error("You need to install the atd daemon to use the publishAt option.") + exit(1) try: FNULL = open(devnull, 'w') check_call(["curl", "-V"], stdout=FNULL, stderr=STDOUT) except CalledProcessError: - exit("You need to install the curl command line to use the publishAt option.") + logging.error("You need to install the curl command line to use the publishAt option.") + exit(1) time = publishAt.split("T") # Remove leading seconds that atd does not manage if time[1].count(":") == 2: @@ -189,18 +197,18 @@ def publishAt(publishAt, oauth, url, idvideo): file.close() except Exception as e: if hasattr(e, 'message'): - print("Error: " + str(e.message)) + logging.error("Error: " + str(e.message)) else: - print("Error: " + str(e)) + logging.error("Error: " + str(e)) try: FNULL = open(devnull, 'w') check_call(["at", "-M", "-f", atFile, atTime], stdout=FNULL, stderr=STDOUT) except Exception as e: if hasattr(e, 'message'): - print("Error: " + str(e.message)) + logging.error("Error: " + str(e.message)) else: - print("Error: " + str(e)) + logging.error("Error: " + str(e)) def mastodonTag(tag): diff --git a/lib/yt_upload.py b/lib/yt_upload.py index 2bc3488..28e9c6f 100644 --- a/lib/yt_upload.py +++ b/lib/yt_upload.py @@ -12,6 +12,7 @@ from os.path import splitext, basename, exists import google.oauth2.credentials import datetime import pytz +import logging from tzlocal import get_localzone from googleapiclient.discovery import build @@ -21,6 +22,9 @@ from google_auth_oauthlib.flow import InstalledAppFlow import utils +logging.basicConfig(format='%(asctime)s %(message)s', level=logging.INFO) + + # Explicitly tell the underlying HTTP transport library not to retry, since # we are handling retry logic ourselves. httplib2.RETRIES = 1 @@ -71,7 +75,7 @@ def get_authenticated_service(): p = copy.deepcopy(vars(credentials)) del p["expiry"] json.dump(p, f) - return build(API_SERVICE_NAME, API_VERSION, credentials=credentials) + return build(API_SERVICE_NAME, API_VERSION, credentials=credentials, cache_discovery=False) def initialize_upload(youtube, options): @@ -133,18 +137,19 @@ def resumable_upload(request): retry = 0 while response is None: try: - print('Youtube : Uploading file...') + logging.info('Youtube : Uploading file...') status, response = request.next_chunk() if response is not None: if 'id' in response: template = ('Youtube : Video was successfully ' 'uploaded.\n' 'Watch it at https://youtu.be/%s (post-encoding could take some time)') - print(template % response['id']) + logging.info(template % response['id']) else: template = ('Youtube : The upload failed with an ' 'unexpected response: %s') - exit(template % response) + logging.error(template % response) + exit(1) except HttpError as e: if e.resp.status in RETRIABLE_STATUS_CODES: template = 'Youtube : A retriable HTTP error %d occurred:\n%s' @@ -155,14 +160,15 @@ def resumable_upload(request): error = 'Youtube : A retriable error occurred: %s' % e if error is not None: - print(error) + logging.warning(error) retry += 1 if retry > MAX_RETRIES: - exit('Youtube : No longer attempting to retry.') + logging.error('Youtube : No longer attempting to retry.') + exit(1) max_sleep = 2 ** retry sleep_seconds = random.random() * max_sleep - print('Youtube : Sleeping %f seconds and then retrying...' + logging.warning('Youtube : Sleeping %f seconds and then retrying...' % sleep_seconds) time.sleep(sleep_seconds) @@ -172,5 +178,5 @@ def run(options): try: initialize_upload(youtube, options) except HttpError as e: - print('Youtube : An HTTP error %d occurred:\n%s' % (e.resp.status, + logging.error('Youtube : An HTTP error %d occurred:\n%s' % (e.resp.status, e.content)) diff --git a/nfo_example.txt b/nfo_example.txt index 6de918f..f4ed7ef 100644 --- a/nfo_example.txt +++ b/nfo_example.txt @@ -16,4 +16,5 @@ privacy = private disable-comments = True nsfw = True platform = youtube, peertube -language = French \ No newline at end of file +language = French +publishAt=2018-05-07T19:00:00 \ No newline at end of file diff --git a/prismedia_upload.py b/prismedia_upload.py index 84519b9..7261cbf 100755 --- a/prismedia_upload.py +++ b/prismedia_upload.py @@ -56,6 +56,8 @@ Languages: from os.path import dirname, realpath import sys import datetime +import logging +logging.basicConfig(format='%(asctime)s %(message)s', level=logging.INFO) from docopt import docopt @@ -71,18 +73,20 @@ try: # noinspection PyUnresolvedReferences from schema import Schema, And, Or, Optional, SchemaError except ImportError: - exit('This program requires that the `schema` data-validation library' - ' is installed: \n' - 'see https://github.com/halst/schema\n') + logging.error('This program requires that the `schema` data-validation library' + ' is installed: \n' + 'see https://github.com/halst/schema\n') + exit(1) try: # noinspection PyUnresolvedReferences import magic except ImportError: - exit('This program requires that the `python-magic` library' - ' is installed, NOT the Python bindings to libmagic API \n' - 'see https://github.com/ahupp/python-magic\n') + logging.error('This program requires that the `python-magic` library' + ' is installed, NOT the Python bindings to libmagic API \n' + 'see https://github.com/ahupp/python-magic\n') + exit(1) -VERSION = "prismedia v0.3" +VERSION = "prismedia v0.4" VALID_PRIVACY_STATUSES = ('public', 'private', 'unlisted') VALID_CATEGORIES = (