forked from QCaudron/Twitter-GoogleMaps
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTweetMapper.py
executable file
·69 lines (63 loc) · 2.31 KB
/
TweetMapper.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
#coding: utf-8
# Copyright 2014, Radhika S. Saksena (radhika dot saksena at gmail dot com)
# Licensed under the Apache License, Version 2.0
# http://www.apache.org/licenses/LICENSE-2.0
import time
import json
import os
import sys
import re
import codecs
from tweepy.streaming import StreamListener
from pygeocoder import Geocoder
from pygeolib import GeocoderError
import pygmaps
import webbrowser
#import MapTweet
"""Set of functions that use the GoogleMaps API to geocode and map tweets."""
def geocodeTweet(jsonStr,gcCount,flog):
"""This method geocodes tweets and does one of the following:
(1) returns tweet's coordinates or (2) returns tweeter's geocoded loc
or (3) check if it's a retweet and return orig tweeter's loc or
(4) finally, gc failed and return ["-99999","-99999"]"""
coordinates = ["-99999","-99999"]
try:
if(jsonStr["coordinates"]):
coordinates = jsonStr["coordinates"]["coordinates"]
else:
location = jsonStr["user"]["location"]
coordinates = Geocoder.geocode(location)[0].coordinates
gcCount += 1
except GeocoderError,e:
flog.write("GeocoderError exception is: %s\n" % e)
pass
except Exception,e:
flog.write("Other exception is: %s\n" % e)
pass
return coordinates
def initMap(ofile,useBrowser):
"""Set up Google Maps"""
# centre map on Greenwich
twMap = pygmaps.maps(35.20,0.0,2.95)
twMap.draw(os.getcwd() + "/" + ofile)
if useBrowser:
if(re.search("linux",sys.platform)):
print "os is linux"
controller = webbrowser.get("firefox")
elif(re.search("darwin",sys.platform)):
print "os is darwin"
controller = webbrowser.get("safari")
elif(re.search("os/2",sys.platform,re.IGNORECASE)):
print "os is os/2"
controller = webbrowser.get("safari")
else:
print "os recognized, trying to get default browser."
controller = webbrowser.get()
else:
controller = None
return (twMap,controller)
def updateMap(twMap,ofile,cnew,drawFlag,cStr):
"""Update map with a new tweet's coordinates"""
twMap.addradpoint(cnew[0],cnew[1],60000,cStr)
if(drawFlag):
twMap.draw(os.getcwd() + "/" + ofile)