-
Notifications
You must be signed in to change notification settings - Fork 0
/
scraper.php
271 lines (227 loc) · 6.13 KB
/
scraper.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
<?php
/*
* The directory to which we're saving the JSON files, one per comment.
*/
define('OUTPUT_DIR', 'json');
/*
* Our time zone. (This is, of course, EST, but we have to define this to keep PHP from
* complaining.)
*/
date_default_timezone_set('America/New_York');
/*
* Given a string that consists of HTML, extract each field and return an object of relevant
* fields.
*/
function extract_comment($html)
{
if (empty($html))
{
return FALSE;
}
/*
* If a 404 is returned (it's actually a 200 -- the error is found only within the page
* content), then return false.
*/
if (strpos($html, 'Error: Invalid comment id specified!') !== FALSE)
{
return FALSE;
}
$comment = new stdClass();
/*
* The agency and board names are surrounded by the same HTML, so capture them in the order in
* which they appear.
*/
preg_match_all('/<div style="float: left">(\s+)(.*)(\s+)<\/div>/', $html, $matches);
if (!empty($matches[2][0]))
{
$comment->agency->name = trim($matches[2][0]);
}
if (!empty($matches[2][1]))
{
$comment->board->name = trim($matches[2][1]);
}
preg_match('/ViewAgency\.cfm\?AgencyNumber=([0-9]+)"/', $html, $matches);
if (!empty($matches[1]))
{
$comment->agency->number = $matches[1];
}
preg_match('/ViewBoard\.cfm\?BoardID=([0-9]+)"/', $html, $matches);
if (!empty($matches[1]))
{
$comment->board->id = $matches[1];
}
preg_match('/ViewChapter\.cfm\?ChapterID=([0-9]+)"/', $html, $matches);
if (!empty($matches[1]))
{
$comment->chapter->id = $matches[1];
}
preg_match('/<div style="float: left; font-weight: bold;">(\s+)(.*)(\s+)<span /', $html, $matches);
if (!empty($matches[1]))
{
$comment->chapter->name = trim($matches[2]);
}
preg_match('/222288">(\s*)\[(.*)\]<\/span>/', $html, $matches);
if (!empty($matches[2]))
{
$comment->citation = html_entity_decode(str_replace(' ', ' ', $matches[2]), 0);
$comment->citation = str_replace(' ‑ ', '‑', $comment->citation);
}
preg_match('/Commenter:<\/strong>([^<]+)/', $html, $matches);
if (!empty($matches[1]))
{
$comment->commenter = preg_replace('/\v/', '', $matches[1]);
$comment->commenter = str_replace(' ', ' ', (trim(preg_replace('/(\h{2,})/', ' ', $comment->commenter))));
}
preg_match('/padding: 4px">(.*)<\/div>/', $html, $matches);
if (!empty($matches[1]))
{
$comment->timestamp = date('c', strtotime(str_replace(' ', ' ', $matches[1])));
}
preg_match('/href="viewaction\.cfm\?actionid=([0-9]+)" class="linkblack">(.*)<\/a>/', $html, $matches);
if (isset($matches[1]))
{
$comment->action->name = $matches[2];
$comment->action->id = $matches[1];
}
preg_match('/href="viewstage\.cfm\?stageid=([0-9]+)"(( class="linkblack")?)>(.*)<\/a>/', $html, $matches);
if (isset($matches[1]))
{
$comment->stage->name = $matches[4];
$comment->stage->id = $matches[1];
}
preg_match('/Comment Period<\/strong><\/td>(\s+)<td>(\s+)Ends (.*)(\s+)<\/td>/', $html, $matches);
if (isset($matches[3]))
{
$comment->period_ends = date('c', strtotime($matches[3]));
}
preg_match('/<br><br>(\s+)<strong>(.*)<\/strong>/', $html, $matches);
if (!empty($matches[2]))
{
$comment->title = $matches[2];
}
/*
* Perform a greedy, case-insensitive match, to get all paragraphs.
*/
preg_match('/<div style="clear: right"> <\/div>(.+?)<\/div>/s', $html, $matches);
if (!empty($matches[1]))
{
$comment->comment = trim(str_replace(' ', ' ', $matches[1]));
$comment->comment = str_replace('<!--- MSIE Browser --->', '', $comment->comment);
/*
* Pipe this HTML through HTML Tidy to clean it up.
*/
$tidy = new tidy;
$config = array(
'show-body-only' => TRUE,
'drop-font-tags' => TRUE
);
$tidy->parseString($comment->comment, $config);
$tidy->cleanRepair();
if (empty($tidy->errorBuffer))
{
$comment->comment = $tidy;
}
}
return $comment;
}
/*
* Define the base URL that we'll be iterating on.
*/
$base_url = 'http://townhall.virginia.gov/L/viewcomments.cfm?commentid=';
/*
* Define the minimum and maximum comment ID. A huge maximum number is given to keep this from
* running out of control.
*/
$min = 1;
$max = 100000;
/*
* If the output directory already exists, see what the highest-numbered file is, so that we can
* resume scraping where we left off.
*/
if (file_exists(OUTPUT_DIR))
{
$files = array_slice(scandir(OUTPUT_DIR, TRUE), 0, 1);
$min = preg_replace('[^0-9]', '', $files[0]) + 1;
if ($min == '..')
{
$min = 1;
}
}
/*
* If the output directory doesn't exist, then create it.
*/
else
{
if (!mkdir(OUTPUT_DIR))
{
die('Could not create ' . OUTPUT_DIR . ' directory to save output.');
}
}
/*
* Maintain a count of the number of failed attempts to retrieve a comment. We only really care
* about sequential failures, to determine when we've finished indexing all comments, so we
* reset this to 0 with each successful retrieval.
*/
$failures = 0;
/*
* Iterate through every comment.
*/
for ($i = $min; $i <= $max; $i++)
{
/*
* Assemble the URL.
*/
$url = $base_url . $i;
/*
* Get the HTML found at that URL.
*/
$html = file_get_contents($url);
/*
* If we successfully retrieve that HTML.
*/
if ($html !== FALSE)
{
/*
* Pull the comment data out of the HTML, storing it as an object.
*/
$comment = extract_comment($html);
if ($comment === FALSE)
{
$failures++;
continue;
}
/*
* Append the comment's ID and URL.
*/
$comment->id = $i;
$comment->url = $url;
echo $comment->url."\n";
/*
* Turn the object into JSON.
*/
$comment = json_encode($comment);
/*
* Write the comment JSON to a file.
*/
file_put_contents(OUTPUT_DIR . '/' . str_pad($i, 5, '0', STR_PAD_LEFT) . '.json', $comment);
/*
* Set our count of consecutive failures to 0, since we just had a successful download.
*/
if ($failures > 0)
{
$failures = 0;
}
}
else
{
$failures++;
}
/*
* If we've had 40 consecutive failed attempts to retrieve comments, stop running.
*/
if ($failures >= 40)
{
die('<p>After 20 consecutive failed attempts to retrieve data (from 20 consecutive '
. 'URLs), aborting further attempts. The last URL tried was '. $url .'</p>');
}
}