Category:
Author/Contact Info giant ( /msg giant )
Description: Hello monks!

I've been bored last night and did a script that fetches data of a website that hosts "quake"-demos (it's a 3d-computer-game for those of you who don't know), then prints that data out and downloads specific demos, unzips them and puts them in a folder. It's working pretty good but I'm wondering if there are some things that could be improved, optimized, made more elegant etc. so I'd like you to have a short look at it...
#!/usr/bin/perl -w
use strict;
use LWP::Simple;
use Cwd;
use File::Copy;

my $workdir    = $ENV{'HOME'};
my $qizmodir   = '/quake/qizmo';
my $demodir    = '/quake/qw/demos';

my $auto_unzip          = 1;
my $auto_qizmo_decomp   = 1;
my $auto_mvd_check      = 1;
my $auto_move           = 1;

if    (!$ARGV[0] || $ARGV[0] eq 'last')   { showdemos('new'); }
elsif ($ARGV[0] eq 'find')                { showdemos('find',$ARGV[1],
+0); }
elsif ($ARGV[0] eq 'getall')              { print "Downloading all dem
+os of $ARGV[1]\n\n"; showdemos('get', $ARGV[1]); }
elsif ($ARGV[0] eq 'get')                 { getdemo($ARGV[1]); }
elsif ($ARGV[0] eq 'getnew')              { showdemos('getnew', $ARGV[
+1]); }

sub getdemo {
   my $demo = shift;
   my ($content, @unzipinfo);
   print "Looking for demo nr $demo...\n";
   unless ($content = get("http://www.challenge-tv.com/demos/?demo=$de
+mo&dl=$demo")) {
      warn "Couldn't get infos for $demo: $! (maybe site is down?)\n";
+ return;
   }
   foreach (split(/\n/, $content)) {
      if (/\<meta\shttp-equiv=\"refresh\"\scontent=\"0/) {
         my ($thingy) = $_ =~ /URL=(.*?)\"\>$/;
         print "Downloading file  $thingy...\n";
         my ($file) = $thingy =~ /.*\/(.*?)$/;
         unless (getstore($thingy, "$workdir/$file")) {
            warn "Error while downloading $file: $!\n"; return;
         }
         print "Saved demo as $workdir/$file..\n";
         if ($auto_unzip == 1 && $file =~ /\.zip$/) {
            print "It's a zip-file unzipping...\n";
            unless (@unzipinfo = qx(unzip $workdir/$file -d $workdir))
+ {
               warn "Error while unzipping $file: $!"; return;
            }
            foreach (@unzipinfo) {
               if (/inflating/) {
                  unlink("$workdir/$file");
                  ($file) = $_ =~ /.*\/(.*?)$/;
                  while ($file =~ /\s$/) { chop($file); }
               }
            }
         }
         if ($auto_qizmo_decomp == 1 && $file =~ /\.qwz$/) {
            print "It's qizmo-compressed! uncompressing...\n";
            chdir($qizmodir);
            unless (qx(./qizmo -D $workdir/$file)) {
               warn "Error while decompressing $file: $!\n"; return;
            }
            unlink("$workdir/$file");
            $file =~ s/\.qwz/\.qwd/g;
         }
         if ($file =~ /\.mvd$/ && $auto_mvd_check == 1) {
            print 'doh! it\'s a MVD! Only M$-slaves can watch it :('."
+\n";
         }
         if ($auto_move == 1) {
            $demodir =~ s/\/$//g;
            print "Moving downloaded file to $demodir/$file\n";
            unless (move("$workdir/$file", "$demodir/$file")) {
               warn "Error while moving $file: $!"; return;
            }
         }
         print "Done demo nr $demo.!\n";
      }
   }
}

sub showdemos {
   my ($type, $findme, $next) = @_;
   my (@reloaddemos, @infos, $thingy, $infos, $trigger);
   $next = +50 if $type ne 'new';

   if ($type eq 'find' || $type eq 'get') {
      $infos   = get("http://www.challenge-tv.com/demos/?team=$findme"
+);
      @infos   = grep {/\"demoList\"/} split(/\n/, $infos);
   } elsif ($type eq 'more' || $type eq 'getmore') {
      $infos   = get("http://www.challenge-tv.com/demos/?din=$next&tea
+m=$findme");
      @infos   = grep {/\"demoList\"/} split(/\n/, $infos);
   } elsif ($type eq 'new' || $type eq 'getnew') {
      @infos   = grep {/\"demoList\"/} split(/\n/, get('http://www.cha
+llenge-tv.com/demos/?game=2'));
   }

   print " TEAM 1".(" " x 21)."TEAM 2".(" " x 21)."POV".(" " x 23)."MA
+P".(" " x 25)."C/DLs".(" " x 21)."NR\n" if $type !~ /get/;
   print " ~~~~~~".(" " x 21)."~~~~~~".(" " x 21)."~~~".(" " x 23)."~~
+~".(" " x 25)."~~~~~".(" " x 21)."~~\n" if $type !~ /get/;



   while (@infos) {
      local($_) = shift @infos;
      if ($_ =~ /Advanced\sSearch/) {
         while (@infos) {
            for my $nr (1..8) {
               while (@infos) {
                  local $_ = shift @infos;
                  if ($nr == 4) {
                     ($thingy) = $_ =~ /\"demoList\"\>.*?map=.*?\>(.*?
+)\<.*?\<\/SPAN\>/;
                  } elsif ($nr == 8) {
                     ($thingy) = $_ =~ /\"demoList\"\>.*href=\"\?demo=
+(.*?)\"\>.*\<\/SPAN\>/;
                     if ($type eq 'getnew' && $trigger && $trigger == 
+1) { print "Ah, ".$thingy."\n"; $trigger = 0; }
                     if ($type eq 'get' || $type eq 'getmore') { getde
+mo($thingy); }
                  } elsif ($nr != 5 && $nr != 6) {
                     ($thingy) = $_ =~ /\"demoList\"\>(.*?)\<\/SPAN\>/
+;
                     if ($thingy =~ m/reload/ig && $type eq 'new') { p
+ush(@reloaddemos, $thingy); }
                     if ($findme && $thingy =~ m/$findme/i) { $trigger
+ = 1; }
                  }
                  print $thingy.(" " x (27 - length($thingy))) if $nr 
+!= 5 && $nr != 6 && $type !~ /get/;
                  last;
               }
            }
            print "\n" if $type !~ /get/;
         }
      }
   }
   if ($infos && grep {/\?din=$next&team=$findme/} split(/\n/, $infos)
+) {
      print "\nThere seem to be even more demos of $findme...\n\n" if 
+$type !~ /get/;
      $type =~ /get/ ? showdemos('getmore', $findme, $next) : showdemo
+s('more', $findme, $next);
   }
   if (@reloaddemos) {
      print "\n\n**** OMFG! FOUND SOME (NEW?) RELOAD STUFF! ****\n";
   }
}

Added readmore tag - dvergin 2002-05-19