Created
September 26, 2012 01:43
-
-
Save amiller/3785509 to your computer and use it in GitHub Desktop.
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 urllib, urllib2 | |
import json | |
import base64 | |
import time | |
def coroutine(func): | |
def start(*args,**kwargs): | |
cr = func(*args,**kwargs) | |
cr.next() | |
return cr | |
return start | |
class config: | |
TWITTER = { | |
'user': 'dv_test_1', | |
'password': 'mrybRJnp'} | |
TIME_FORMAT = "" | |
LANG = "en" | |
USER_AGENT = "" | |
def twitter_producer(queries): | |
# Consumes all of its input before yielding | |
url = 'https://stream.twitter.com/1/statuses/filter.json' | |
queries = [q.lower() for q in queries] | |
quoted_queries = [urllib.quote(q) for q in queries] | |
query_post = 'track=' + ",".join(quoted_queries) | |
request = urllib2.Request(url, query_post) | |
auth = base64.b64encode('%s:%s' % (config.TWITTER['user'], config.TWITTER['password'])) | |
request.add_header('Authorization', "basic %s" % auth) | |
request.add_header('User-agent', config.USER_AGENT) | |
for item in urllib2.urlopen(request): | |
try: | |
item = json.loads(item) | |
except json.JSONDecodeError: #for whatever reason json reading twitters json sometimes raises this | |
continue | |
yield item | |
@coroutine | |
def twitter_transducer(queries, emit): | |
while True: | |
item = (yield) | |
if 'text' in item and 'user' in item: | |
text = item['text'].lower() | |
user_id = item['user']['id_str'] | |
for q in queries: | |
if q in text: | |
emit((q, (user_id, text))) | |
def buffer_stream(source, emit): | |
import gevent | |
twitter_source() | |
def square_(keywords, text): | |
return keyword in text | |
def word_in_dict(token,keywords): | |
if token in keywords: pass | |
def match(keyword, word): | |
filter(op.equals, text.split()) | |
if keyword in text: yield (keyword, text) | |
def fan_in(keywords, text): | |
for kw in keywords: | |
pass | |
fruits = ['apple','banana','carrot'] | |
def follow(data, target): | |
for datum in data: target.send(datum) | |
@coroutine | |
def printer(name=''): | |
while True: | |
line = (yield) | |
print name, line | |
if __name__ == '__main__': | |
follow(twitter_producer(fruits), twitter_transducer(['apple'], printer().send)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment