-
Notifications
You must be signed in to change notification settings - Fork 4
/
uifttt.py
41 lines (35 loc) · 1.17 KB
/
uifttt.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
import socket
try:
import ussl as ssl
except:
import ssl
IFTTT_HOST = 'maker.ifttt.com'
IFTTT_PORT = 443
SAFE_CHARS = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_.- '
def make_safe(string):
r = []
for c in string:
if c in SAFE_CHARS:
r.append(c)
else:
r.append('%%%x' % ord(c))
return (''.join(r)).replace(' ', '+')
def trigger(event, key, value1='', value2='', value3=''):
path = '/trigger/%s/with/key/%s' % (make_safe(event), make_safe(key))
data = '{'+value1+':"",'+value2+':"",'+value3+':""}'
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(10)
s = ssl.wrap_socket(sock)
s.connect(socket.getaddrinfo(IFTTT_HOST, IFTTT_PORT)[0][4])
request = '%s %s HTTP/1.0\r\n' % ('POST', path)
request += 'Host: %s\r\n' % IFTTT_HOST
request += 'Content-Type: application/json\r\n'
request += 'Content-Length: %s\r\n\r\n%s\r\n\r\n' % (len(data), data)
s.send(request)
response = ''
while 1:
recv = s.readline()
if len(recv) == 0: break
response += recv.decode()
s.close()
return response