Created
March 20, 2012 03:36
-
-
Save kracekumar/2130913 to your computer and use it in GitHub Desktop.
zeromq + twitter staus filter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import time | |
import json | |
import zmq | |
from multiprocessing import Process | |
import couchdbkit | |
s = couchdbkit.Server("https://usr:[email protected]") | |
db = couchdbkit.Database("https://usr:[email protected]/db") | |
def worker(): | |
context = zmq.Context() | |
work_receiver = context.socket(zmq.PULL) | |
work_receiver.connect("tcp://127.0.0.1:5557") | |
while 1: | |
d = json.loads(work_receiver.recv()) | |
try: | |
db.save_doc(doc=d) | |
except Exception as e: | |
with open('sachin_omq_log.txt','a') as f: | |
msg = " ".join([e.message, " ", time.ctime(), "\n"]) | |
f.write(msg) | |
def store_live_tweets(): | |
Process(target=worker, args=()).start() | |
context = zmq.Context() | |
send = context.socket(zmq.PUSH) | |
send.bind("tcp://127.0.0.1:5557") | |
track = ['sachin', 'sachinism', 'Sachin', 'Sachinism', 'tendulkar', 'Tendulkar', 'sachintendulkar','SachinTendulkar'] | |
r = requests.post('https://stream.twitter.com/1/statuses/filter.json', | |
data={'track': track}, auth=('usr', 'passwd')) | |
for line in r.iter_lines(): | |
if line: | |
print line | |
send.send(line) | |
if __name__ == '__main__': | |
try: | |
store_live_tweets() | |
except Exception as e: | |
with open('sachin_omq_log.txt', 'a') as f: | |
msg = " ".join([e.message, " ", time.ctime(), "\n"]) | |
f.write(msg) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment