Mercurial > eagle-eye
diff Ikariam.pm @ 91:53c87400183f
rewrite viewScore with Extractor.
author | "Rex Tsai <chihchun@kalug.linux.org.tw>" |
---|---|
date | Sun, 26 Oct 2008 22:14:15 +0800 |
parents | 0fa9cd836e1e |
children | bcd2e32cfabb |
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; }