forked from akshaygill/scratch_gpio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadDweet.py
executable file
·53 lines (43 loc) · 1.33 KB
/
readDweet.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
import sys
import socket
import time
from array import array
def sendScratchCommand(cmd):
n = len(cmd)
a = array('c')
a.append(chr((n >> 24) & 0xFF))
a.append(chr((n >> 16) & 0xFF))
a.append(chr((n >> 8) & 0xFF))
a.append(chr(n & 0xFF))
try:
PORT = 42001
HOST = '127.0.0.1'
scratchSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
scratchSock.connect((HOST, PORT))
#print "data len" , n
scratchSock.sendall(a.tostring() + cmd)
time.sleep(0.1)
scratchSock.shutdown(socket.SHUT_RDWR)
scratchSock.close()
except:
print sys.exc_info()[0]
print ("Failed to send to Scratch")
dweetname = sys.argv[1] #Name of dweet channel to use
try:
key = sys.argv[2] #key name
value = sys.argv[3] #key value
except:
pass
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #Alocate a socket
s.connect(("dweet.io", 80)) #Connect to dweet.io
s.send("GET /get/latest/dweet/for/cycy42 HTTP/1.0\n\n")
#s.send("GET /get/dweets/for/cycy42 HTTP/1.0\n\n")
result = s.recv(50000)
#print(result)
res2 = result.split('"content"')
#print
#print res2[1]
res3 = res2[1].split('"')
#print
print res3[1],res3[3]
sendScratchCommand('sensor-update ' + res3[1] + ' ' + res3[3])