Last active
December 27, 2015 13:59
-
-
Save marcolino/7337608 to your computer and use it in GitHub Desktop.
Downloads a comics strip image (from "http://www.gocomics.com")
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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