-
Notifications
You must be signed in to change notification settings - Fork 323
/
Copy pathtwitter-search-geo.py
executable file
·91 lines (76 loc) · 3.8 KB
/
twitter-search-geo.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env python3
#-----------------------------------------------------------------------
# twitter-search-geo
# - performs a search for tweets close to New Cross, London,
# and outputs them to a CSV file.
#-----------------------------------------------------------------------
from twitter import *
import sys
import csv
latitude = 51.474144 # geographical centre of search
longitude = -0.035401 # geographical centre of search
max_range = 1 # search range in kilometres
num_results = 50 # minimum results to obtain
outfile = "output.csv"
#-----------------------------------------------------------------------
# load our API credentials
#-----------------------------------------------------------------------
import sys
sys.path.append(".")
import config
#-----------------------------------------------------------------------
# create twitter API object
#-----------------------------------------------------------------------
twitter = Twitter(auth=OAuth(config.access_key,
config.access_secret,
config.consumer_key,
config.consumer_secret))
#-----------------------------------------------------------------------
# open a file to write (mode "w"), and create a CSV writer object
#-----------------------------------------------------------------------
csvfile = open(outfile, "w")
csvwriter = csv.writer(csvfile)
#-----------------------------------------------------------------------
# add headings to our CSV file
#-----------------------------------------------------------------------
row = ["user", "text", "latitude", "longitude"]
csvwriter.writerow(row)
#-----------------------------------------------------------------------
# the twitter API only allows us to query up to 100 tweets at a time.
# to search for more, we will break our search up into 10 "pages", each
# of which will include 100 matching tweets.
#-----------------------------------------------------------------------
result_count = 0
last_id = None
print("Querying results close to %f, %f" % (latitude, longitude))
while result_count < num_results:
#-----------------------------------------------------------------------
# perform a search based on latitude and longitude
# twitter API docs: https://dev.twitter.com/rest/reference/get/search/tweets
#-----------------------------------------------------------------------
query = twitter.search.tweets(q="", geocode="%f,%f,%dkm" % (latitude, longitude, max_range), count=100, max_id=last_id)
for result in query["statuses"]:
#-----------------------------------------------------------------------
# only process a result if it has a geolocation
#-----------------------------------------------------------------------
if result["geo"]:
user = result["user"]["screen_name"]
text = result["text"]
latitude = result["geo"]["coordinates"][0]
longitude = result["geo"]["coordinates"][1]
#-----------------------------------------------------------------------
# now write this row to our CSV file
#-----------------------------------------------------------------------
row = [user, text, latitude, longitude]
csvwriter.writerow(row)
result_count += 1
last_id = result["id"]
#-----------------------------------------------------------------------
# let the user know where we're up to
#-----------------------------------------------------------------------
print(" - Got %d results" % result_count)
#-----------------------------------------------------------------------
# we're all finished, clean up and go home.
#-----------------------------------------------------------------------
csvfile.close()
print("Written to %s" % outfile)