-
Notifications
You must be signed in to change notification settings - Fork 0
/
monitor-exec.py
171 lines (132 loc) · 4.03 KB
/
monitor-exec.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import sys
import os
import dropbox
import picamera
from time import sleep
import subprocess
outdir = "/home/neil/raspi-home-monitor/output/"
PHOTOFLAG = True
VIDEOFLAG = False
TESTFLAG = False
vidLength = 10 #seconds
if len(sys.argv) > 1: #then there are command line arguments
if '-v' in sys.argv:
PHOTOFLAG = False
VIDEOFLAG = True
if len(sys.argv) > 2:
vidLength = int(sys.argv[2]) #If video, then there is the optional length argument
if '-t' in sys.argv:
PHOTOFLAG = False
TESTFLAG = True
#CAPTURING
def photoCapture(filename):
camera = picamera.PiCamera()
camera.resolution = (2592, 1944)
camera.start_preview()
# Camera warm-up time
sleep(3)
camera.capture(outdir + filename)
f = open(outdir + filename, 'rb')
return f
def vidCapture(filename):
camera = picamera.PiCamera()
camera.resolution = (1296, 972)
camera.start_recording(outdir + filename)
camera.wait_recording(vidLength)
camera.stop_recording()
f = open(outdir + filename, 'rb')
return f
#Takes a screencapture - OSX ONLY
def screenCapture(filename):
os.system("screencapture " + outdir + filename)
f = open(outdir + filename,'rb')
return f
#DROPBOX UTILITIES
#Uploads a file to the dropbox client
def upload(client, filename):
with open("output/" + filename, 'rb') as f:
data = f.read()
writepath = "/"
writepath += filename
response = client.files_upload(data,writepath,mode=dropbox.files.WriteMode.add)
# response = client.files_upload()
# response = client.put_file(filename, thefile)
print("uploaded:", response)
#This is to ensure that you don't overwrite a file that has been previously uploaded
#def getValidFilename(baseName, ext):
# isInvalidName = True
# validName = None
#
# while(isInvalidName):
# folder_metadata = client.metadata('',list=True)
# files = [content['path'].split('/')[-1] for content in folder_metadata['contents']]
#
# files.sort()
#
# usedNumbers = [fn.split('.')[0].split('-')[-1] for fn in files if baseName in fn]
# usedNumbers.sort()
#
# if len(usedNumbers) > 0:
# lastNumber = usedNumbers[-1]
# else:
# lastNumber = 0
# newNumber = int(lastNumber) + 1
#
# validName = baseName + '-' + str(newNumber) + ext
#
# if validName not in files:
# isInvalidName = False
#
# return validName
def getValidFilename(baseName, ext):
currentNum = 0
with open("num.txt",'r') as nf:
try:
currentNum = int(nf.read())
except:
currentNum = 0
nextNum = currentNum+1
with open("num.txt",'w') as nf:
nf.write(str(nextNum))
validName = baseName + '-' + str(nextNum) + ext
return validName
#MAIN
if __name__ == '__main__':
KEY = os.environ['DBKEY']
SECRET = os.environ['DBSECRET']
TOKEN = os.environ['DBTOKEN']
print("Capturing...")
if PHOTOFLAG is True:
baseName = "img"
ext = ".jpg"
filename = getValidFilename(baseName,ext)
f = photoCapture(filename)
elif VIDEOFLAG is True:
baseName = "vid"
ext = ".h264" #TODO figure out the video extension
filename = getValidFilename(baseName,ext)
f = vidCapture(filename)
elif TESTFLAG is True:
baseName = "test"
ext = ".png"
filename = getValidFilename(baseName,ext)
f = screenCapture(filename)
else:
print("Error during capture - no flags set somehow. Exiting...")
exit(1)
print("Captured.")
f.close()
print("Converting...")
base = filename.split('.')[0]
convName = base + ".mp4"
subprocess.call(["MP4Box", "-add", "output/" + filename, "output/" + convName])
local = False
try:
client = dropbox.Dropbox(TOKEN)
except:
print("Client didn't connect!!! Recording Locally")
local = True
if not local:
print("Client Connected")
print("Uploading to Dropbox")
upload(client, convName)