-
Notifications
You must be signed in to change notification settings - Fork 6
/
crawl.php
executable file
·277 lines (215 loc) · 7.92 KB
/
crawl.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
<?php
// His purpose is to regenerate all existing images
// The process is to first regenerate all HTML files
// And then to trigger a write_png of all HTML files
include("config.php");
include("sources/esm.php");
global $baseURI;
$baseURI = $_GLOBAL['baseURI'];
//$action = $_GET['action'];
//if(!$action) render_ui();
start();
function render_ui() {
}
function start() {
global $argv;
$start = $argv[1];
$end = $argv[2];
echo "Beginning crawl between $start and $end\r\n";
// get a list of all HTML files
$htmls = crawl("prod_exports/html/");
$i=1;
foreach($htmls as $html) {
$i++;
if($i<$start) continue;
$result = process($html);
if(!$result) echo("Failed to pre-processs $html\r\n");
else echo "\r\n"; // success is implied
if($i >= $end) exit;
}
var_dump($html);
}
function process($html) {
global $baseURI;
$url = $baseURI . "/prod_exports/html/" . $html;
debug("My local HTML copy (old data) <a href='$url'>$html</a><br />");
debug("See if it exists at <a href='https://everysinglemonth.org/exports/png/".substr($html,0,-5).".png'>online</a><br />");
// identify city name
preg_match("/.+?(?=_)/",$html,$city);
$city = $city[0];
//$city = "16000US2255000";
// identify image hash
preg_match("/(?<=_)[^\.]+/",$html,$image);
$imagehash = $image[0];
debug("Found city: $city");
debug("Found image hash: $imagehash");
// get the relevant google cache data
$google_json = find_google($city);
//debug("I have the data. <pre>$google_json</pre>");
$image_url = match_hash($google_json, $imagehash);
if(!$image_url) return false;
debug("Image URL determined to be <a href='$image_url'><tt>$image_url</tt></a>");
// next I should run the HTML writer again with new data
// it lives at index.php?action=doPNG&id=16000US2743000&src='+imageSrc+debugStr
$writeurl = "http://old.everysinglemonth.org/index.php?action=doPNG&id=$city&src=$image_url";
$verifyurl = "http://old.everysinglemonth.org/exports/png/".$city."_".$imagehash.".png";
//echo "Should request this URL: $writeurl \r\n";
//echo "Should verify at this URL: $verifyurl \r\n";
reproduce($writeurl, $verifyurl);
//return(array($writeurl,$verifyurl));
debug("Click here to regenerate: <a href='$writeurl'>$writeurl</a><hr />");
//$google_cache = file_get_contents("cache/google/".);
return true;
}
function mstime() {
return round(microtime(true) * 1000);
}
function reproduce($writeurl, $verifyurl, $attempt=0) {
if($attempt > 3) {
echo "FATAL - reached attempt number $attempt. Aborting. \r\n";
die;
}
$acceptable_delay = 8000; // time in miliseconds. If we don't get a response within this time, assume failure and try again
$starttime = mstime();
//echo "The time is $starttime, now sleeping\r\n";
if($attempt>0) echo "Note: on attempt # ". $attempt . "\r\n";
echo "Requesting $writeurl \r\n";
//sleep(rand(0,1));
$is_ok = http_response($writeurl);
//sleep(4);
//$is_ok = TRUE;
if(!$is_ok) {
//echo "Fail: got an HTTP error $is_ok with that URL. Trying again... \r\n";
//reproduce($writeurl,$verifyurl,$attempt+1);
}
//sleep(100);
//echo "The time is " . mstime() . "\r\n";
$time_elapsed = mstime() - $starttime;
if($time_elapsed > $acceptable_delay) {
//echo "You were asleep for $time_elapsed, which is longer than $acceptable_delay s, this is bad\r\n";
$message = "Failed to get a response within $acceptable_delay (took $time_elapsed). Aborting \r\n";
echo $message;
file_put_contents("errors.log","$writeurl - $message",FILE_APPEND);
//reproduce($writeurl,$verifyurl,$attempt+1);
} else {
$acceptable_size = 100000;
//echo "The write request happened in $time_elapsed, which is less than than $acceptable_delay s, continuing as normal\r\n";
echo "Write completed in $time_elapsed.\r\n";
echo "Checking image at $verifyurl \r\n";
//sleep(2);
$size = get_remote_size($verifyurl);
echo "Remote size is $size bytes \r\n";
if($size <$acceptable_size) {
echo "Error: filesize was only $size, which is less than $acceptable_size. Failing.";
//reproduce($writeurl,$verifyurl,$attempt+1);
die;
} else {
echo "Verification complete. Advancing to next image... \r\n";
}
}
}
function http_response($url, $status = null, $wait = 3)
{
$time = microtime(true);
$expire = $time + $wait;
// we fork the process so we don't have to wait for a timeout
$pid = pcntl_fork();
if ($pid == -1) {
die('could not fork');
} else if ($pid) {
// we are the parent
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_NOBODY, TRUE); // remove body
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$head = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if(!$head)
{
return FALSE;
}
if($status === null)
{
if($httpCode < 400)
{
return TRUE;
}
else
{
return FALSE;
}
}
elseif($status == $httpCode)
{
return TRUE;
}
return FALSE;
pcntl_wait($status); //Protect against Zombie children
} else {
// we are the child
while(microtime(true) < $expire)
{
sleep(0.5);
}
return FALSE;
}
}
function get_remote_size($url) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_NOBODY, TRUE);
$data = curl_exec($ch);
$size = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
curl_close($ch);
return $size;
}
function match_hash($json, $imagehash) {
$json = json_decode($json,TRUE);
//var_dump($json);
foreach($json['image_results'] as $image_result) {
$sourceUrl = $image_result['sourceUrl'];
$thishash = hash_image($sourceUrl);
//debug("Checking to see if the hash I'm looking for <tt>$imagehash</tt> matches <tt>$thishash</tt>");
if($imagehash == $thishash) {
//debug("Found the match! <tt>$sourceUrl</tt> matches $thishash");
return $sourceUrl;
}
//debug("Checking ".$image_result['sourceUrl']);
}
debug("No match found for $imagehash :-(");
return false;
}
function find_google($city) {
global $baseURI;
debug("Attempting to find google data for $city");
$path = "prod_cache/google/";
$googles = crawl($path);
$i = 0;
foreach($googles as $google) {
preg_match("/.+?(?=-)/",$google,$result);
//debug("Check to see if $city matches $google (".$result[0]);
if($result[0] == $city) {
$url = $url = $baseURI . "/" . $path . $google;
debug("I found a matching Google cache for that city at <a href='$url'><tt>$google</tt></a>");
$json = file_get_contents($path . $google);
return $json;
}
//if($i>0) exit;
}
}
function crawl($path) {
debug("Crawling <tt>$path</tt>");
global $baseURI;
$files = scandir_by_mtime($path);
/*$i = 1;
foreach($files as $file) {
if($i > 8) break;
$url = $baseURI . "/" . $folder . "" . $file;
$out .= str_replace("{URL}",$url,$image_gallery_HTML);
$i++;
}*/
return $files;
}