python-hard-way/Sermon-Upload/sermon-upload.py

88 lines
3.5 KiB
Python

# Sermon Upload v2.0 Alpha
import subprocess
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
import re
import argparse
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()
def upload(scrdir, dstdir, key, ssh): # Input has to end in /
#time.sleep(10) # Wait 10 seconds.
os.chdir(scrdir) # Go to sermon directory.
list = sorted(glob.glob('*.mp3')) # Sort files alphabetically.
mostrecent = list[-1] # Find most recent sermon
secondrecent = list[-2] # Find second most recent sermon
sermonsrc = scrdir + mostrecent # Grab the most recent sermon to copy.
sermondst = dstdir + mostrecent # Destination for most recent sermon.
copyfile(sermonsrc, sermondst) # Copy the sermon
# bash = subprocess.check_output('./sermon-upload.sh')
# print(bash)
os.chdir(dstdir) # Go to the directory where the sermon has been copied to
os.system("export sermondst")
os.environ['sermondst'] = str(sermondst)
# os.system(sermondst=sermondst)
#print(os.system("echo $sermondst"))
os.system("export key")
os.environ['key'] = key
# os.system(key=key)
os.system("export ssh")
os.environ['ssh'] = ssh
# os.system(ssh=ssh)
os.system('rsync --update --progress -e "ssh -i $key" *.mp3 $ssh:$sermondst') # Upload the sermon.
os.system("export dstdir")
os.environ['dstdir'] = dstdir
# os.system(dstdir=dstdir)
# os.system("findsermon='ls $dstdir'")
# os.system("export findsermon")
# os.environ['findsermon'] = 'ls $dstdir'
os.system("export recentsermon") # Export the list to Python
# subprocess.call("ssh -i $key $ssh ls $dstdir")
recentsermon = os.popen('ssh -i $key $ssh ls $dstdir').read()
# print(re.sub(' +', ' ', recentsermon))
recentsermon = "".join(recentsermon.split()) # Strip whitespace.
# recentsermon = re.sub (r'([^(\s+)])', '', recentsermon)
# print(recentsermon)
# print(ser)
# os.system(sermons=$("ssh -i $key $ssh ls $dstdir"))
# os.environ["recentsermon"] # Set it to a variable
# comparesermon = ""
# for c in recentsermon:
# comparesermon = comparesermon + recentsermon[c] # Try to grab most recent sermon.
# print(recentsermon) # Print most recent sermon.
#print(recentsermon)
#print(mostrecent)
if str(mostrecent) == str(recentsermon): # If the most recent sermon uploaded matches the most recent sermon locally, we're good.
notify2.init('Sermon Script')
n = notify2.Notification("Sermon Uploaded",
mostrecent
)
n.show()
else: # Otherwise, something went wrong.
notify2.init('Sermon Script')
n = notify2.Notification("Sermon Upload Failed",
mostrecent
)
n.show()
#upload("/mnt/52265C99265C8041/Users/Berean-Sound/sermons", "/home/josh/sermons/2018", "/home/berean/sermons/.tempssh/temp", "sermons@bereanbibleutah.org")
#upload("/home/josh/Downloads/Downloads/", "/home/josh/Downloads/Downloads2/", "/home/josh/.ssh/id_rsa", "josh@127.0.0.1")