-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlocation_stream.py
73 lines (63 loc) · 2.71 KB
/
location_stream.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
61
62
63
64
65
66
67
68
69
70
71
72
73
#-*- coding: utf-8 -*-
from tweepy.streaming import StreamListener
from tweepy import Stream
from datetime import datetime
from model import Tweet
from auth import auth_handler
from traceback import print_exc
from database import db_session
import json
import time
errors = {
"304": "There was no new data to return.",
"400": "The request was invalid or cannot be otherwise served",
"401": "Missing or incorrect authentication credentials",
"403": "The request is understood, but it has been refused or access is not allowed",
"404": "The URI requested is invalid or the resource requested",
"406": "Invalid format is specified in the request.",
"410": "API endpoint has been turned off",
"420": "Returned when you are being rate limited",
"422": "Returned when an image uploaded to POST account / update_profile_banner is unable to be processed.",
"429": "Too Many Requests",
"500": "Internal Server Error",
"502": "Bad Gateway",
"503": "Service Unavailable",
"504": "Gateway timeout" }
class StdOutListener(StreamListener):
def on_data(self, data):
"""
Called by StreamListener whenever a new tweet arrives
"""
try:
tweet_json = json.loads(data)
coordinates = tweet_json.get('coordinates', {}).get('coordinates', None) if tweet_json.get('coordinates') else None
place = tweet_json.get('place').get('full_name') if tweet_json.get('place') else ''
if coordinates is None:
# If we don't have coordinates we dont care about this tweet
return True
tweet = Tweet(
id=tweet_json.get('id'),
tweet_text=tweet_json.get('text'),
user_id=tweet_json.get('user').get('id'),
coordinates=coordinates,
created_at=tweet_json.get('created_at'),
json=tweet_json,
last_update=datetime.now(),
place=place)
tweet.save()
except:
print_exc()
db_session.rollback()
return True
def on_error(self, status_code):
if errors.has_key(status_code):
print(errors.get(status_code))
else:
print(status_code)
if __name__ == '__main__':
#This handles Twitter authetification and the connection to Twitter Streaming API
l = StdOutListener()
stream = Stream(auth_handler, l)
print('[{}] Start location stream API listener'.format(time.strftime('%c')))
#This line filter Twitter Streams to capture data from london and new york
stream.filter(locations = [-74.25888888888889,40.4772222,-73.7,40.9175,-0.35138888888888886,51.3847222,0.14805555555555555,51.6722222,-77.119759,38.7916449,-76.909393,38.995548,-118.6681759,33.7036519,-118.1552891,34.3373061])