-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslackshot.py
107 lines (86 loc) · 3.69 KB
/
slackshot.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
from input import Input
from slacker import Slacker
import sys, getopt, os
import ConfigParser
import picamera
import time
def main(argv):
(slack_config, camera_config, gpio_config) = parse_config()
start_capture(slack_config, gpio_config, camera_config)
def parse_config():
defaults = {'brightness': '50',
'contrast': '50',
'duration': '30',
'frame-rate': '30',
'message': 'Check out this amazing trick-shot!',
'pir': '-1', # PIR is disabled by default
'video-path' : '/home/pi/video.h264'}
config = ConfigParser.ConfigParser(defaults)
config.read('slackshot.cfg')
api_key = config.get('slack', 'api-key')
if api_key is None:
print 'Please add api-key to slackshot.cfg under [slack] -section'
sys.exit(2)
button_pin = config.getint('gpio', 'button')
if button_pin is None:
print 'Please add button to slackshot.cfg under [gpio] -section'
sys.exit(2)
pir_pin = config.getint('gpio', 'pir')
channels = config.get('slack', 'channels')
slack_config = {'api-key': api_key, 'channels': channels.split(','),
'message': config.get('slack', 'message')}
camera_config = {'video-path': config.get('camera', 'video-path'),
'brightness': config.getint('camera', 'brightness'),
'contrast': config.getint('camera', 'contrast'),
'duration': config.getint('camera', 'duration'),
'frame-rate': config.getint('camera', 'frame-rate'),
'resolution': (config.getint('camera', 'width'), config.getint('camera', 'height'))}
gpio_config = {'button': button_pin, 'pir': pir_pin}
return (slack_config, camera_config, gpio_config)
def start_capture(slack_config, gpio_config, camera_config):
button = Input('Button', gpio_config['button'])
pir = None
if gpio_config['pir'] != -1:
pir = Input('PIR sensor', gpio_config['pir'])
else:
print "No PIR sensor configured. Recording constantly."
with picamera.PiCamera() as camera:
configure_camera(camera, camera_config)
print 'Starting main loop...'
while True:
if pir is None or pir._pressed:
record_video(camera, camera_config['video-path'], camera_config['duration'])
if pir is not None:
pir._pressed = False
if button._pressed:
send_video(slack_config, camera_config['video-path'])
button._pressed = False
time.sleep(0.1)
def configure_camera(camera, camera_config):
print "Configuring camera..."
camera.brightness = camera_config['brightness']
camera.contrast = camera_config['contrast']
camera.framerate = camera_config['frame-rate']
camera.resolution = camera_config['resolution']
def record_video(camera, video_path, duration):
print "Recording clip for {} seconds...".format(duration)
camera.start_preview()
camera.start_recording(video_path)
time.sleep(duration)
camera.stop_recording()
camera.stop_preview()
print "Recorded."
def send_video(slack_config, video_path):
if os.path.isfile(video_path):
channels = slack_config['channels']
message = slack_config['message']
print "Sending video to channel(s): {}".format(channels)
slack = Slacker(slack_config['api-key'])
slack.chat.post_message(channels, message)
slack.files.upload(video_path, channels=channels)
print "Deleting file"
os.remove(video_path)
else:
print "No file to send at {}".format(video_path)
if __name__ == "__main__":
main(sys.argv[1:])