-
Notifications
You must be signed in to change notification settings - Fork 0
/
highrise.py
52 lines (45 loc) · 1.48 KB
/
highrise.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
import os
from pyrise import Highrise, Person, Company
from geopy import geocoders
def run():
"""
Pulls down all people and companies in the highrise account specified
by the HIGHRISE_SERVER and HIGHRISE_AUTH_TOKEN env vars
and returns a dictionary in the format:
{
<id>: {
'name': <name>
'lat': <lat>,
'lng': <lng>
},
...
}
"""
HIGHRISE_SERVER = os.environ.get('HIGHRISE_SERVER')
HIGHRISE_AUTH_TOKEN = os.environ.get('HIGHRISE_AUTH_TOKEN')
if not HIGHRISE_SERVER and HIGHRISE_AUTH_TOKEN:
raise Exception(
'HIGHRISE_SERVER and HIGHRISE_AUTH_TOKEN are required env vars'
)
Highrise.set_server(HIGHRISE_SERVER)
Highrise.auth(HIGHRISE_AUTH_TOKEN)
gn = geocoders.GeoNames()
data = {}
for x in Person.all() + Company.all():
cd = x.contact_data
zipcode = None
if cd.addresses:
a = cd.addresses[0]
zipcode = a.zip
if zipcode:
if type(x) == Person:
name = '%s %s (%s)' % (x.first_name, x.last_name, x.company_name)
elif type(x) == Company:
name = x.name
try:
print 'Finding lat/lng for %s' % name
place, (lat,lng) = gn.geocode(zipcode)
data[x.id] = {'name': name, 'lat': lat, 'lng': lng}
except Exception:
pass
return data