-
Notifications
You must be signed in to change notification settings - Fork 4
/
twitter-to-mqtt.py
43 lines (35 loc) · 1.25 KB
/
twitter-to-mqtt.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
#!/usr/bin/env python
import os, pycurl, json, StringIO, threading
import mosquitto
import settings
class TwitterStreamClient:
def __init__(self):
self.buffer = ""
self.shutdown = False
self.client = mosquitto.Mosquitto("TwitterFeed_%s"%(os.urandom(4).encode("hex"),))
self.client.on_connect = self.onmqttconnect
self.client.connect(settings.MQTT_HOST,settings.MQTT_PORT,keepalive=60)
try:
while True:
self.client.loop(1000)
except:
self.client.disconnect()
self.shutdown = True;
def onmqttconnect(self,client,obj,rc):
if rc == 0:
self.streamthread = threading.Thread(target=self.startstream)
self.streamthread.start()
def startstream(self):
self.conn = pycurl.Curl()
self.conn.setopt(pycurl.USERPWD, "%s:%s" % (settings.TWITTER_USER, settings.TWITTER_PASS))
self.conn.setopt(pycurl.URL, settings.TWITTER_STREAM_URL)
self.conn.setopt(pycurl.WRITEFUNCTION, self.onstreamreceive)
self.conn.perform()
def onstreamreceive(self, data):
self.buffer += data
if data.endswith("\r\n") and self.buffer.strip():
self.client.publish(settings.MQTT_TOPIC,self.buffer)
self.buffer = ""
if self.shutdown:
return 0
client = TwitterStreamClient()