#!/usr/bin/perl # Copyright (c) Kim Holburn 2004 # based on an idea from Steve Hanley # # Download from : # http://www.holburn.net/software/anusearch.perl.txt # # released under the GPL v2 http://www.gnu.org/copyleft/gpl.html @type = qw(Phone+List Staff+Email Student+Email); $query=$1; sub fail_usage { if (scalar @_) { print "$0: Error: \n"; map {print " $_\n";} @_; } print < - number of columns (default = 70 characters) -1 - 1 line per entry (if -1 is specified text will be truncated) more than one -v = verbose (not very interesting!) $0 -h|--help show this screen EOM exit ; } my $type=""; my $verbose=0; my $multiline=1; $columns=70; if (scalar @ARGV < 1) { fail_usage ("no arguments"); } while ($ARGV=$ARGV[0]) { if ($ARGV =~ /^(-p|--phone)$/ ) { $type = $type[0]; } elsif ($ARGV =~ /^(-e|--email)$/ ) { $type = $type[1]; } elsif ($ARGV =~ /^(-s|--student)$/ ) { $type = $type[2]; } elsif ($ARGV =~ /^(-v|--verbose)$/ ) { $verbose++; } elsif ($ARGV =~ /^(-1)$/ ) { $multiline=0; } elsif ($ARGV eq '-c' ) { shift @ARGV; if (!($ARGV = $ARGV[0])) { fail_usage ("-c needs an argument"); } if ($ARGV =~ /^-[^1]/) { fail_usage ("-c must be followed by an argument"); } if ($ARGV =~ /[^0-9-]/) { fail_usage ("columns must be an integer"); } if ($ARGV != -1 && $ARGV < 20) { fail_usage ("columns must be larger than 20"); } $columns = $ARGV; } elsif ($ARGV =~ /^(-h|--help)/ ) { fail_usage; } elsif ($ARGV =~ /^-/ ) { fail_usage " invalid option ($ARGV)"; } else { last ; } shift @ARGV; } if (scalar @ARGV < 1) { fail_usage ("no names"); } #print "args were : "; #if ($type) { print " type=($type)\n"; } #print "additional arguments were: ", join (':', @ARGV), "\n"; sub printw { my ($str1, $str2, $str3) = @_; use Text::Wrap qw($columns &wrap); if ($multiline) { print "$str1 $str2\n"; print wrap (" "x8, " "x8, $str3), "\n" ; } else { if (0 < $columns) { my $output = wrap ("", "", "$str1 $str2 $str3") ; $output =~ s/\n.*$//s; print $output, "\n"; } else { print "$str1 $str2 $str3\n"; } } } if (!$type) { $type = $type[0]; } #if ($type) { print " type=($type)\n"; } undef $/; my $http; for (@ARGV) { if (/[^a-z0-9_. '-]/i) { fail_usage ("invalid characters in name ($_)"); } $name=$_; $name =~ s/\s+/+/g; $name =~ s/'/%27/g; $url="http://www.anu.edu.au/dirs/csearch.php?stype=$type\&querytext=$name\&submit=Search"; # print "debug url=$url\n"; if ($verbose) { print "getting url=($url)\n"; } # BEGIN { # if (eval "use LWP::Simple;") { # $http=LWP::Simple::get ($url); # if ($verbose) { print "used LWP\n"; } # } elsif (open URL, "curl -s \"$url\"|") { if (open URL, "curl -s \"$url\"|") { $http = ; close URL; if ($verbose) { print "used curl \n"; } # if ($verbose) { print "used curl debug:\n $http\n"; } } else { print "Couldn\'t open curl\n"; next; } if (!$http) { print "nothing in url\n"; } # } if ($type eq $type[0]) { my $count = 0; $http =~ s/^.*Go to Telephone Home page//; while ($http =~ m#[0-9]+[0-9 ]*\ \ #) { $http =~ s#([0-9]+[0-9 ]*)\ \ ([^<>]+)([^<>]+)##; printw ($1, $2, $3); $count++; } if ($count == 0) { print "nothing found\n"; next; } } elsif ($type eq $type[1]) { @things = split /.*$//s; print "$_\n"; } @things } elsif ($type eq $type[2]) { my $count = 0; while ($http =~ /]*)'>##; # if ($count == 0) { printf "%30s %s\n", "Student Name", "Email"; } printf "%30s %-40s\n", $1, $2; $count++; } if ($count == 0) { print "nothing found\n"; next; } } } __END__ http://www.anu.edu.au/dirs/csearch.php?stype=Phone+List&querytext=smith&submit=Search #54670  Mr Adam SmithCentre for Resource and Environmental Studies, W K Hancock Bldg, 043 http://www.anu.edu.au/dirs/csearch.php?stype=Staff+Email&querytext=smith&submit=Search http://www.anu.edu.au/dirs/csearch.php?stype=Student+Email&querytext=smith&submit=Search