diff options
author | Daniel Friesel <derf@derf.homelinux.org> | 2010-05-20 20:17:23 +0200 |
---|---|---|
committer | Daniel Friesel <derf@derf.homelinux.org> | 2010-05-20 20:17:23 +0200 |
commit | f962ebcf7e6a04040199d16556bbae492856e20a (patch) | |
tree | af300b3604aa5ba9cee4db3f8bc0fab44b1686af | |
parent | 3bcc67a36f854a15079f1f120ab496e57f744dcd (diff) |
Add basic image download. Tested & working with XKCD and explosm
-rwxr-xr-x | bin/comirror | 50 |
1 files changed, 41 insertions, 9 deletions
diff --git a/bin/comirror b/bin/comirror index 7f65745..1239741 100755 --- a/bin/comirror +++ b/bin/comirror @@ -9,6 +9,15 @@ my $mech = WWW::Mechanize->new( stack_depth => 0, ); +my $uri = shift || read_file('last_uri'); +my $image_re = read_file('image_re'); + +if (not defined $uri or not defined $image_re) { + die("last_uri or image_re not found / specified\n"); +} + +$image_re = qr{$image_re}; + sub find_next_link { foreach my $re ( qr{ ^ next $ }ix, @@ -23,6 +32,34 @@ sub find_next_link { die("Cannot find next link\n"); } +sub find_image { + my $image = $mech->find_image(url_abs_regex => $image_re); + + if ($image) { + my $tmpmech = WWW::Mechanize->new(); + $tmpmech->get($image->url_abs); + return $tmpmech; + } + return; +} + +sub get_image { + my $tmpmech = find_image() or return; + my $filename = (split(qr{/}o, $tmpmech->uri->as_string))[-1]; + + if (-e $filename) { + say "img: $filename (skipped)"; + } + else { + say "img: $filename"; + open(my $fh, '>', $filename) or die("Cannot open $filename: $!\n"); + print {$fh} $tmpmech->content(); + close($fh) or die("Cannot close $filename: $!\n"); + } + + return; +} + sub read_file { my ($filename) = @_; my ($line, $fh); @@ -46,13 +83,6 @@ sub save_lasturi { return; } -my $uri = shift || read_file('last_uri'); -my $image_re = read_file('image_re'); - -if (not defined $uri or not defined $image_re) { - die("last_uri or image_re not found / specified\n"); -} - $SIG{INT} = sub { save_lasturi(); exit(0); @@ -64,8 +94,9 @@ while ( and $mech->status() == 200 ) { - say $mech->uri->as_string; - say $uri; + say "URI: $uri"; + + get_image; $uri = find_next_link->URI->abs->as_string; @@ -74,6 +105,7 @@ while ( die("Looks like we're in a loop, bailing out\n"); } + print "\n"; sleep(1); } |