# HG changeset patch # User "Rex Tsai " # Date 1225030455 -28800 # Node ID 53c87400183f9ce27f72ebcf3b079979a996a9f4 # Parent ba96a3f8f522c18de81ae66643600dc664ae40d6 rewrite viewScore with Extractor. diff -r ba96a3f8f522 -r 53c87400183f Ikariam.pm --- 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; } diff -r ba96a3f8f522 -r 53c87400183f overall.yaml --- 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. # 酒館 # 學院 diff -r ba96a3f8f522 -r 53c87400183f scores.pl --- 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})) {