-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrape.php
319 lines (272 loc) · 9.56 KB
/
scrape.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
<?php
/*
* Downloader für das Ratsinfo System von Dresden (allris,sessionet)
*
* $risdl->run_tests(); //start the build-in tests
*
* Nutzbare Script-Parameter:
*
* --trynext=2000 (optional)
* sucht in den nächsten 2000 ids nach neuen Dokumenten (2000 = Standardwert) und lädt sie herunter
*
* --diff (optional)
* sucht nach Veränderungen auf dem Server und lädt nur die veränderte Dokumente herunter
* vorhandene Dokumente werden versioniert
*
*/
$risdl = new RIS_Downloader;
// handle script parameters
$userparam = getopt('', array('trynext::', 'diff::'));
if( isset($userparam['diff']) ) {
$risdl->diff_all( $userparam['diff'] );
}
else
{
if( isset($userparam['trynext']) ) {
echo 'The next '.$userparam['trynext'].' IDs will be scaned.';
$trynext = $userparam['trynext'];
}
else
{
$trynext = 2000; //if no value for --try is given, 2000 file will be tryed out
echo 'No limit is set, the next '.$trynext.' IDs will be scaned.';
}
$risdl->search_new_files( $trynext );
}
/*
* Ratsinfomations downloader class
*
*/
class RIS_Downloader
{
private $downloadDir = 'downloads/'; //primary download folder
private $versionsDir = 'versions/'; //contains older versions of a file
private $infoFileSuffix = 'scrapeinfo'; //contains all data about a downloaded file
private $newFilesCount = 0;
private $unequal_files = array();
/*
* check differences between former downloaded files and
* the files are now online at the RIS
*
* different files will be downloaded and former files
* will be versioned
*
*/
public function diff_all( $limit )
{
$this->add_log("**** start new diff process ****");
$this->discover( 'allIds' );
$this->find_unequal_files( $limit );
$this->version_unequal_files();
$this->download_unequal_files();
}
/*
*
*/
private function version_unequal_files()
{
if( count( $this->unequal_files) > 0 )
{
if( file_exists($this->versionsDir) === false ) mkdir( $this->versionsDir );
foreach($this->unequal_files as $id => $name)
{
$name_scrapeinfo = str_replace('.pdf', '.scrapeinfo', $name);
copy( $this->downloadDir.$name, $this->versionsDir.$name );
copy( $this->downloadDir.$name_scrapeinfo, $this->versionsDir.$name_scrapeinfo );
unlink( $this->downloadDir.$name);
unlink( $this->downloadDir.$name_scrapeinfo);
}
}
}
/*
*
*/
private function download_unequal_files()
{
$bulk = count($this->unequal_files);
foreach($this->unequal_files as $id => $path)
{
$this->download( $id, $this->downloadDir );
}
echo "\nResults:";
if( $this->newFilesCount > 0 ) $this->add_log('Searched for '.$bulk.' new files, and find and download '.$this->newFilesCount.' new files.', true );
else $this->add_log( 'Searched for '.$bulk.' new files. Could not find new files.', true );
}
/*
* compare former downloaded files with the files are now online
*/
private function find_unequal_files($limit = 0)
{
$count = count($this->allKnownIds);
if( $count > 0 )
{
$xi=0;
$starttime = mktime();
$unequal = array();
ksort( $this->allKnownIds );
foreach($this->allKnownIds as $id => $PDFpath)
{
$xi++;
if($limit > 0 AND $xi > $limit) break; // limitate the scan for test behaviour
echo "\ncheck id:$id ($xi/ $count, ". round($starttime / mktime())*($count-$xi) . ' seconds remaining)' ;
$http_head = $this->download( $id, false, true ); //gets only the head of the file
$scrapeinfo = str_replace('.pdf', '.scrapeinfo', $PDFpath);
$scrapeinfo = parse_ini_file( $this->downloadDir.$scrapeinfo, false, INI_SCANNER_RAW );
if( isset($http_head['Content-Length']))
{
if( isset($scrapeinfo['size']) == false )
$scrapeinfo['size'] = $http_head['Content-Length']; //fix empty values in scrapeinfo
if($scrapeinfo['size'] != $http_head['Content-Length'])
$unequal[$id] = $PDFpath; //save unequal
}
}
echo "\nfound ".count($unequal).' unequal files';
$this->add_log("unequal files found: ".implode(',',$unequal), true);
$this->unequal_files = $unequal;
}
else
{
echo $this->add_log('Exit, because no former files where found.', true );
}
}
/*
* identify the highes document-id are there and
* search for new files by scaning for IDs above
* this highest known id
*
*/
public function search_new_files( $next )
{
if( ! $next > 0) return false;
$startId = $this->discover( 'highestId' );
echo "\nFind $startId as higest id";
for( $id=$startId; $startId+$next > $id; $id++ )
{
$this->download( $id, $this->downloadDir );
}
echo "\nResults:";
if( $this->newFilesCount > 0 ) $this->add_log('Searched for '.$next.' new files, and find and download '.$this->newFilesCount.' new files.', true );
else $this->add_log( 'Searched for '.$next.' new files. Could not find new files.', true );
}
private function discover( $flag )
/////////////////////////////
// returns the higest last id found. If no previous, return zero for starting new
// $flag for different action later
{
$higestId = 0;
$this->allKnownIds = array();
if( file_exists($this->downloadDir) === false) mkdir( $this->downloadDir );
$dh = opendir( $this->downloadDir );
while( false !== ($entry = readdir($dh)))
{
if( substr_count($entry, '.pdf') < 1 ) continue;
$nameparts = preg_split("/[_.]/", $entry); //explodes string at underline and point
$id = (int) $nameparts[1];
if($id > $higestId) $higestId = $id;
if( $flag == 'allIds')
{
$this->allKnownIds[$id] = $entry;
}
}
if( $higestId > 0) return $higestId;
else return 0;
}
/////////////////////////////
// download(id [,head(bool)] )
// try to download given file or just the head
// create file and *.info fileid or return false
/* $http_response_header:
// [0] => HTTP/1.1 200 OK
// [1] => Date: Mon, 13 Apr 2015 17:22:52 GMT
// [2] => Server: Apache/2.4.7 (Ubuntu)
// [3] => Referer: http://ratsinfo.dresden.de
// [4] => X-Powered-By: PHP/5.5.9-1ubuntu4.6
// [5] => Expires: 0
// [6] => Cache-Control: must-revalidate, post-check=0, pre-check=0
// [7] => Pragma: public
// [8] => Content-Length: 14450
// [9] => Content-transfer-encoding: binary
// [10] => Content-Disposition: attachment;filename="Einladung_-_OSR_SB.pdf"
// [11] => X-Robots-Tag: noindex
// [12] => Content-Type: application/pdf
// [13] => Set-Cookie: PHPSESSID=l1hhnpp4tlm4k2qlr87ilqt453; path=/
// [14] => Connection: close
*/
private function download( $id , $destination, $head = false)
{
$h = @fopen('http://ratsinfo.dresden.de/getfile.php?id='.$id.'&type=do','rb');
if( $h === false) return false; //id not found, end of story
if( $head === true ) //just download head
{
fclose($h);
foreach($http_response_header as $str)
{
$field = substr($str, 0, strpos($str,':'));
$value = substr($str, strpos($str,':')+1);
$http_response_header_new[$field] = $value;
}
return $http_response_header_new;
// über fread() werden immer nur die ersten bytes gehlt, dann abgebrochen, das kann zur analyse genutzt werden
}
else
{
$dl = stream_get_contents( $h );
if( $dl === false ) $this->add_log( 'can not download id '.$id );
else
{
echo "\n".'try to download id: '.$id;
$size = trim( substr( $http_response_header[8], 15) );
$OrgNname = trim( substr( $http_response_header[10], 42, -1) );
$suffix = substr( $OrgNname, strrpos($OrgNname, '.')+1 );
$now = mktime();
$filename = 'fileid_'.$id.'_'.$now; //should: fileid_00001_1428947960.pdf [bez-id-timestamp.suffix])
file_put_contents($destination.$filename.".$suffix", $dl);
$infofile =
'name = '.$OrgNname .PHP_EOL.
'size = '.$size .PHP_EOL.
'download_date = '.date('d-m-Y-H:i:s',$now)." ($now)";
file_put_contents($destination.$filename.'.'.$this->infoFileSuffix, $infofile);
$this->newFilesCount++;
}
}
fclose($h);
}
////////////////////////////////
private function add_log( $msg, $echo=false )
{
$msg = date("d.m.Y-h:i:s").': '.$msg.PHP_EOL;
file_put_contents('scrape.log', $msg, FILE_APPEND);
if($echo) echo "\n\r".$msg; //log also to console
}
////////////////////////////////
/// TESTIING SECTION \\\\
////////////////////////////////
////////////////////////////////
public function run_tests()
{
if(
$this->unit_download()
)
return true;
else return false;
}
////////////////////////////////
// downloads a known file an checks the download
private function unit_download()
{
$dir = 'testing/';
if(file_exists($dir) === false) mkdir($dir);
else {
array_map('unlink', glob( $dir.'*.*'));
}
$this->download(16490, $dir);
$files = scandir($dir);
if($files[0] && $files[1]) return true;
else {
echo "\n**** unit test fails, cannot download or write files ****";
return false;
}
}
}
print "\n"; // new line and avoid ugly console output (|%|) after execution
?>