-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfoscam.py
executable file
·115 lines (99 loc) · 3.74 KB
/
foscam.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
#!/usr/bin/env python
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import os
import sys
import datetime
import getopt
from time import sleep
import urllib
import signal
import utils
import creds
import config
import socket
def main():
socket.setdefaulttimeout(config.url_timeout)
opts, args = getopt.getopt(sys.argv[1:], "hdi:c:u:", ["help", "debug", "image-dir=", "camera-name=", "url="])
_debug = False
show_help = False
image_dir = None
camera_name = None;
url = creds.url_pass
for o, a in opts:
if o in("-d", "--debug"):
_debug = True
if o in("-h", "--help"):
show_help = True
if o in("-i", "--image-dir"):
image_dir = a
if o in("-c", "--camera-name"):
camera_name = a
if o in("-u", "--url"):
url = a
if not os.path.isdir(image_dir):
print "Image store directory " + image_dir + " does not exist. Aborting"
sys.exit(-1)
if camera_name == None:
print "Please provide camera name with -c option"
sys.exit(-1)
error_count = 0
email_sent = 0
log_filename = camera_name + ".log"
log_filepath = os.path.join(image_dir, log_filename)
log = open(log_filepath,'a+')
log.write("".join(["[", datetime.datetime.now().strftime('%x %X'), "]",
"Starting foscam for ", camera_name, "\n"]))
try:
while True:
log.write
ctr = 0
fname_ctr = 0
monthstr = datetime.datetime.now().strftime('%Y-%m')
monthdir = os.path.join(image_dir, monthstr)
if not os.path.isdir(monthdir):
os.makedirs(monthdir)
timestr = datetime.datetime.now().strftime('%d_%H-%M-%S')
timedir = os.path.join(monthdir, timestr)
os.makedirs(timedir)
while ctr <int(config.rotate_interval):
ctr = ctr + int(config.lapse_interval)
fname_ctr = fname_ctr + 1
fname = "%s/snap_%05d.jpg" % (timedir, fname_ctr)
try:
urllib.urlretrieve(url, fname)
except IOError:
log.write("Exception in urllib")
error_count = error_count + 1
log.write("Error count:" + str(error_count) + "\n")
if error_count > config.max_errors:
if email_sent == 0:
utils.send_email (config.report_from, config.report_to, "Error count reached on foscam", "Camera name:" +camera_name )
email_sent = email_sent + 1;
sleep(int(config.lapse_interval))
cmd = "convert -delay %s %s/snap_*.jpg %s/lapse_%s.mp4 2>/dev/null" % (config.lapse_interval, timedir, timedir, timestr)
if _debug:
print cmd
os.system(cmd)
cmd = "chmod 644 %s/lapse_%s.mp4" % (timedir,timestr)
if _debug:
print cmd
os.system(cmd)
log.close()
except KeyboardInterrupt:
print("\nKeyboard interrupt detected. Gracefully exiting.")
log.close()
sys.exit(0)
if __name__ == "__main__":
main()