anndl: Fixed a couple of bugs.
[utils.git] / anndata
diff --git a/anndata b/anndata
index 755c1bf..7ed3e13 100755 (executable)
--- a/anndata
+++ b/anndata
@@ -1,80 +1,70 @@
 #!/usr/bin/perl -w
 
-use LWP::UserAgent;
 use Getopt::Long;
+use Anime::ANN;
 
-sub get
-{
-    my($uri, $ua, $res);
-    ($uri) = @_;
-
-    $ua = LWP::UserAgent->new;
-    $ua->agent("ANNData/0.1");
-
-    $res = $ua->request(HTTP::Request->new("GET", "$uri"));
-
-    die "could not fetch $uri\n" unless $res->is_success;
-    return $res->content;
-}
-
-sub getlist
-{
-    my($name, $il, $html, @ret);
-    ($name) = @_;
-    
-    $il = uc(($name =~ /^(.)/)[0]);
-    $il = "9" if (!($il =~ /[A-Z]/));
-    $html = get "http://www.animenewsnetwork.com/encyclopedia/anime.php?list=$il";
-    
-    # The only way to recognize entries that seems sure is to look
-    # after the "HOVERLINE" class.
-    
-    push @ret, $1 while $html =~ /<A\s.*CLASS=HOVERLINE\s.*>.*<FONT.*>([^<>]*$name[^<>]*)<\/FONT/ig;
-    
-    return(@ret);
-}
-
-sub getid
-{
-    my($name, $il, $html, $url);
-    ($name) = @_;
-    
-    $il = uc(($name =~ /^(.)/)[0]);
-    $il = "9" if (!($il =~ /[A-Z]/));
-    $html = get "http://www.animenewsnetwork.com/encyclopedia/anime.php?list=$il";
-    
-    # The only way to recognize entries that seems sure is to look
-    # after the "HOVERLINE" class.
-    
-    (($url) = ($html =~ /<A\s.*CLASS=HOVERLINE\s.*HREF=\"([^\"]+)\".*$name/i)) || return;
-    
-    return((($url =~ /\?id=(\d+)$/)[0]));
-}
-
-sub getnamefromid
-{
-    my($id, $html);
-    ($id) = @_;
-    
-    $html = get "http://www.animenewsnetwork.com/encyclopedia/anime.php?id=$id";
-    
-    return(($html =~ /\<TITLE\>Anime News Network - ([^<]*)<\/TITLE>/)[0]);
-}
-
-GetOptions(\%options, ("l"));
+binmode STDOUT, ":utf8";
+GetOptions(\%options, "l=s", "b=s", "d") || exit 1;
 
 if($options{"l"}) {
-    @list = getlist $ARGV[0];
+    @list = Anime::ANN::getlist($options{"l"});
     foreach $name (@list) {
        print "$name\n";
     }
     exit 0;
 }
 
-unless($test = getid $ARGV[0]) {
+$browse = "";
+if($options{"b"}) {
+    $browse = $options{"b"};
+} elsif($options{"d"}) {
+    $browse = `basename "\$(pwd)"`;
+}
+if($browse) {
+    $id = Anime::ANN::getid $browse;
+    if(defined($id)) {
+       exec "htmlview", Anime::ANN::geturl($id);
+    } else {
+       printf STDERR "could not find " . $browse . "\n";
+       exit 1;
+    }
+}
+
+if(!defined($ARGV[0])) {
+    printf STDERR "usage: anndata NAME\n";
+    exit 1;
+}
+
+unless($id = Anime::ANN::getid $ARGV[0]) {
     printf STDERR "could not find $ARGV[0]\n";
     exit 1;
 }
 
-print getnamefromid($test) . "\n";
+$info = Anime::ANN::getseries $id;
+
+sub refdump
+{
+    my($ref, $ind);
+    ($ref, $ind) = @_;
+    if(!defined($ind)) {
+       $ind = 0;
+    }
+    
+    if(ref $ref eq "HASH") {
+       for $key (sort keys %{$ref}) {
+           print (("  " x $ind) . "$key: " . (" " x (20 - length $key)) . "(" . $ref->{$key} . ")\n");
+           refdump($ref->{$key}, $ind + 1) if ref $ref->{$key};
+       }
+    } elsif(ref $ref eq "ARRAY") {
+       for($i = 0; $i < @{$ref}; $i++) {
+           print (("  " x $ind) . "$i: " . $ref->[$i] . "\n");
+           refdump($ref->[$i], $ind + 1) if ref $ref->[$i];
+       }
+    } elsif(ref $ref eq "SCALAR") {
+       print (("  " x $ind) . $$ref . "\n");
+    } else {
+       print (("  " x $ind) . "Unknown ref: $ref\n");
+    }
+}
 
+refdump $info;