2018-10-20 23:31:13 +00:00
|
|
|
# Sermon Upload v2.1 Alpha
|
2018-10-06 17:38:44 +00:00
|
|
|
|
2018-10-20 22:37:27 +00:00
|
|
|
import subprocess
|
2018-09-30 01:06:38 +00:00
|
|
|
from subprocess import call # We can do BASH in Python if needed.
|
|
|
|
import os # Lots of system stuff
|
|
|
|
from shutil import copyfile # Allow copying of files
|
|
|
|
import glob # Import glob tools for file listings.
|
|
|
|
import notify2 # Notification utility.
|
|
|
|
import time # Import time into the galaxy
|
2018-10-20 22:37:27 +00:00
|
|
|
import re
|
|
|
|
import argparse
|
2018-09-30 01:06:38 +00:00
|
|
|
|
2018-10-20 22:37:27 +00:00
|
|
|
parser = argparse.ArgumentParser(usage="python3 sermon-upload.py source destination keyfile sshinfo \nFor example, python3 sermon-upload.py /home/josh/Downloads/Downloads/ /home/josh/Downloads/Downloads2/ /home/josh/.ssh/id_rsa josh@127.0.0.1")
|
|
|
|
parser.add_argument("scrdir")
|
|
|
|
parser.add_argument("dstdir")
|
|
|
|
parser.add_argument("key")
|
|
|
|
parser.add_argument("ssh")
|
|
|
|
args = parser.parse_args()
|
2018-10-20 19:34:37 +00:00
|
|
|
|
2018-10-20 22:37:27 +00:00
|
|
|
def upload(scrdir, dstdir, key, ssh): # Input has to end in /
|
2018-10-20 23:31:13 +00:00
|
|
|
# time.sleep(10) # Wait 10 seconds to make sure the machine is booted if we're doing auto. NOTE: Change to only run when auto is specified.
|
2018-10-20 22:37:27 +00:00
|
|
|
os.chdir(scrdir) # Go to sermon directory.
|
2018-10-20 19:34:37 +00:00
|
|
|
list = sorted(glob.glob('*.mp3')) # Sort files alphabetically.
|
|
|
|
mostrecent = list[-1] # Find most recent sermon
|
|
|
|
secondrecent = list[-2] # Find second most recent sermon
|
|
|
|
|
2018-10-20 22:37:27 +00:00
|
|
|
sermonsrc = scrdir + mostrecent # Grab the most recent sermon to copy.
|
|
|
|
sermondst = dstdir + mostrecent # Destination for most recent sermon.
|
2018-10-20 19:34:37 +00:00
|
|
|
|
|
|
|
copyfile(sermonsrc, sermondst) # Copy the sermon
|
|
|
|
|
2018-10-20 22:37:27 +00:00
|
|
|
os.chdir(dstdir) # Go to the directory where the sermon has been copied to
|
2018-10-20 23:31:13 +00:00
|
|
|
|
|
|
|
# Export some variables to BASH
|
|
|
|
|
|
|
|
# os.popen("export sermondst")
|
|
|
|
os.environ['sermondst'] = sermondst
|
|
|
|
# os.popen("export key")
|
2018-10-20 22:37:27 +00:00
|
|
|
os.environ['key'] = key
|
2018-10-20 23:31:13 +00:00
|
|
|
# os.popen("export ssh")
|
2018-10-20 22:37:27 +00:00
|
|
|
os.environ['ssh'] = ssh
|
2018-10-20 23:31:13 +00:00
|
|
|
os.popen('rsync --update --progress -e "ssh -i $key" $mostrecent $ssh:$sermondst') # Upload the sermon.
|
|
|
|
# os.popen("export dstdir")
|
2018-10-20 22:37:27 +00:00
|
|
|
os.environ['dstdir'] = dstdir
|
2018-10-20 23:31:13 +00:00
|
|
|
# os.popen("export recentsermon") # Export the list to Python
|
|
|
|
recentsermon = os.popen('ssh -i $key $ssh ls $sermondst').read() # Get most recent sermon from destination server.
|
|
|
|
|
|
|
|
# recentsermon = recentsermon[-1:]
|
|
|
|
recentsermon = recentsermon.rstrip() # Strip whitespace at the end of the string.
|
|
|
|
# recentsermon = recentsermon.split(',')
|
|
|
|
# print(recentsermon[-1])
|
|
|
|
# recentsermon = recentsermon[-1]
|
|
|
|
# recentsermon = "".join(recentsermon.split()) # Strip whitespace.
|
|
|
|
|
2018-10-20 22:37:27 +00:00
|
|
|
#print(mostrecent)
|
2018-10-20 23:31:13 +00:00
|
|
|
#print(recentsermon)
|
|
|
|
|
|
|
|
if recentsermon == sermondst:
|
2018-10-20 22:37:27 +00:00
|
|
|
|
2018-10-20 23:31:13 +00:00
|
|
|
# if str(mostrecent) == str(recentsermon): # If the most recent sermon uploaded matches the most recent sermon locally, we're good.
|
2018-10-20 19:34:37 +00:00
|
|
|
|
2018-10-20 23:31:13 +00:00
|
|
|
notify2.init('Sermon Script') # Notify user of success.
|
2018-10-20 19:34:37 +00:00
|
|
|
n = notify2.Notification("Sermon Uploaded",
|
|
|
|
mostrecent
|
|
|
|
)
|
|
|
|
n.show()
|
|
|
|
|
2018-10-20 23:31:13 +00:00
|
|
|
else: # Otherwise, something went wrong, say it failed.
|
2018-10-20 19:34:37 +00:00
|
|
|
|
|
|
|
notify2.init('Sermon Script')
|
|
|
|
n = notify2.Notification("Sermon Upload Failed",
|
|
|
|
mostrecent
|
2018-10-06 17:48:39 +00:00
|
|
|
)
|
2018-10-20 19:34:37 +00:00
|
|
|
n.show()
|
2018-10-06 17:48:39 +00:00
|
|
|
|
2018-10-20 23:31:13 +00:00
|
|
|
upload(args.scrdir, args.dstdir, args.key, args.ssh)
|
2018-10-20 22:37:27 +00:00
|
|
|
#upload("/mnt/52265C99265C8041/Users/Berean-Sound/sermons", "/home/josh/sermons/2018", "/home/berean/sermons/.tempssh/temp", "sermons@bereanbibleutah.org")
|