Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
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]
[Lady_Aleena]: He never hears it.
[marioroy]: Back in HighSchool, I came late to Gym class. There was a fellow who was picked last, no team wanted him for racketball. So it was us two against everybody. I looked at him in the eye and said to let your body loose and let the powers to be help us win.
[marioroy]: We made it all the way to the end. The expience was magical. I not good at sports. Someone or thing was there.
erix considers extracting a catalog ...
[karlgoethebier]: marioroy: Racket ball is considered harmful, like Squash
[marioroy]: Wait till folks see what MCE::Hobo can do in the upcoming release. Multiple instances, nested spawning, 3 levels, doesn't matter. it works on Windiows too.
[Lady_Aleena]: I hate sites that tell me to turn ad-blocker off. They should know better.
[marioroy]: s/Windiows/ Windows/
[Discipulus]: marioroy can you expand that Niagara's fall history?
erix only sees fallen angels

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (8)
As of 2017-05-29 08:44 GMT
Find Nodes?
    Voting Booth?