changeset 91:53c87400183f

rewrite viewScore with Extractor.
author "Rex Tsai <chihchun@kalug.linux.org.tw>"
date Sun, 26 Oct 2008 22:14:15 +0800
parents ba96a3f8f522
children bcd2e32cfabb
files Ikariam.pm overall.yaml scores.pl
diffstat 3 files changed, 16 insertions(+), 38 deletions(-) [+]
line wrap: on
line diff
--- a/Ikariam.pm	Sun Oct 26 16:27:45 2008 +0800
+++ b/Ikariam.pm	Sun Oct 26 22:14:15 2008 +0800
@@ -149,6 +149,7 @@
     my $type = shift || 'score';
     my $user = shift || '';
     my $offset = shift || 0;
+    my %users;
 
     my $res = $self->{mech}->post(sprintf("http://%s/index.php", $self->{server}), [
         highscoreType => $type,
@@ -156,52 +157,28 @@
         searchUser => $user,
         view => 'highscore'
         ]);
-
-    # TODO rewrite with Xpath
-    my $c;
-    my $status = gunzip \$res->content => \$c 
-        or die "gunzip failed: $GunzipError\n";
-
-    my %users;
-    my $html = HTML::TagParser->new($c);
-    my ($table) = $html->getElementsByAttribute("class", "table01");
-    return %users if(!defined($table));
-
-    my @elems = getElementsByTagName($table, "tr");
-
-    foreach my $elem (@elems) {
-        my $e;
+    my $extractor = new Ikariam::Extractor(content => $res->content);
+    my $result = $extractor->{doc}->find('//table[@class="table01"][2]//tr');
+    foreach my $tr ( @$result ) {
         my %user;
 
-        $e = getElementsByAttribute($elem, "class", "action");
-        $e = getElementsByTagName($e, "a");
-
-        if(defined ($e) && $e->getAttribute('href') =~ /index\.php\?view=sendMessage&with=(\d+)&oldView=highscore/)
-        {
+        my $href = $tr->find('//td[@class="action"]/a/@href');
+        if($href =~ /index\.php\?view=sendMessage&with=(\d+)&oldView=highscore/) {
             $user{'id'} = $1;
-
-            $e = getElementsByAttribute($elem, "class", "name");
-            $user{'name'} = $e->innerText();
-
-            $e = getElementsByAttribute($elem, "class", "allytag");
-            $user{'ally'} = $e->innerText();
-
-            $e = getElementsByTagName($e, "a");
-            if($e->getAttribute('href') =~ /\?view=allyPage&allyId=(\d+)/)
-            {
+            $user{'name'} = $tr->find('//td[@class="name"]/text()');
+            $user{'ally'} = ($tr->find('//td[@class="allytag"]/a/text()'))[0]->to_literal();
+            my $allyHref = $tr->find('//td[@class="allytag"]/a/@href');
+            if($allyHref =~  /\?view=allyPage&allyId=(\d+)/) {
                 $user{'allyId'} = $1;
             }
-
-            $e = getElementsByAttribute($elem, "class", "score");
-            $user{$type} = $e->innerText();
+            $user{$type} = $tr->find('//td[@class="score"]/text()');
             $user{$type} =~ s/,//g;
 
+            # converting data type and encoding.
+            foreach(keys(%user)) { utf8::encode($user{$_}); }
             $users{$user{'id'}} = \%user;
-        } else {
-            next;
         }
     }
-
     return \%users;
 }
 
--- a/overall.yaml	Sun Oct 26 16:27:45 2008 +0800
+++ b/overall.yaml	Sun Oct 26 22:14:15 2008 +0800
@@ -25,6 +25,8 @@
      # 異國文化
      - is_culturalexchange_researched:
         0: research_seafaring
+     # 希臘火, 建造噴火船
+
      # is_reousrce_balanced.
         # 酒館
         # 學院
--- a/scores.pl	Sun Oct 26 16:27:45 2008 +0800
+++ b/scores.pl	Sun Oct 26 22:14:15 2008 +0800
@@ -19,8 +19,7 @@
 {
     my $users = shift;
 
-    foreach my $user (values(%{$users}))
-    {
+    foreach my $user (values(%{$users})) {
         printf("Saving %s\n", $user->{'name'});
         if(my $c = Ikariam::User->retrieve($user->{id}))
         {