-
Notifications
You must be signed in to change notification settings - Fork 1
/
geo_twitter_lookup.py
105 lines (72 loc) · 3.04 KB
/
geo_twitter_lookup.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# System imports
import json
# Third-party dependencies
from geopy import Nominatim
import requests
import twython as tw
from motionless import DecoratedMap, LatLonMarker
WHITE_PAGES_API_KEY = 'ea04b1f16b56a1b1a21b0159b8b1990e'
TWITTER_KEY = 'YABlxDeSUvuJGLcJoGsuFpCvA'
TWITTER_SECRET = 'ALttKP1BNhsRqvGL2PR9mkcGwdgh3gKs05v8pfjJrLElrxjQ8L'
TWITTER_TOKEN = '4840597894-HxW7lZoZQIbmkEHTjfaeb9wCDVZyLNPxAvczMZr'
TWITTER_TOKEN_SECRET = 'ULfOa5W84nJCj5mEvULVqtmTDFWI2x8ooqgvzNonVqhIR'
SEARCH_RADIUS = '100' # tweet search radius
SEARCH_UNITS = 'mi'
TWITTER = tw.Twython(
TWITTER_KEY, TWITTER_SECRET, TWITTER_TOKEN, TWITTER_TOKEN_SECRET)
def query_white_pages(phone_number):
'''
Returns the address of an input phone number
'''
req = 'https://proapi.whitepages.com/2.1/phone.json?api_key=%s&phone_number=%s' % (WHITE_PAGES_API_KEY,
phone_number)
result = requests.get(req)
asDict = json.loads(result.text)
locationValues = asDict['results'][0]['best_location']
locKeys = ['standard_address_line1', 'standard_address_line2',
'city', 'standard_address_location']
streetAddr = ''
for name in locKeys:
streetAddr += locationValues[name] + \
' ' if locationValues[name] else ''
return streetAddr
def create_twitter_map(coordinates):
dmap = DecoratedMap()
for coordinate in coordinates:
if coordinate is None:
continue
lon = coordinate[0]
lat = coordinate[1]
tweet_marker = LatLonMarker(lat=lat, lon=lon)
dmap.add_marker(tweet_marker)
return dmap.generate_url()
def search_twitter(phone_number=None, keywords=None):
if phone_number is None or not phone_number.isdigit():
return ValueError('Invalid phone #')
if keywords is '' or keywords is None:
return ValueError("No keywords provided")
streetAddr = query_white_pages(phone_number)
geolocator = Nominatim()
location = geolocator.geocode(streetAddr)
if location is None:
return ValueError('Invalid phone #')
lat, lon = location.latitude, location.longitude
tweets = []
tweet_coordinates = []
geocode = '%s,%s,%s%s' % (lat, lon, SEARCH_RADIUS, SEARCH_UNITS)
keywords = keywords.split(',')
for keyword in keywords:
result = TWITTER.search(q=keyword, count=100, geocode=geocode)
num_tweets = len(result['statuses'])
for tweet in range(num_tweets):
tweet_text = result['statuses'][tweet]['text']
name = result['statuses'][tweet]['user']['screen_name']
coordinates = result['statuses'][tweet]['coordinates']
if coordinates is not None:
coordinates = coordinates['coordinates']
tweets.append(["@%s: %s %s" % (name, tweet_text, coordinates)])
tweet_coordinates.append(coordinates)
map_url = create_twitter_map(tweet_coordinates)
return tweets, map_url
if __name__ == '__main__':
search_twitter('6176354500', 'sex')