Use argparse to take CLI input, check if sermon uploaded and notify the user of success/failure.

This commit is contained in:
Josh Mudge 2018-10-20 16:37:27 -06:00
parent c75ec75627
commit 741c2effca
3 changed files with 101 additions and 30 deletions

View File

@ -1,3 +1,3 @@
# Sermon Upload
Finds latest sermon, uploads it, notifies the user. Sounds simple, it isn't.
Finds latest sermon based on input, uploads it, notifies the user whether or not it works. Sounds simple, it isn't.

View File

@ -1,39 +1,73 @@
# Sermon Upload v1.2 Alpha
# 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
def upload(sermonsrc, sermondst, key): # Input has to end in /
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()
time.sleep(10) # Wait 10 seconds.
os.chdir(sermonsrc) # Go to sermon directory.
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 = sermonsrc + mostrecent # Grab the most recent sermon to copy.
sermondst = sermondst + mostrecent # Destination for 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
os.chdir(sermondst) # Go to the directory where the sermon has been copied to
call(sermondst=sermondst)
call("export sermondst")
print(call("echo sermondst"))
os.system("rsync --update --progress -e 'ssh -i /home/berean/sermons/.tempssh/temp' *.mp3 sermons@bereanbibleutah.org:/home/josh/sermons/2018/") # Upload the sermon.
call("findsermon='ls $sermondst'")
call("export findsermon")
call('sermons=$("ssh -i $key sermons@bereanbibleutah.org $findsermon")')
call("export sermons") # Export the list to Python
recentsermon = os.environ["sermons"] # Set it to a variable
recentsermon = recentsermon[-1] # Try to grab most recent sermon.
print(recentsermon) # Print most recent 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 mostrecent == recentsermon: # If the most recent sermon uploaded matches the most recent sermon locally, we're good.
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",
@ -49,4 +83,5 @@ def upload(sermonsrc, sermondst, key): # Input has to end in /
)
n.show()
upload("/mnt/52265C99265C8041/Users/Berean-Sound/sermons", "/home/josh/sermons/2018", "/home/berean/sermons/.tempssh/temp")
#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")

View File

@ -1,16 +1,52 @@
cd /home/berean/sermons/2018
sermons=$(ls /mnt/52265C99265C8041/Users/Berean-Sound/sermons)
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
--srcdir)
srcdir="$2"
shift # past argument
;;
--dstdir)
dstdir="$2"
shift # past argument
;;
--key)
key="$2"
shift # past argument
;;
--ssh)
ssh="$2"
shift # past argument
;;
--file)
file="$2"
shift # past argument
;;
*)
# unknown option
if test -z "${unknown}"
then
unknown=$1
else
echo "Unknown Option"
exit 1
fi
;;
esac
shift # past argument or value
done
cd $dstdir
sermons=$(ls $srcdir)
IFS=$'\n'
lastsermon=$(echo "${sermons[*]}" | sort -nr | head -n1)
cp /mnt/52265C99265C8041/Users/Berean-Sound/sermons/$lastsermon .
cp $srcdir/$lastsermon .
rsync --update --progress -e "ssh -i /home/berean/sermons/.tempssh/temp" *.mp3 sermons@bereanbibleutah.org:/home/josh/sermons/2018/
rsync --update --progress -e "ssh -i $key" *.mp3 $ssh:$dstdir
sermons=$(ssh -i /home/berean/sermons/.tempssh/temp sermons@bereanbibleutah.org 'ls /home/josh/sermons/2018')
sermons=$(ssh -i $key $ssh 'ls $dstdir')
IFS=$'\n'
recentsermon=$(echo "${sermons[*]}" | sort -nr) #| head -n2)
echo $recentsermon
#cd ..
#cd 2016
#rsync --progress -e "ssh -i ../.tempssh/temp" *.mp3 sermons@bereanbibleutah.org:/home/josh/sermons/2016/