/
live_elastic_search.py
60 lines (52 loc) · 2.73 KB
/
live_elastic_search.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
54
55
56
57
58
59
60
from elasticsearch import Elasticsearch
from tweepy import OAuthHandler
from tweepy import Stream
from tweepy.streaming import StreamListener
import json
# Variables that contains the user credentials to access Twitter API
consumer_key=''
consumer_secret=''
access_token=''
access_token_secret=''
host = ["https://search-jds797-gr2rzisoplktc2g7orat65jfci.us-west-2.es.amazonaws.com"]
index_name = "twitter-index"
# This is a listener that appends the tweet text, longitude and latitude to a csv file.
class StdOutListener(StreamListener):
def on_data(self, data):
if len(host)==0:
es = Elasticsearch()
else:
es = Elasticsearch(host)
try:
json_data = json.loads(data)
tweet = json_data['text']
id = str(json_data['id'])
lon = None
lat = None
if json_data['coordinates']:
lon = float(json_data['coordinates']['coordinates'][0])
lat = float(json_data['coordinates']['coordinates'][1])
elif 'place' in json_data.keys() and json_data['place']:
lon = float(json_data['place']['bounding_box']['coordinates'][0][0][0])
lat = float(json_data['place']['bounding_box']['coordinates'][0][0][1])
elif 'retweeted_status' in json_data.keys() and 'place' in json_data['retweeted_status'].keys() and json_data['retweeted_status']['place']:
lon = float(json_data['retweeted_status']['place']['bounding_box']['coordinates'][0][0][0])
lat = float(json_data['retweeted_status']['place']['bounding_box']['coordinates'][0][0][1])
elif 'quoted_status' in json_data.keys() and 'place' in json_data['quoted_status'].keys() and json_data['quoted_status']['place']:
lon = float(json_data['quoted_status']['place']['bounding_box']['coordinates'][0][0][0])
lat = float(json_data['quoted_status']['place']['bounding_box']['coordinates'][0][0][1])
if lat and lon:
print (lat)
es.index(index=index_name, id=id, doc_type="tweet", body={"tweet": tweet, "location": {"lat": lat, "lon": lon}})
except Exception as e:
print("ERROR: " + str(e))
def on_error(self, status):
print(status)
if __name__ == '__main__':
# This handles Twitter authetification and the connection to Twitter Streaming API
l = StdOutListener()
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
stream = Stream(auth, l)
# This line filter Twitter Streams to capture data by the keywords: 'python', 'javascript', 'ruby'
stream.filter(track=['starbucks','android','national geographic','pets','music'])