Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
PerlMonks strip grabber

by billyjoeray (Initiate)
on Mar 15, 2000 at 01:08 UTC ( #5384=sourcecode: print w/replies, xml ) Need Help??
Category: web stuff/misc
Author/Contact Info ICQ: 15291367
Description: This script can be run on regular intervals to download the latest strips from I'm putting this here because its a little big for the front page, but for whoever reads this, I'd like some tips on how to clean up my code and some wisdom on how to easily write code with 'use scrict;' I always seem to be needing to use variables in a global sense.
#!/usr/bin/perl -w

use LWP::UserAgent;
use HTTP::Request::Common;

$numofstrips = NumofStrips();

$home = `echo \$HOME`;chop $home;$rcfile = "$home\/.stripnum";

if (-f $rcfile) {
    $stripnum = `cat ~/.stripnum`;
} else {
    print "~/.stripnum does not exist, creating...\n";
    system 'echo 1 > ~/.stripnum';
    $stripnum = `cat ~/.stripnum`;

if (-d "$home\/.strips") {
} else {
    print "directory $home\/.strips does not exist, creating...\n";
    system "mkdir $home\/.strips";
print "Should only see this once!\n";
mainblock: {
    $url = "$stripnum";
    $bot = new LWP::UserAgent;
    $response = $bot->request(GET $url);
    $content = $response->content;

    @content = split(/\n/, $content);

  content: for($i=0;$i<$#content;$i++) {
      $content[$i] =~ /.*Strip \#(.*):<.*/;
      if ($1) { $number = $1; last content; }
    if ($number != $stripnum) { 
        print "$stripnum does not seem to exhist\n";
        $numofstrips = NumofStrips();
        print "$numofstrips seems to be total number of strips\n";
        if ($stripnum == $numofstrips) {
            open (FILE, ">$rcfile") || die;
            print (FILE "$stripnum") || die;
            close FILE;
            die "Got last file exiting\n";
        else {
        print "lets try to get $stripnum\n";
        goto mainblock;
    undef $filetodl;
  searchforfiletodl: for (split "\n", $content) {
        $filetodl = "$1.jpg" if/.*img\/strips\/(.*).jpg/;
        if ($filetodl){
            #print $filetodl;
            last searchforfiletodl;
    $imgurl = "$filetodl";
    print "Downloading $imgurl\n";
    $response = $bot->request(GET $imgurl);
    $content = $response->content;

    $file = "$home\/.strips\/$number.jpg";
    print "opening $file\n";
    open (FILE, ">$file") || die;
    print (FILE "$content") || die;
    print "success!\n";
    close FILE;
    print "$stripnum\n";
    open (FILE, ">$rcfile") || die;
    print (FILE $stripnum) || die;
    close FILE;

goto mainblock;

sub NumofStrips {
    my $url = "";
    my $bot = new LWP::UserAgent;
    my $response = $bot->request(GET $url);
    my $content = $response->content;
    @content = split(/\/td/, $content);
    searchfornumofstrips: for($i=0;$i<$#content;$i++){
        $content[$i] =~ /<tr><td valign='top'><p>(.*)<\/p></;
        if($1) { $numofstrips = $1; last searchfornumofstrips; }
    return $numofstrips;

print "This isnt supposed to happen!\n"; die;
Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://5384]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (7)
As of 2016-10-25 08:35 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (315 votes). Check out past polls.