comparison 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
comparison
equal deleted inserted replaced
90:ba96a3f8f522 91:53c87400183f
147 { 147 {
148 my $self = shift; 148 my $self = shift;
149 my $type = shift || 'score'; 149 my $type = shift || 'score';
150 my $user = shift || ''; 150 my $user = shift || '';
151 my $offset = shift || 0; 151 my $offset = shift || 0;
152 my %users;
152 153
153 my $res = $self->{mech}->post(sprintf("http://%s/index.php", $self->{server}), [ 154 my $res = $self->{mech}->post(sprintf("http://%s/index.php", $self->{server}), [
154 highscoreType => $type, 155 highscoreType => $type,
155 offset => $offset, 156 offset => $offset,
156 searchUser => $user, 157 searchUser => $user,
157 view => 'highscore' 158 view => 'highscore'
158 ]); 159 ]);
159 160 my $extractor = new Ikariam::Extractor(content => $res->content);
160 # TODO rewrite with Xpath 161 my $result = $extractor->{doc}->find('//table[@class="table01"][2]//tr');
161 my $c; 162 foreach my $tr ( @$result ) {
162 my $status = gunzip \$res->content => \$c
163 or die "gunzip failed: $GunzipError\n";
164
165 my %users;
166 my $html = HTML::TagParser->new($c);
167 my ($table) = $html->getElementsByAttribute("class", "table01");
168 return %users if(!defined($table));
169
170 my @elems = getElementsByTagName($table, "tr");
171
172 foreach my $elem (@elems) {
173 my $e;
174 my %user; 163 my %user;
175 164
176 $e = getElementsByAttribute($elem, "class", "action"); 165 my $href = $tr->find('//td[@class="action"]/a/@href');
177 $e = getElementsByTagName($e, "a"); 166 if($href =~ /index\.php\?view=sendMessage&with=(\d+)&oldView=highscore/) {
178
179 if(defined ($e) && $e->getAttribute('href') =~ /index\.php\?view=sendMessage&with=(\d+)&oldView=highscore/)
180 {
181 $user{'id'} = $1; 167 $user{'id'} = $1;
182 168 $user{'name'} = $tr->find('//td[@class="name"]/text()');
183 $e = getElementsByAttribute($elem, "class", "name"); 169 $user{'ally'} = ($tr->find('//td[@class="allytag"]/a/text()'))[0]->to_literal();
184 $user{'name'} = $e->innerText(); 170 my $allyHref = $tr->find('//td[@class="allytag"]/a/@href');
185 171 if($allyHref =~ /\?view=allyPage&allyId=(\d+)/) {
186 $e = getElementsByAttribute($elem, "class", "allytag");
187 $user{'ally'} = $e->innerText();
188
189 $e = getElementsByTagName($e, "a");
190 if($e->getAttribute('href') =~ /\?view=allyPage&allyId=(\d+)/)
191 {
192 $user{'allyId'} = $1; 172 $user{'allyId'} = $1;
193 } 173 }
194 174 $user{$type} = $tr->find('//td[@class="score"]/text()');
195 $e = getElementsByAttribute($elem, "class", "score");
196 $user{$type} = $e->innerText();
197 $user{$type} =~ s/,//g; 175 $user{$type} =~ s/,//g;
198 176
177 # converting data type and encoding.
178 foreach(keys(%user)) { utf8::encode($user{$_}); }
199 $users{$user{'id'}} = \%user; 179 $users{$user{'id'}} = \%user;
200 } else { 180 }
201 next; 181 }
202 }
203 }
204
205 return \%users; 182 return \%users;
206 } 183 }
207 184
208 sub viewWorldMap 185 sub viewWorldMap
209 { 186 {