Skip to content

Instantly share code, notes, and snippets.

@marcolino
Last active December 27, 2015 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcolino/7337608 to your computer and use it in GitHub Desktop.
Save marcolino/7337608 to your computer and use it in GitHub Desktop.
Downloads a comics strip image (from "http://www.gocomics.com")
#!/usr/bin/perl
#
# Download a comics strip
use strict;
use warnings;
use CGI qw(:all);
use LWP::UserAgent;
use File::Type;
my $url = "http://www.gocomics.com";
my $ua = "Mozilla/8.0";
my $q = CGI->new;
my $stripname = $q->param('strip');
my $lwp = LWP::UserAgent->new();
$lwp->agent($ua);
my $content;
my $response;
$response = $lwp->get("$url/$stripname");
if ($response->is_success) {
$content = $response->content;
} else {
error($response->status_line);
}
$content =~ /class="strip" src="([^"]*?)" \/>/;
my $stripurl = $1;
$stripurl =~ s/width=900/height=240/;
$response = $lwp->get($stripurl);
if ($response->is_success) {
$content = $response->content;
} else {
error("Error getting strip (" . $response->status_line . ")");
}
my $ft = File::Type->new();
my $type = $ft->checktype_contents($content);
print $q->header(-type => $type);
print $content;
exit 0;
sub error {
my $msg = shift;
print $q->header(-type => 'text/html');
print $msg;
exit -1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment