-
Notifications
You must be signed in to change notification settings - Fork 8
/
generate_areacode_lookup_data.py
68 lines (64 loc) · 2.16 KB
/
generate_areacode_lookup_data.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
from selenium import webdriver
from selenium.webdriver.common.by import By
from app.models import AreaCodeLookup
from pyzipcode import ZipCodeDatabase
from app import db
import us
from geopy.geocoders import Nominatim
from easydict import EasyDict as edict
print("starting webdriver")
driver = webdriver.Firefox()
print("getting webpage")
driver.get("https://www.allareacodes.com/")
result = driver.find_elements(By.XPATH, "//select[@style='width: 100%; margin-right: 2px']")
area_code_and_place = result[0].text.split("\n")
prefixes = [
"New", "Los", "San", "Baton", "Fort",
"Bowling", "Lake", "Grand", "Saint",
"Charlotte"
]
zcdb = ZipCodeDatabase()
geolocator = Nominatim()
for area_code in area_code_and_place:
state = area_code.split("-")[1].split("(")[0].strip()
if "DC" in state:
state = us.states.lookup("DC").abbr
else:
state = us.states.lookup(state).abbr
city = area_code.split("-")[1].split("(")[1].rstrip(")")
city = city.strip()
if "," in city:
city = city.split(",")[0]
if " " in city:
if [prefix for prefix in prefixes if prefix in city] == []:
city = city.split(" ")[0]
if isinstance(zcdb.find_zip(city=city,state=state),list):
zip_code = zcdb.find_zip(city=city,state=state)[0]
else:
zip_code = zcdb.find_zip(city=city,state=state)
if zip_code is None:
try:
zip_code = zcdb.find_zip(state=state)[0]
except:
if state == "MP":
zip_code = edict({
"latitude":15.200755,
"longitude":145.756952
})
elif state == "GU":
zip_code = edict({
"latitude":13.463345,
"longitude":144.733168
})
else:
import code
code.interact(local=locals())
area_code = AreaCodeLookup(
area_code.split("-")[0].strip(),
city,
state,
zip_code.latitude,
zip_code.longitude
)
db.session.add(area_code)
db.session.commit()