Browse Source

add verbose logging with date

pull/18/head 1.0.0-beta3
LecygneNoir 6 years ago
parent
commit
0ddbca711c
5 changed files with 62 additions and 39 deletions
  1. +12
    -8
      lib/pt_upload.py
  2. +23
    -15
      lib/utils.py
  3. +14
    -8
      lib/yt_upload.py
  4. +2
    -1
      nfo_example.txt
  5. +11
    -7
      prismedia_upload.py

+ 12
- 8
lib/pt_upload.py View File

@ -4,6 +4,7 @@
import os import os
import mimetypes import mimetypes
import json import json
import logging
from os.path import splitext, basename, abspath from os.path import splitext, basename, abspath
from ConfigParser import RawConfigParser from ConfigParser import RawConfigParser
@ -75,7 +76,8 @@ def upload_video(oauth, secret, options):
continue continue
# Tag more than 30 chars crashes Peertube, so exit and check tags # Tag more than 30 chars crashes Peertube, so exit and check tags
if len(strtag) >= 30: 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 Mastodon compatibility is enabled, clean tags from special characters
if options.get('--mt'): if options.get('--mt'):
strtag = utils.mastodonTag(strtag) strtag = utils.mastodonTag(strtag)
@ -120,10 +122,11 @@ def upload_video(oauth, secret, options):
idvideo = str(jresponse['id']) idvideo = str(jresponse['id'])
template = ('Peertube : Video was successfully uploaded.\n' template = ('Peertube : Video was successfully uploaded.\n'
'Watch it at %s/videos/watch/%s.') 'Watch it at %s/videos/watch/%s.')
print(template % (url, uuid))
logging.info(template % (url, uuid))
else: 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'): if options.get('--publishAt'):
utils.publishAt(str(options.get('--publishAt')), oauth, url, idvideo) utils.publishAt(str(options.get('--publishAt')), oauth, url, idvideo)
@ -134,15 +137,16 @@ def run(options):
try: try:
secret.read(PEERTUBE_SECRETS_FILE) secret.read(PEERTUBE_SECRETS_FILE)
except Exception as e: 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') insecure_transport = secret.get('peertube', 'OAUTHLIB_INSECURE_TRANSPORT')
os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = insecure_transport os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = insecure_transport
oauth = get_authenticated_service(secret) oauth = get_authenticated_service(secret)
try: try:
print('Peertube : Uploading file...')
logging.info('Peertube : Uploading file...')
upload_video(oauth, secret, options) upload_video(oauth, secret, options)
except Exception as e: except Exception as e:
if hasattr(e, 'message'): if hasattr(e, 'message'):
print("Error: " + str(e.message))
logging.error("Error: " + str(e.message))
else: else:
print("Error: " + str(e))
logging.error("Error: " + str(e))

+ 23
- 15
lib/utils.py View File

@ -6,6 +6,7 @@ from os.path import dirname, splitext, basename, isfile
from os import devnull from os import devnull
from subprocess import check_call, CalledProcessError, STDOUT from subprocess import check_call, CalledProcessError, STDOUT
import unicodedata import unicodedata
import logging
### CATEGORIES ### ### CATEGORIES ###
YOUTUBE_CATEGORY = { YOUTUBE_CATEGORY = {
@ -102,40 +103,44 @@ def loadNFO(options):
video_directory = dirname(options.get('--file')) + "/" video_directory = dirname(options.get('--file')) + "/"
if options.get('--nfo'): if options.get('--nfo'):
try: try:
print "Using " + options.get('--nfo') + " as NFO, loading..."
logging.info("Using " + options.get('--nfo') + " as NFO, loading...")
if isfile(options.get('--nfo')): if isfile(options.get('--nfo')):
nfo = RawConfigParser() nfo = RawConfigParser()
nfo.read(options.get('--nfo')) nfo.read(options.get('--nfo'))
return nfo return nfo
else: 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: except Exception as e:
exit("Problem with NFO file: " + str(e))
logging.error("Problem with NFO file: " + str(e))
exit(1)
else: else:
if options.get('--name'): if options.get('--name'):
nfo_file = video_directory + options.get('--name') + ".txt" nfo_file = video_directory + options.get('--name') + ".txt"
print nfo_file print nfo_file
if isfile(nfo_file): if isfile(nfo_file):
try: try:
print "Using " + nfo_file + " as NFO, loading..."
logging.info("Using " + nfo_file + " as NFO, loading...")
nfo = RawConfigParser() nfo = RawConfigParser()
nfo.read(nfo_file) nfo.read(nfo_file)
return nfo return nfo
except Exception as e: 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 # if --nfo and --name does not exist, use --file as default
video_file = splitext(basename(options.get('--file')))[0] video_file = splitext(basename(options.get('--file')))[0]
nfo_file = video_directory + video_file + ".txt" nfo_file = video_directory + video_file + ".txt"
if isfile(nfo_file): if isfile(nfo_file):
try: try:
print "Using " + nfo_file + " as NFO, loading..."
logging.info("Using " + nfo_file + " as NFO, loading...")
nfo = RawConfigParser() nfo = RawConfigParser()
nfo.read(nfo_file) nfo.read(nfo_file)
return nfo return nfo
except Exception as e: 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 return False
@ -155,7 +160,8 @@ def parseNFO(options):
except NoOptionError: except NoOptionError:
continue continue
except NoSectionError: 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 return options
@ -168,12 +174,14 @@ def publishAt(publishAt, oauth, url, idvideo):
FNULL = open(devnull, 'w') FNULL = open(devnull, 'w')
check_call(["at", "-V"], stdout=FNULL, stderr=STDOUT) check_call(["at", "-V"], stdout=FNULL, stderr=STDOUT)
except CalledProcessError: 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: try:
FNULL = open(devnull, 'w') FNULL = open(devnull, 'w')
check_call(["curl", "-V"], stdout=FNULL, stderr=STDOUT) check_call(["curl", "-V"], stdout=FNULL, stderr=STDOUT)
except CalledProcessError: 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") time = publishAt.split("T")
# Remove leading seconds that atd does not manage # Remove leading seconds that atd does not manage
if time[1].count(":") == 2: if time[1].count(":") == 2:
@ -189,18 +197,18 @@ def publishAt(publishAt, oauth, url, idvideo):
file.close() file.close()
except Exception as e: except Exception as e:
if hasattr(e, 'message'): if hasattr(e, 'message'):
print("Error: " + str(e.message))
logging.error("Error: " + str(e.message))
else: else:
print("Error: " + str(e))
logging.error("Error: " + str(e))
try: try:
FNULL = open(devnull, 'w') FNULL = open(devnull, 'w')
check_call(["at", "-M", "-f", atFile, atTime], stdout=FNULL, stderr=STDOUT) check_call(["at", "-M", "-f", atFile, atTime], stdout=FNULL, stderr=STDOUT)
except Exception as e: except Exception as e:
if hasattr(e, 'message'): if hasattr(e, 'message'):
print("Error: " + str(e.message))
logging.error("Error: " + str(e.message))
else: else:
print("Error: " + str(e))
logging.error("Error: " + str(e))
def mastodonTag(tag): def mastodonTag(tag):

+ 14
- 8
lib/yt_upload.py View File

@ -12,6 +12,7 @@ from os.path import splitext, basename, exists
import google.oauth2.credentials import google.oauth2.credentials
import datetime import datetime
import pytz import pytz
import logging
from tzlocal import get_localzone from tzlocal import get_localzone
from googleapiclient.discovery import build from googleapiclient.discovery import build
@ -21,6 +22,9 @@ from google_auth_oauthlib.flow import InstalledAppFlow
import utils import utils
logging.basicConfig(format='%(asctime)s %(message)s', level=logging.INFO)
# Explicitly tell the underlying HTTP transport library not to retry, since # Explicitly tell the underlying HTTP transport library not to retry, since
# we are handling retry logic ourselves. # we are handling retry logic ourselves.
httplib2.RETRIES = 1 httplib2.RETRIES = 1
@ -71,7 +75,7 @@ def get_authenticated_service():
p = copy.deepcopy(vars(credentials)) p = copy.deepcopy(vars(credentials))
del p["expiry"] del p["expiry"]
json.dump(p, f) 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): def initialize_upload(youtube, options):
@ -133,18 +137,19 @@ def resumable_upload(request):
retry = 0 retry = 0
while response is None: while response is None:
try: try:
print('Youtube : Uploading file...')
logging.info('Youtube : Uploading file...')
status, response = request.next_chunk() status, response = request.next_chunk()
if response is not None: if response is not None:
if 'id' in response: if 'id' in response:
template = ('Youtube : Video was successfully ' template = ('Youtube : Video was successfully '
'uploaded.\n' 'uploaded.\n'
'Watch it at https://youtu.be/%s (post-encoding could take some time)') 'Watch it at https://youtu.be/%s (post-encoding could take some time)')
print(template % response['id'])
logging.info(template % response['id'])
else: else:
template = ('Youtube : The upload failed with an ' template = ('Youtube : The upload failed with an '
'unexpected response: %s') 'unexpected response: %s')
exit(template % response)
logging.error(template % response)
exit(1)
except HttpError as e: except HttpError as e:
if e.resp.status in RETRIABLE_STATUS_CODES: if e.resp.status in RETRIABLE_STATUS_CODES:
template = 'Youtube : A retriable HTTP error %d occurred:\n%s' 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 error = 'Youtube : A retriable error occurred: %s' % e
if error is not None: if error is not None:
print(error)
logging.warning(error)
retry += 1 retry += 1
if retry > MAX_RETRIES: 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 max_sleep = 2 ** retry
sleep_seconds = random.random() * max_sleep 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) % sleep_seconds)
time.sleep(sleep_seconds) time.sleep(sleep_seconds)
@ -172,5 +178,5 @@ def run(options):
try: try:
initialize_upload(youtube, options) initialize_upload(youtube, options)
except HttpError as e: 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)) e.content))

+ 2
- 1
nfo_example.txt View File

@ -16,4 +16,5 @@ privacy = private
disable-comments = True disable-comments = True
nsfw = True nsfw = True
platform = youtube, peertube platform = youtube, peertube
language = French
language = French
publishAt=2018-05-07T19:00:00

+ 11
- 7
prismedia_upload.py View File

@ -56,6 +56,8 @@ Languages:
from os.path import dirname, realpath from os.path import dirname, realpath
import sys import sys
import datetime import datetime
import logging
logging.basicConfig(format='%(asctime)s %(message)s', level=logging.INFO)
from docopt import docopt from docopt import docopt
@ -71,18 +73,20 @@ try:
# noinspection PyUnresolvedReferences # noinspection PyUnresolvedReferences
from schema import Schema, And, Or, Optional, SchemaError from schema import Schema, And, Or, Optional, SchemaError
except ImportError: 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: try:
# noinspection PyUnresolvedReferences # noinspection PyUnresolvedReferences
import magic import magic
except ImportError: 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_PRIVACY_STATUSES = ('public', 'private', 'unlisted')
VALID_CATEGORIES = ( VALID_CATEGORIES = (

Loading…
Cancel
Save