-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser.php
321 lines (295 loc) · 7.74 KB
/
parser.php
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
<?php
/* A simple wrapper for cURL. */
function http_retrieve($url)
{
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_HEADER, 0);
ob_start();
curl_exec ($ch);
curl_close ($ch);
$string = ob_get_contents();
ob_end_clean();
return $string;
}
/* This is a list of localities and their associated VDACS IDs, as found in the source of the
* Dangerous Dog Registry home page.
*/
$localities = array(
'1' => 'Accomack County',
'3' => 'Albemarle County',
'5' => 'Alleghany County',
'7' => 'Amelia County',
'9' => 'Amherst County',
'11' => 'Appomattox County',
'13' => 'Arlington County',
'15' => 'Augusta County',
'17' => 'Bath County',
'19' => 'Bedford County',
'21' => 'Bland County',
'23' => 'Botetourt County',
'25' => 'Brunswick County',
'27' => 'Buchanan County',
'29' => 'Buckingham County',
'31' => 'Campbell County',
'33' => 'Caroline County',
'35' => 'Carroll County',
'36' => 'Charles City County',
'37' => 'Charlotte County',
'41' => 'Chesterfield County',
'510' => 'City Of Alexandria',
'515' => 'City Of Bedford',
'520' => 'City Of Bristol',
'530' => 'City Of Buena Vista',
'540' => 'City Of Charlottesville',
'550' => 'City Of Chesapeake',
'570' => 'City Of Colonial Heights',
'580' => 'City Of Covington',
'590' => 'City Of Danville',
'595' => 'City Of Emporia',
'600' => 'City Of Fairfax',
'610' => 'City Of Falls Church',
'620' => 'City Of Franklin',
'630' => 'City Of Fredericksburg',
'640' => 'City Of Galax',
'650' => 'City Of Hampton',
'660' => 'City Of Harrisonburg',
'670' => 'City Of Hopewell',
'678' => 'City Of Lexington',
'680' => 'City Of Lynchburg',
'683' => 'City Of Manassas',
'685' => 'City Of Manassas Park',
'690' => 'City Of Martinsville',
'700' => 'City Of Newport News',
'710' => 'City Of Norfolk',
'720' => 'City Of Norton',
'730' => 'City Of Petersburg',
'735' => 'City Of Poquoson',
'740' => 'City Of Portsmouth',
'750' => 'City Of Radford',
'760' => 'City Of Richmond',
'770' => 'City Of Roanoke',
'775' => 'City Of Salem',
'790' => 'City Of Staunton',
'800' => 'City Of Suffolk',
'810' => 'City Of Virginia Beach',
'820' => 'City Of Waynesboro',
'830' => 'City Of Williamsburg',
'840' => 'City Of Winchester',
'43' => 'Clarke County',
'45' => 'Craig County',
'47' => 'Culpeper County',
'49' => 'Cumberland County',
'51' => 'Dickenson County',
'53' => 'Dinwiddie County',
'57' => 'Essex County',
'59' => 'Fairfax County ',
'61' => 'Fauquier County',
'63' => 'Floyd County',
'65' => 'Fluvanna County',
'67' => 'Franklin County',
'69' => 'Frederick County',
'71' => 'Giles County',
'73' => 'Gloucester County',
'75' => 'Goochland County',
'77' => 'Grayson County',
'79' => 'Greene County',
'81' => 'Greensville County',
'83' => 'Halifax County',
'85' => 'Hanover County',
'87' => 'Henrico County ',
'89' => 'Henry County',
'91' => 'Highland County',
'93' => 'Isle Of Wight County',
'95' => 'James City County',
'99' => 'King George County',
'101' => 'King William County',
'97' => 'King And Queen County',
'103' => 'Lancaster County',
'105' => 'Lee County',
'107' => 'Loudoun County',
'109' => 'Louisa County',
'111' => 'Lunenburg County',
'113' => 'Madison County',
'115' => 'Mathews County',
'117' => 'Mecklenburg County',
'119' => 'Middlesex County',
'121' => 'Montgomery County',
'125' => 'Nelson County',
'127' => 'New Kent County',
'131' => 'Northampton County',
'133' => 'Northumberland County',
'135' => 'Nottoway County',
'137' => 'Orange County',
'139' => 'Page County',
'141' => 'Patrick County',
'143' => 'Pittsylvania County',
'145' => 'Powhatan County',
'147' => 'Prince Edward County',
'149' => 'Prince George County',
'153' => 'Prince William County',
'155' => 'Pulaski County',
'157' => 'Rappahannock County',
'159' => 'Richmond County',
'161' => 'Roanoke County',
'163' => 'Rockbridge County',
'165' => 'Rockingham County',
'167' => 'Russell County',
'169' => 'Scott County',
'171' => 'Shenandoah County',
'173' => 'Smyth County',
'175' => 'Southampton County',
'177' => 'Spotsylvania County',
'179' => 'Stafford County',
'181' => 'Surry County',
'183' => 'Sussex County',
'185' => 'Tazewell County',
'1001' => 'Town Of Big Stone Gap',
'1000' => 'Town Of Blackstone',
'1002' => 'Town Of Clifton Forge',
'1003' => 'Town Of Marion',
'1004' => 'Town Of South Boston',
'1005' => 'Town Of Tappahannock',
'1006' => 'Town Of Vienna',
'1007' => 'Town Of Vinton',
'1008' => 'Town Of Wytheville',
'187' => 'Warren County',
'191' => 'Washington County',
'193' => 'Westmoreland County',
'195' => 'Wise County',
'197' => 'Wythe County',
'199' => 'York County'
);
/*
* Initialize the array in which we'll store our final array of data.
*/
$dogs = array();
/*
* Iterate through each locality and store their listings as flat HTML.
*/
foreach ($localities as $id => $name)
{
$place_url = 'https://dd.va-vdacs.com/public/public.cgi?loc='.$id.'&submit=Go';
$html = http_retrieve($place_url);
preg_match_all('/Owner - ([^<]+)<br>Dog - ([^<]+)<br>([^<]+)<br>([^<]+), VA ([0-9]{5})<br><a href="([^"]+)">More Information<\/a>/', $html, $matches);
if ($matches)
{
/*
* Iterate through the URLs and prefix them properly.
*/
foreach ($matches[6] as &$url)
{
$url = 'https://dd.va-vdacs.com/public/' . $url;
}
/*
* Pivot the arrays. Right now there's one array for each regex group, and we want to pivot
* those so that we have one array for each *entry*.
*/
foreach ($matches as $group => $match)
{
if ($group === 0)
{
continue;
}
foreach ($match as $number => $field)
{
$groups = array(
1 => 'owner',
2 => 'name',
3 => 'street',
4 => 'city',
5 => 'zip',
6 => 'url');
$label = $groups[$group];
$dogs[$name][$number][$label] = trim($field);
}
}
}
unset($matches);
}
/*
* Store this listing as JSON.
*/
file_put_contents('dogs.json', json_encode($dogs));
echo '<p><a href="dogs.json">dogs.json generated</a>.</p>';
/*
* Store the listing as HTML.
*/
$html = '
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Virginia Dangerous Dog Listing</title>
<link rel="stylesheet" href="/css/reset.css" media="screen" />
<link rel="stylesheet" href="/css/master.css" media="screen" />
<style>
dl + dl {
margin-top: 2em;
}
dl {
padding: 0.5em;
}
dt {
float: left;
clear: left;
width: 100px;
text-align: right;
font-weight: bold;
}
dt:after {
content: ":";
}
dd {
margin: 0 0 0 110px;
padding: 0 0 0.5em 0;
}
</style>
<script src="https://www.google.com/jsapi?key=ABQIAAAAn01L8sl4uwWn5vTPpoEoXhSoclbV0lStNWyWkmXm7JYp1pRtdhQmMHq94Ax7asts20lRgq4acShXHw"></script>
</head>
<body>
<header>
<h1>Virginia Dangerous Dog Registry</h1>
<p>A more accessible version of the list of dogs on the
<a href="http://www.vdacs.virginia.gov/animals/dogs.shtml">Virginia Dangerous Dog
Registry</a>. Last updated on '.date('F j, Y').'.</p>
</header>
<section id="page">';
/*
* Iterate through every locality, and then every dog, to display HTML.
*/
foreach ($dogs as $locality_name => $locality)
{
$locality_anchor = str_replace(' ', '-', strtolower($locality_name));
$html .= '<section>
<h2 id="'.$locality_anchor.'">'.$locality_name.'</h2>';
/*
* Output each dog in this locality.
*/
foreach ($locality as $dog)
{
$html .= '<dl>';
foreach ($dog as $key => $value)
{
if ($key == 'url')
{
$value = '<a href="' . $value . '"> ' . urlencode($value) . '</a>';
}
$html .=
'<dt>'.$key.'</dt>
<dd>'.$value.'</dd>';
}
$html .= '</dl>';
}
$html .= '</section>';
}
$html .= '
</section>
</body>
</html>';
echo '<p><a href="index.html">index.html generated</a>.</p>';
/*
* Save the entire listing to index.html.
*/
file_put_contents('index.html', $html);
?>