Browse Source

add verbose logging with date

pull/18/head 1.0.0-beta3
LecygneNoir 5 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 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))

+ 23
- 15
lib/utils.py View File

@ -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):

+ 14
- 8
lib/yt_upload.py View File

@ -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))

+ 2
- 1
nfo_example.txt View File

@ -16,4 +16,5 @@ privacy = private
disable-comments = True
nsfw = True
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
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 = (

Loading…
Cancel
Save