comparison Ikariam.pm @ 88:0fa9cd836e1e

rewrited the viewIsland function.
author "Rex Tsai <chihchun@kalug.linux.org.tw>"
date Sun, 26 Oct 2008 16:12:03 +0800
parents 4c82cf844aa8
children 53c87400183f
comparison
equal deleted inserted replaced
87:4c82cf844aa8 88:0fa9cd836e1e
89 package Ikariam; 89 package Ikariam;
90 use strict; 90 use strict;
91 use Data::Dumper; 91 use Data::Dumper;
92 use LWP; 92 use LWP;
93 # use LWP::Debug qw(+ -conns -trace -debug); 93 # use LWP::Debug qw(+ -conns -trace -debug);
94 # use LWP::Debug qw(+trace); 94 use LWP::Debug qw(+trace);
95 use HTTP::Cookies; 95 use HTTP::Cookies;
96 use WWW::Mechanize; 96 use WWW::Mechanize;
97 use HTML::TagParser; 97 use HTML::TagParser;
98 use XML::LibXML qw(:encoding); 98 use XML::LibXML qw(:encoding);
99 use IO::Uncompress::Gunzip qw(gunzip $GunzipError) ; 99 use IO::Uncompress::Gunzip qw(gunzip $GunzipError) ;
209 { 209 {
210 my $self = shift; 210 my $self = shift;
211 my $x = shift; 211 my $x = shift;
212 my $y = shift; 212 my $y = shift;
213 213
214 if(!defined($x) && !defined($y)) 214 if(!defined($x) && !defined($y)) {
215 {
216 die('location required'); 215 die('location required');
217 } 216 }
218 217
219 my $res = $self->{mech}->post(sprintf("http://%s/index.php?view=worldmap_iso", $self->{server}), [ 218 my $res = $self->{mech}->post(sprintf("http://%s/index.php?view=worldmap_iso", $self->{server}), [
220 xajax => 'getMapData', 219 xajax => 'getMapData',
285 { 284 {
286 my $self = shift; 285 my $self = shift;
287 my $island = shift; 286 my $island = shift;
288 287
289 my $res = $self->{mech}->get(sprintf("http://%s/index.php?view=island&id=%s", $self->{server}, $island)); 288 my $res = $self->{mech}->get(sprintf("http://%s/index.php?view=island&id=%s", $self->{server}, $island));
290 289 my $extractor = new Ikariam::Extractor(content => $res->content);
291 my $c; 290
292 my $status = gunzip \$res->content => \$c 291 my @cities;
293 or die "gunzip failed: $GunzipError\n"; 292 foreach my $i (0..16) {
294 293 my $cityLocations = $extractor->find(sprintf('//li[@id="cityLocation%s"]/@class', $i));
295 my $html = HTML::TagParser->new($c); 294 if($cityLocations =~ /city level(\d+)/) {
296 295 my %info;
297 # find inactivity and vacation 296 $info{'citylevel'} = $1;
298 my %status; 297 $info{'cityname'} = $extractor->find(sprintf('//li[@id="cityLocation%s"]//li[@class="name"]/text()', $i));
299 foreach my $class (qw/inactivity vacation/) 298 $info{'owner'} = $extractor->find(sprintf('//li[@id="cityLocation%s"]//li[@class="owner"]/text()', $i));
300 { 299 $info{'owner'} =~ s/\s+//g;
301 my @elems = $html->getElementsByAttribute("class", $class); 300 $info{'ally'} = $extractor->find(sprintf('//li[@id="cityLocation%s"]//li[@class="ally"]/a/text()', $i));
302 foreach my $elem (@elems) { 301 delete($info{'ally'}) if($info{'ally'} eq '-');
303 if($elem->innerText() =~ /^(.*?) \((\w)\)/) { 302
304 $status{$1} = $2; 303 my $href = $extractor->find(sprintf('//li[@id="cityLocation%s"]//a[@class="messageSend"]/@href', $i));
305 # printf("%s\n", $elem->innerText()); 304 # ?view=sendMessage&type=0&with=20204&destinationCityId=64165&oldView=island
305 if ($href =~ /with=(\d+)&destinationCityId=(\d+)/) {
306 $info{'user'} = $1;
307 $info{'cityId'} = $2;
308 # ?view=sendAllyMessage&allyId=1192&oldView=island&id=721
309 } else {
310 # 聯盟
311 # this is me.
312 my $id = $extractor->find(sprintf('//li[@id="cityLocation%s"]/a/@id', $i));
313 if($id =~ /city_(\d+)/) {
314 $info{'user'} = undef; # FIXME
315 $info{'cityId'} = $1;
316 }
306 } 317 }
307 } 318
308 } 319 if(defined(($extractor->find(sprintf('//li[@id="cityLocation%s"]//span[@class="vacation"]', $i)))[0])) {
309 320 $info{'status'} = 'v';
310 # find content 321 }
311 my @elems = $html->getElementsByClassName( "cityinfo" ); 322 push @cities, \%info;
312 my @cities;
313 foreach my $elem (@elems) {
314 my %info;
315
316 my @e = getElementsByTagName($elem, "li");
317 $info{'cityname'} = substr($e[0]->innerText(), 8);
318 $info{'citylevel'} = substr($e[1]->innerText(), 14);
319 $info{'owner'} = substr($e[2]->innerText(), 8);
320 $info{'ally'} = substr($e[3]->innerText(), 8);
321 delete($info{'ally'}) if($info{'ally'} eq '-');
322
323 @e = getElementsByAttribute($elem, "class", "messageSend");
324 if ( $e[0]->getAttribute("href") =~ /with=(\d+)&destinationCityId=(\d+)/)
325 {
326 $info{'user'} = $1;
327 $info{'cityId'} = $2;
328 }
329
330 # update status;
331 if(defined($status{$info{'cityname'}})) {
332 $info{'status'} = $status{$info{'cityname'}};
333 } else { 323 } else {
334 $info{'status'} = undef; 324 # TODO: delete the old city.
335 } 325 }
336 # print(Dumper(\%info)); 326
337 push @cities, \%info;
338 } 327 }
339 328
340 return @cities; 329 return @cities;
341 } 330 }
342 331