-
Notifications
You must be signed in to change notification settings - Fork 16
/
stream_geolocation.py
47 lines (38 loc) · 1.71 KB
/
stream_geolocation.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
import sys
import tweepy
from tweepy import OAuthHandler
from tweepy import Stream
import argparse
import api_keys
from my_listener import MyListener
parser = argparse.ArgumentParser(description='collect tweets based on geographic location')
parser.add_argument('-l', '--geo-locations', type=str,
help='geo location coordinates from http://boundingbox.klokantech.com '
'copy and past using csv option',
required=True)
parser.add_argument('-j', '--json', type=str,
help='the the json output file.', required=True)
parser.add_argument('-n', '--number', type=int,
help='the number of tweets that you want to collect', required=True)
def login():
if api_keys.consumer_key == '' or api_keys.consumer_secret == '' \
or api_keys.access_token == '' or api_keys.access_secret == '':
print("API key not found. Please check api_keys.py file")
sys.exit(-1)
auth = OAuthHandler(api_keys.consumer_key, api_keys.consumer_secret)
auth.set_access_token(api_keys.access_token, api_keys.access_secret)
api = tweepy.API(auth)
return auth, api
def get_tweets(my_locations, outfile, stop_num):
auth, api = login()
twitter_stream = Stream(auth, MyListener(outfile, stop_num))
# Bounding boxes for geo-locations
# http://boundingbox.klokantech.com/
# Online-Tool to create boxes (c+p as raw CSV):
twitter_stream.filter(locations=my_locations, async=True)
if __name__ == '__main__':
args = parser.parse_args()
my_locations = [float(x) for x in args.geo_locations.split(',')]
stopAtNumber = args.number
outfile = args.json
get_tweets(my_locations, outfile, stopAtNumber)