HTTP Loader

 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
#!/bin/usr/perl
use LWP::UserAgent;
use HTTP::Response;
use HTTP::Headers;
use HTTP::Date;
$url = @ARGV[0];
$fname = @ARGV[1];
if (@ARGV < 2) {
print "Usage: URL FILE\n", "Checks URL for updates and saves it to FILE\n";
exit(1);
}
$ua = LWP::UserAgent->new;
$mtime = 0;
do {
$response = $ua->get($url);
die "Can't get $url\n", "Response tells: ", $response->status_line, "\n"
unless $response->is_success;
$ctime = str2time($response->header('Last-Modified'));
if ($ctime != $mtime) {
$mtime = $ctime;
open F, ">".$fname
or die "Can't open $fname\n";
print F $response->decoded_content();
close F;
print "$fname updated\n";
}
sleep(1);
} while(1);