-
Notifications
You must be signed in to change notification settings - Fork 1
/
streamer.py
49 lines (40 loc) · 1.63 KB
/
streamer.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
# -*- coding: utf-8 -*-
import tweepy
from os import environ
from searcher import es
# keywords to stream
KEYWORDS = ['apple', 'bar', 'cat', 'dog', 'eat', 'food']
def parse_tweet(tweet):
return {'user_name': tweet['user']['name'],
'screen_name': tweet['user']['screen_name'],
'text': tweet['text'],
'location': {'lat': tweet['coordinates']['coordinates'][1],
'lon': tweet['coordinates']['coordinates'][0]},
'date': tweet['created_at']}
class StreamListener(tweepy.StreamListener):
"""docstring for StreamListener"""
# save twitts to Elastic Search
def on_status(self, status):
# reject twitts without geotag
if status._json['coordinates'] is None:
return
tweet = parse_tweet(status._json)
print tweet, "\n---------------"
es.index(index="tweets", doc_type="tweet", body=tweet)
class Streamer:
"""docstring for Streamer"""
def __init__(self, auth=None, keywords=['foo']):
self.keywords = keywords
self.streamer = tweepy.Stream(auth=auth, listener=StreamListener())
def run(self):
self.streamer.filter(None, self.keywords)
self.streamer.userstream(None)
if __name__ == '__main__':
twitter_auth = tweepy.OAuthHandler(environ['twitt_api_key'],
environ['twitt_api_secret'])
twitter_auth.set_access_token(environ['twitt_token_key'],
environ['twitt_token_secret'])
print es.info()
print "---------------\nStreamming...\n---------------"
streamer = Streamer(twitter_auth, KEYWORDS)
streamer.run()