From 9dbce853f9ad028dd6597344f45f63d6de69e0cb Mon Sep 17 00:00:00 2001 From: Luke Ogburn <21106956+logburn@users.noreply.github.com> Date: Wed, 21 Apr 2021 16:17:55 -0400 Subject: [PATCH] long time no commit --- conn_test.py | 19 ----------------- cronjob.py | 59 +++++++++++++++++++++++++++++++++++++++++++++------- fork_test.py | 21 +++++++++++++++++++ interval | 2 +- numFiles.txt | 2 +- server.py | 7 +++++-- zip_test.py | 14 ------------- 7 files changed, 80 insertions(+), 44 deletions(-) delete mode 100644 conn_test.py create mode 100644 fork_test.py delete mode 100644 zip_test.py diff --git a/conn_test.py b/conn_test.py deleted file mode 100644 index 9b7cae3..0000000 --- a/conn_test.py +++ /dev/null @@ -1,19 +0,0 @@ -import os -from signal import signal, SIGINT -from goprocam import GoProCamera, constants - -seconds = 4 - -# helps with video recording -def handler(s, f): - gopro.stopWebcam() - quit() - -def takeVideo(time): - signal(SIGINT, handler) - gopro = GoProCamera.GoPro() - gopro.shoot_video(time) - gopro.downloadLastMedia(custom_filename=(location+"plaintext/" + numFiles() + ".mp4")) - gopro.delete("last") - -takeVideo(seconds) diff --git a/cronjob.py b/cronjob.py index 2851a45..1455d45 100644 --- a/cronjob.py +++ b/cronjob.py @@ -16,9 +16,11 @@ import os import glob from signal import signal, SIGINT from goprocam import GoProCamera, constants +import time +import shutil # global vars -location = "/home/pi/final/videos/" # location of video files, subfolders "plaintext" and "encrypted" assumed to exist +location = os.getcwd() + "/videos/" # location of video files, subfolders "plaintext" and "encrypted" assumed to exist locpt = location + "plaintext/" locec = location + "encrypted/" seconds = int(open("interval", "r").read()) # seconds to record video for @@ -30,7 +32,7 @@ def handler(s, f): # returns the current file number for naming convention def numFiles(): - return str(len([item for item in os.listdir(location+"encrypted/") if os.path.isfile(os.path.join(location+"encrypted/", item))])) + return str(len([item for item in os.listdir(locec) if os.path.isfile(os.path.join(locec, item))])) # compresses a given file, new file is named same filename with .zip at end # returns name of new file @@ -44,7 +46,7 @@ def compress(filename): def takeVideo(time): signal(SIGINT, handler) gopro = GoProCamera.GoPro(ip_address=GoProCamera.GoPro.getWebcamIP()) - gopro.video_settings("720p", fps='30') + gopro.video_settings("480p", fps='30') gopro.shoot_video(time) gopro.downloadLowRes(custom_filename=(location+"plaintext/" + numFiles() + ".mp4")) gopro.delete("last") @@ -74,13 +76,15 @@ def encryptVideo(file): cipher = AES.new(key, AES.MODE_EAX) ciphertext, tag = cipher.encrypt_and_digest(message) # save encrypted file - file_out = open(location + "encrypted/" + numFiles() + ".mpc", "wb") + file_out = open(locec + numFiles() + ".mpc", "wb") [ file_out.write(x) for x in (cipher.nonce, tag, ciphertext) ] file_out.close() # save the encrypted key with open("keys/" + str(int(numFiles()) - 1) + ".asc", "wb+") as keyf: keyf.write(PKEncrypt(key)) +# OLD METHOD +'''''' # record the video and store in file location takeVideo(seconds) @@ -90,11 +94,52 @@ fileset = [file for file in glob.glob(location + "plaintext/*.mp4", recursive=Fa with open("numFiles.txt", "w+") as numf: numf.write(numFiles()) -# compress -i = 1 +# compress and encrypt for file in fileset: newf = compress(file) encryptVideo(newf) os.remove(file) os.remove(newf) - +'''''' + +# FORK +def child(): + # child compresses and encrypts + fileset = [file for file in glob.glob(locpt + "*.mp4", recursive=False)] + if(len(fileset) == 0): + os._exit(0) + # prevents double encryption + folder = locpt + str(int(time.time())) + os.mkdir(folder) + for file in fileset: + shutil.move(file, folder) + fileset = [file for file in glob.glob(folder + "/*.mp4", recursive=False)] + for file in fileset: + print("Encrypting file " + file) + newf = compress(file) + encryptVideo(newf) + os.remove(file) + os.remove(newf) + shutil.rmtree(folder) + with open("numFiles.txt", "w+") as numf: + numf.write(numFiles()) + os._exit(0) + +def parent(): + # parent records, child does everything else + try: + while True: + newpid = os.fork() + if newpid == 0: + child() + else: + takeVideo(seconds) + except KeyboardInterrupt: + print('Interrupted') + try: + #child() + os._exit(0) + except SystemExit: + os._exit(0) +#parent() + diff --git a/fork_test.py b/fork_test.py new file mode 100644 index 0000000..c46ece2 --- /dev/null +++ b/fork_test.py @@ -0,0 +1,21 @@ +import os + +def child(): + print('\nA new child ', os.getpid()) + os._exit(0) + +def parent(): + while True: + newpid = os.fork() + if newpid == 0: + child() + else: + pids = (os.getpid(), newpid) + print("parent: %d, child: %d\n" % pids) + reply = input("q for quit / c for new fork") + if reply == 'c': + continue + else: + break + +parent() diff --git a/interval b/interval index 697cb3a..7ed6ff8 100644 --- a/interval +++ b/interval @@ -1 +1 @@ -300 +5 diff --git a/numFiles.txt b/numFiles.txt index 56a6051..d8263ee 100644 --- a/numFiles.txt +++ b/numFiles.txt @@ -1 +1 @@ -1 \ No newline at end of file +2 \ No newline at end of file diff --git a/server.py b/server.py index 45b570d..5a771d0 100644 --- a/server.py +++ b/server.py @@ -1,9 +1,12 @@ import http.server import socketserver +import socket PORT = 63654 Handler = http.server.SimpleHTTPRequestHandler +h_name = socket.gethostname() +IPaddr = str(socket.gethostbyname(h_name)) with socketserver.TCPServer(("", PORT), Handler) as httpd: - print("Serving at port", PORT) - httpd.serve_forever() + print("Serving at " + IPaddr + ":" + str(PORT)) + httpd.serve_forever() diff --git a/zip_test.py b/zip_test.py deleted file mode 100644 index b869ffc..0000000 --- a/zip_test.py +++ /dev/null @@ -1,14 +0,0 @@ -import zipfile - -w = zipfile.ZipFile("NewZipfile.zip", mode='w', compression=zipfile.ZIP_DEFLATED) - -# Write to zip file -w.write("test.txt") -w.close() - -r = zipfile.ZipFile("NewZipfile.zip", mode='r', compression=zipfile.ZIP_DEFLATED) - -# Reading Zip File -print("\n", r.read('test.txt')) - -r.close()