-
Notifications
You must be signed in to change notification settings - Fork 2
/
fetch-n-update-urlsrc.pl
executable file
·73 lines (58 loc) · 1.34 KB
/
fetch-n-update-urlsrc.pl
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
#!/usr/bin/env perl
use strict;
use warnings;
use autodie;
use Getopt::Long;
use LWP::UserAgent;
require HTTP::Status;
#use HTTP::Request;
sub usage {
die <<END;
Usage: $0 [-n | --dryrun] URLSRC
This tiny script fetches contents from a url found in URLSRC
and print it to stdout.
When given url returned "302 Moved Permanently",
this command *UPDATES* URLSRC file
(unless -n option is given).
END
}
{
GetOptions("n|dryrun" => \ (my $o_dryrun)
, "m|maxredirects=i" => \ (my $o_max_redirects = 5)
)
or usage();
my $urlsrc = shift
or usage();
chomp(my $urlStr = do {open my $fh, '<', $urlsrc; local $/; <$fh>});
my $ua = LWP::UserAgent->new;
my $response = $ua->get($urlStr);
{
my @res = $response->redirects;
my $new_url;
while (@res and is_permanent_redirect($res[0])) {
my $res = shift @res;
$new_url = $res->header('Location');
}
if ($new_url) {
if ($o_dryrun) {
print STDERR "# Redirected to URL: $new_url\n";
} else {
safe_write_file($urlsrc, "$new_url\n");
}
}
}
print $response->content;
}
sub is_permanent_redirect {
my ($response) = @_;
$response->code == &HTTP::Status::RC_MOVED_PERMANENTLY
}
sub safe_write_file {
my ($fn, $content) = @_;
my $tmpFn = "$fn.tmp$$";
{
open my $fh, '>', $tmpFn;
print $fh $content;
}
rename($tmpFn, $fn);
}