-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebcam_handler.py
105 lines (95 loc) · 3.82 KB
/
webcam_handler.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
from base_handler import *
import datetime
import requests
from utils import PERM_ADMIN
from utils import get_exclamation
from PIL import Image
UPDATE = 'UP'
NOIMAGE = 'noimage.png'
class WebcamHandler(BaseHandler):
def __init__(self, config, messenger, service_hub):
super().__init__(config, messenger, service_hub, key="webcam", name="Webcams")
if 'webcam' in config:
self.cams = config['webcam'].get('cams', [])
self.tmp_path = config['webcam'].get('tmp_path', '/tmp/campic.jpg')
self.enabled = True
else:
self.enabled = False
def matches_message(self, message):
if not self.enabled:
return False
if message.lower().startswith('show '):
l = message[5:].lower()
return any([any([l.startswith(prefix) for prefix in x['prefices']])
for x in self.cams])
def help(self, permission):
if not self.enabled:
return
if permission >= PERM_ADMIN:
return {
'summary': "Shows you snapshots from your webcam feeds",
'examples': ["show living room"],
}
def handle(self, message, **kwargs):
l = message[5:].lower()
for cam in self.cams:
for prefix in cam['prefices']:
if l.startswith(prefix):
if ('users' in cam and str(kwargs['actor_id']) in cam['users']) \
or ('users' not in cam and kwargs['permission'] >= PERM_ADMIN):
return self.get_snapshot(cam['name'])
else:
return "Sorry, you don't get to see this camera."
return "Whoopsie, this never happens"
def handle_button(self, data, **kwargs):
data = data.split('§', 1)
cmd = data[0]
if cmd == UPDATE:
resp = self.get_snapshot(data[1], include_timestamp=True)
resp['answer'] = "Updated!"
return resp
def get_snapshot(self, cam, include_timestamp=False):
buttons = [[{
'text': "Update",
'data': "{}§{}".format(UPDATE, cam)
}]]
try:
url = next(c['url'] for c in self.cams if c['name'] == cam)
resp = requests.get(url, timeout=8)
with open("{}.jpg".format(self.tmp_path), 'wb') as file:
file.write(resp.content)
im = Image.open("{}.jpg".format(self.tmp_path))
im.save("{}.png".format(self.tmp_path))
message = f'{cam} camera'
if include_timestamp:
timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
message = f'{message} ({timestamp})'
return {
'message': message,
'photo': f'{self.tmp_path}.png',
'buttons': buttons,
}
except requests.exceptions.ConnectionError:
return {
'photo': NOIMAGE,
'message': "{} Seems like the {} webcam isn't online. :(".format(get_exclamation(), cam),
'buttons': buttons,
}
except StopIteration:
return {
'photo': NOIMAGE,
'message': "{} Seems like this webcam doesn't exist anymore. :(".format(get_exclamation()),
}
except requests.exceptions.Timeout:
return {
'photo': NOIMAGE,
'message': "{} The {} webcam timed out. :(".format(get_exclamation(), cam),
'buttons': buttons,
}
except Exception as e:
print(e)
return {
'photo': NOIMAGE,
'message': "{} Something went horribly wrong with {} webcam. :(".format(get_exclamation(), cam),
'buttons': buttons,
}