-
Notifications
You must be signed in to change notification settings - Fork 0
/
tweetcast.py
64 lines (49 loc) · 1.99 KB
/
tweetcast.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
# DELETE BEFORE MAKING THE REPO PUBLIC IMPORTANT!!!!!!!!
config = {}
config["consumer_key"] = "n47vnH3SEYVr343ckh4ytHopx"
config["consumer_secret"] = "BAMUGLPv82NO0KIg0sim18nZrdmmR46FnXLKWLYO26aGSNNlCs"
config["access_key"] = "2195450682-fHHCAfEmZr4mwi77esa1uMzRTTwKjwutfn9UXpf"
config["access_secret"] = "sUf5m6dB9G6XUfwXvraKxuRd2Jq14PRVhJdz0fK4W55ea"
#!/usr/bin/python
#-----------------------------------------------------------------------
# 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
import re
latitude = 48.1351 # geographical centre of search
longitude = 11.5820 # geographical centre of search
max_range = 1 # search range in kilometres
num_results = 100 # minimum results to obtain
outfile = "geotweets.csv"
twitter = Twitter(
auth = OAuth(config["access_key"], config["access_secret"], config["consumer_key"], config["consumer_secret"]))
csvfile = file(outfile, "w")
csvwriter = csv.writer(csvfile)
row = [ "user", "text", "latitude", "longitude" ]
csvwriter.writerow(row)
result_count = 0
last_id = None
while result_count < num_results:
query = twitter.search.tweets(q = "", geocode = "%f,%f,%dkm" % (latitude, longitude, max_range), count = 100, max_id = last_id)
for result in query["statuses"]:
if result["geo"]:
user = result["user"]["screen_name"]
text = result["text"]
text = text.encode('ascii', 'replace')
url = ""
url_results = re.search("(?P<url>https?://[^\s]+)", text)
if url_results:
url = url_results.group("url")
latitude = result["geo"]["coordinates"][0]
longitude = result["geo"]["coordinates"][1]
row = [ user, text, latitude, longitude, url ]
csvwriter.writerow(row)
result_count += 1
last_id = result["id"]
print "got %d results" % result_count
csvfile.close()
print "written to %s" % outfile