-
Notifications
You must be signed in to change notification settings - Fork 10
/
get_nanpa_npa.py
102 lines (93 loc) · 3.34 KB
/
get_nanpa_npa.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
import requests
import csv
from io import StringIO
from collections import defaultdict
from pprint import pformat
npa_report = 'http://www.nationalnanpa.com/nanp1/npa_report.csv'
out_file = 'phone_iso3166/nanpa.py'
r = requests.get(npa_report)
r.raise_for_status()
csvdata = StringIO(r.text)
csvdata.readline() # Read header
csvdata.readline() # Read header
npareader = csv.reader(csvdata, delimiter=',', quotechar='"')
npamap = StringIO()
npamap.write('# Generated by get_npna_npa.py\n')
npamap.write('# Based on http://nationalnanpa.com/reports/reports_npa.html\n\n')
npamap.write('npa = \\\n')
npadict = defaultdict(lambda: defaultdict(dict))
for row in npareader:
try:
# Get column 0 and 9, according to field definitions:
# http://www.nanpa.com/area_codes/AreaCodeDatabaseDefinitions.xls
npa = row[0]
assigned = row[5]
use = row[7]
location = row[8]
country = row[9]
if country == 'US':
if location == 'USVI':
country = 'VI'
elif location == 'PUERTO RICO':
country = 'PR'
elif len(location) == 2:
country = 'US'
elif not location and assigned == 'No':
country = 'US' # For NPAs reserved to US, just say US
elif not location and use == 'N':
country = 'US' # For US non-geographic codes, just say US
elif npa == '670':
# Fix Commonwealth of the Northern Mariana Islands (CNMI)
country = 'MP' # For some reason CNMI don't get a location
else:
print('Unknown location {} in US with NPA {}'
.format(location, npa))
elif country == 'CANADA':
country = 'CA'
elif country == 'BAHAMAS':
country = 'BS'
elif country == 'BARBADOS':
country = 'BB'
elif country == 'ANGUILLA':
country = 'AI'
elif country == 'ANTIGUA/BARBUDA':
country = 'AG'
elif country == 'BRITISH VIRGIN ISLANDS':
country = 'VG'
elif country == 'CAYMAN ISLANDS':
country = 'KY'
elif country == 'GRENADA':
country = 'GD'
elif country == 'BERMUDA':
country = 'BM'
elif country == 'TURKS & CAICOS ISLANDS':
country = 'TC'
elif country == 'JAMAICA':
country = 'JM'
elif country == 'MONTSERRAT':
country = 'MS'
elif country == 'SINT MAARTEN':
country = 'SX'
elif country == 'ST. LUCIA':
country = 'LC'
elif country == 'DOMINICA':
country = 'DM'
elif country == 'ST. VINCENT & GRENADINES':
country = 'VC'
elif country == 'DOMINICAN REPUBLIC':
country = 'DO'
elif country == 'TRINIDAD AND TOBAGO':
country = 'TT'
elif country == 'ST. KITTS AND NEVIS':
country = 'KN'
elif country:
print('Unknown NPA country: {} with NPA {}'.format(country, npa))
if country:
n0, n1, n2 = map(int, str(npa))
npadict[n0][n1][n2] = country
except IndexError:
pass
npamap.write(pformat({k: dict(v) for k, v in npadict.items()}))
with open(out_file, 'w') as f:
f.write(npamap.getvalue() + '\n')
print('Wrote ' + out_file)