forked from sedenardi/snapchat-map
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateArray.html
More file actions
176 lines (175 loc) · 6.51 KB
/
createArray.html
File metadata and controls
176 lines (175 loc) · 6.51 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<html>
<head>
<title>Area Code Parser</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="http://netdna.bootstrapcdn.com/bootstrap/3.0.3/js/bootstrap.min.js"></script>
<link href="http://netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css" rel="stylesheet">
<script src="htmlparser.js"></script>
<script src="html2json.js"></script>
</head>
<body style="padding:20px;">
<h3>Parse Area Codes From Remote Site - <a href="view-source:http://www.allareacodes.com/area-code-list.htm" target="_blank">http://www.allareacodes.com/area-code-list.htm</a></h3>
<br />
<div>
<textarea class="form-control" id="inputArea" placeholder="Paste source from http://www.allareacodes.com/area-code-list.htm here" rows="3"></textarea>
<button type="button" class="btn btn-success" id="btnParse">Get Area Codes</button>
</div>
<br/>
<div>
<h5>JSON<span id="lenJSON"></span></h5>
<textarea class="form-control" id="outputJSONArea" rows="6"></textarea>
<button type="button" class="btn btn-success" id="btnFetchCoords">Fetch Coordinates</button>
<button type="button" class="btn btn-danger" id="btnCancel" style="display:none;">Cancel</button>
<span id="progress" style="display:none;">Progress: </span>
<br />
<h5>SQL<span id="lenSQL"></span></h5>
<textarea class="form-control" id="outputSQLArea" rows="6"></textarea>
</div>
<br/>
<div>
<h5>Manual Lookup</h5>
<div class="input-group">
<input type="text" class="form-control" id="inputManual" placeholder="Enter City, State">
<span class="input-group-btn">
<button class="btn btn-default" type="button" id="btnManual">Go!</button>
</span>
</div>
<pre id="outputManual"></pre>
</div>
</body>
</html>
<script type="text/javascript"
src="apiKey.js">
</script>
<script type="text/javascript">
var geocoder, array, interval = null;
var initialize = function() {
geocoder = new google.maps.Geocoder();
};
$('#btnParse').click(function() {
var rawHtml = $('#inputArea').val();
var innerTable = $(rawHtml).find('#main_table').find('table:eq(5)').html();
var json = html2json(innerTable).child;
array = [];
for (var i = 1; i < json.length; i++) {
var a = json[i].child[0].text;
var l1 = json[i].child[1].text.split(': ');
var l2 = l1.length > 1 ? l1[1].split(', ') : ['None'];
var o = {
areacode: a.substr(a.indexOf('<a>')+3, 3),
state: l1[0].replace('<a>','').replace('</a>',''),
cities: l2,
coords: []
};
array.push(o);
}
$('#lenJSON').html(' - ' + array.length);
$('#outputJSONArea').val(JSON.stringify(array,null,2));
$('#btnFetchCoords').show();
});
$('#btnFetchCoords').click(function() {
if (typeof gMapsAPIKey === 'undefined') {
alert('Must put Google Maps API key in apiKey.js');
return;
}
if (!$('#outputJSONArea').val().length) {
alert('Must input JSON from previous run or get area codes from above website');
return;
}
try {
array = JSON.parse($('#outputJSONArea').val());
$('#progress').show();
$('#btnCancel').show();
getNextCoord(0);
} catch(e) {
alert('Error: ' + e);
}
});
$('#btnCancel').click(function() {
if (interval === null) {
alert('Can\'t cancel, not throttled');
return;
}
clearInterval(interval);
interval = null;
$('#progress').html('Cancelled');
$('#btnCancel').hide();
});
var getNextCoord = function(i) {
if (i < array.length) {
if (!array[i].coords.length) {
if (array[i].cities[0] === 'None') {
array[i].coords.push({
city: 'None',
location: {
nb: 0,
ob: 0
}
});
getNextCoord(i+1);
} else {
var j = Math.floor((array[i].cities.length)/2);
var address = array[i].cities[j] + ', ' + array[i].state;
console.log((i + 1) + '/' + array.length + ': ' + address);
geocoder.geocode( { 'address': address }, function(results, status){
if (status === google.maps.GeocoderStatus.OK) {
$('#progress').html('Progress: ' + (i + 1) + '/' + array.length);
console.log(address + ': ' + results[0].geometry.location);
array[i].coords.push({
city: array[i].cities[j],
location: results[0].geometry.location
});
$('#outputJSONArea').val(JSON.stringify(array,null,2));
getNextCoord(i+1);
} else {
$('#progress').html('Progress: ' + (i + 1) + '/' + array.length + ' (Throttled)');
console.log('Throttling ' + (i + 1) + '/' + array.length);
interval = setTimeout(function() {
getNextCoord(i);
}, 15000);
}
});
}
} else {
getNextCoord(i+1);
}
} else {
$('#outputJSONArea').val(JSON.stringify(array,null,2));
$('#progress').html('Progress: Done.');
$('#btnCancel').hide();
makeSQL();
}
};
var makeSQL = function() {
var sql = ['Insert into goodAreacodes(areacode,state,city,lat,lon)','Values'];
for (var i = 0; i < array.length; i++) {
for (var j = 0; j < array[i].coords.length; j++) {
sql.push('(\'' + array[i].areacode + '\',\'' + array[i].state + '\',\'' + array[i].coords[j].city.replace('\'','\\\'') + '\',\'' +
array[i].coords[j].location.nb + '\',\'' +
array[i].coords[j].location.ob + '\'),');
}
}
var last = sql[sql.length-1];
last = last.substr(0,last.length-1) + ';';
sql[sql.length-1] = last;
$('#lenSQL').html(' - ' + sql.length);
$('#outputSQLArea').val(sql.join('\n'));
};
$('#btnManual').click(function(){
geocoder.geocode( { 'address': $('#inputManual').val() }, function(results, status){
if (status === google.maps.GeocoderStatus.OK) {
$('#outputManual').html(JSON.stringify(results[0].geometry.location,null,2));
} else {
$('#outputManual').html(JSON.stringify(status,null,2));
}
});
});
function loadScript() {
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'https://maps.googleapis.com/maps/api/js?key=' + gMapsAPIKey + '&sensor=false&' +
'callback=initialize';
document.body.appendChild(script);
}
window.onload = loadScript;
</script>