diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..72cfa5a --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*.log +miflickup.conf +imgs diff --git a/flickup.conf b/flickup.conf index 18bcaa1..a04c7e7 100644 --- a/flickup.conf +++ b/flickup.conf @@ -1,6 +1,6 @@ [main] -api_key: 5c590366f1af8afdb7190ae6d82a1388 -api_secret: acf7934b3c2a5cd2 +api_key: AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA +api_secret: BBBBBBBBBBBB api_token: 0000000 email_from: from@null.com email_to: to@example.com diff --git a/flickup.py b/flickup.py old mode 100644 new mode 100755 index 4737214..f7a6d0c --- a/flickup.py +++ b/flickup.py @@ -8,69 +8,142 @@ import datetime import sys import configparser +import argparse +import ntpath -import smtplib, os + +import smtplib +import os from email.mime.multipart import MIMEMultipart from email.mime.base import MIMEBase from email.mime.text import MIMEText from email.utils import COMMASPACE, formatdate from email import encoders +from tqdm import tqdm + + +# Default vars +default_args_config = '/etc/flickrup.conf' +default_log_file = 'flickrup.log' +default_uploaded_prefix = '.' + +# Initializing argparser +parser = argparse.ArgumentParser(description='Flickrup: Flicker uploader.') + +# Enable debug +parser.add_argument('-d', + '--debug', + help='Debug output. Default: Disabled', + action='store_true' + ) + +# Path to config file +parser.add_argument('-c', + '--config', + help='Path to config file. Defaul: {}'.format( + default_args_config), + required=False, + default=default_args_config + ) + +# Path to dir with pictures +parser.add_argument('-p', + '--pictures', + help='Path to dir with pictures to upload', + required=False, + default='/etc/flickrup.conf' + ) + +# Path to log file +parser.add_argument('-l', + '--log-file', + help='Path to log file. Default: {}'.format( + default_log_file), + required=False, + default=default_log_file + ) + +# prefix for files created to mark uploaded files +parser.add_argument('-u', + '--uploaded-prefix', + help='File prefix to prevent upload files twice. Default: {}' + .format(default_uploaded_prefix), + required=False, + default=default_uploaded_prefix + ) + + +args = parser.parse_args() + +# Print resume +if args.debug: + print("Running with these config options:") + for arg in vars(args): + print("\t- {}: {}".format(arg, getattr(args, arg))) + -config_file = "/etc/flickrup.conf" config = configparser.ConfigParser() try: - config.read(config_file) - api_key = config.get("main","api_key") - api_secret = config.get("main","api_secret") - api_token = config.get("main","api_token") - email_from = config.get("main","email_from") - email_to = config.get("main","email_to") + config.read(args.config) + api_key = config.get("main", "api_key") + api_secret = config.get("main", "api_secret") + api_token = config.get("main", "api_token") + email_from = config.get("main", "email_from") + email_to = config.get("main", "email_to") except: - print("Missing "+config_file) - sys.exit(0) + print("Missing " + args.config) + sys.exit(0) -done = ".done" +includes = ['*.jpg'] # for files only / case insensitive +# for dirs and files / case insensitive +excludes = [args.log_file] -sdcard = sys.argv[1] + "/" +flickr = flickrapi.FlickrAPI(api_key, api_secret, api_token) -logfile = sdcard + "upload-log.txt" -includes = ['*.jpg'] # for files only / case insensitive -excludes = ["*"+done,logfile] # for dirs and files / case insensitive +def touch(file_name): + with open(file_name, 'w+'): + pass + return True -flickr = flickrapi.FlickrAPI(api_key, api_secret, api_token) -def send_mail( send_from, send_to, subject, text, files=[], server="localhost", port=25, username='', password='', isTls=True): +def send_mail(send_from, send_to, subject, text, files=[], server="localhost", port=25, username='', password='', isTls=True): msg = MIMEMultipart() msg['From'] = send_from msg['To'] = COMMASPACE.join(send_to) - msg['Date'] = formatdate(localtime = True) + msg['Date'] = formatdate(localtime=True) msg['Subject'] = subject - msg.attach( MIMEText(text) ) + msg.attach(MIMEText(text)) for f in files: part = MIMEBase('application', "octet-stream") - part.set_payload( open(f,"rb").read() ) + part.set_payload(open(f, "rb").read()) encoders.encode_base64(part) - part.add_header('Content-Disposition', 'attachment; filename="{0}"'.format(os.path.basename(f))) + part.add_header('Content-Disposition', + 'attachment; filename="{0}"'.format(os.path.basename(f))) msg.attach(part) smtp = smtplib.SMTP(server, port) - if isTls: smtp.starttls() - if len(username)>0 : smtp.login(username,password) + if isTls: + smtp.starttls() + if len(username) > 0: + smtp.login(username, password) smtp.sendmail(send_from, send_to, msg.as_string()) smtp.quit() -def logit( text ): - log.write(str(datetime.datetime.utcnow())+ " "+text+"\n") + +def logit(text): + log.write(str(datetime.datetime.utcnow()) + " " + text + "\n") + def cleanexit(): log.close() - send_mail( email_from, [email_to], "Photo Upload", "Photo upload log", [logfile]) + send_mail(email_from, [email_to], "Photo Upload", + "Photo upload log", [args.log_file]) sys.exit() @@ -83,7 +156,7 @@ def cleanexit(): # Open a browser at the authentication URL. Do this however # you want, as long as the user visits that URL. authorize_url = flickr.auth_url(perms='write') - print("Open the following URL on your web browser and copy the code to " + config_file) + print("Open the following URL on your web browser and copy the code to " + args.config) print(authorize_url) # Get the verifier code from the user. Do this however you @@ -99,17 +172,17 @@ def cleanexit(): excludes = r'|'.join([fnmatch.translate(x) for x in excludes]) or r'$.' if len(sys.argv) < 2: - print("Usage: "+sys.argv[0]+" path") + print("Usage: " + sys.argv[0] + " path") sys.exit(0) try: - log = open(logfile, 'a') + log = open(args.log_file, 'a') except: - print("Can't open log file for writing ("+logfile+")") + print("Can't open log file for writing (" + args.log_file + ")") sys.exit() logit("New run") -for root, dirs, files in os.walk(sdcard): +for root, dirs, files in os.walk(args.pictures + '/'): # exclude dirs dirs[:] = [os.path.join(root, d) for d in dirs] @@ -121,14 +194,20 @@ def cleanexit(): files = [f for f in files if not re.match(excludes, f.lower())] files = [f for f in files if re.match(includes, f.lower())] - for fname in files: - try: - resp = flickr.upload(filename=fname, tags='rpiup', is_public=0) - pre, ext = os.path.splitext(fname) - os.rename(fname, pre + done) - logit(fname + " : " + resp.find('photoid').text) - except: - logit("Error on " + fname) + for fname in tqdm(files): + control_file = ntpath.dirname(fname) + \ + '/' + \ + args.uploaded_prefix + \ + ntpath.basename(fname) + + if not os.path.isfile(control_file): + try: + resp = flickr.upload(filename=fname, tags='rpiup', is_public=0) + pre, ext = os.path.splitext(fname) + touch(control_file) + logit(fname + " : " + resp.find('photoid').text) + except: + logit("Error on " + fname) logit("End run") cleanexit()