changeset 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 86402d28544f
files Ikariam.pm scan.pl
diffstat 2 files changed, 49 insertions(+), 58 deletions(-) [+]
line wrap: on
line diff
--- a/Ikariam.pm	Sun Oct 26 15:02:06 2008 +0800
+++ b/Ikariam.pm	Sun Oct 26 16:12:03 2008 +0800
@@ -91,7 +91,7 @@
 use Data::Dumper;
 use LWP;
 # use LWP::Debug qw(+ -conns -trace -debug);
-# use LWP::Debug qw(+trace);
+use LWP::Debug qw(+trace);
 use HTTP::Cookies;
 use WWW::Mechanize;
 use HTML::TagParser;
@@ -211,8 +211,7 @@
     my $x = shift;
     my $y = shift;
 
-    if(!defined($x) && !defined($y))
-    {
+    if(!defined($x) && !defined($y)) {
         die('location required');
     }
 
@@ -287,54 +286,44 @@
     my $island = shift;
 
     my $res = $self->{mech}->get(sprintf("http://%s/index.php?view=island&id=%s", $self->{server}, $island));
+    my $extractor = new Ikariam::Extractor(content => $res->content);
 
-    my $c;
-    my $status = gunzip \$res->content => \$c 
-        or die "gunzip failed: $GunzipError\n";
-
-    my $html = HTML::TagParser->new($c);
+    my @cities;
+    foreach my $i (0..16) {
+        my $cityLocations = $extractor->find(sprintf('//li[@id="cityLocation%s"]/@class', $i));
+        if($cityLocations =~ /city level(\d+)/) {
+            my %info;
+            $info{'citylevel'} = $1;
+            $info{'cityname'} = $extractor->find(sprintf('//li[@id="cityLocation%s"]//li[@class="name"]/text()', $i));
+            $info{'owner'} = $extractor->find(sprintf('//li[@id="cityLocation%s"]//li[@class="owner"]/text()', $i));
+            $info{'owner'} =~ s/\s+//g;
+            $info{'ally'} = $extractor->find(sprintf('//li[@id="cityLocation%s"]//li[@class="ally"]/a/text()', $i));
+            delete($info{'ally'}) if($info{'ally'} eq '-');
 
-    # find inactivity and vacation
-    my %status;
-    foreach my $class (qw/inactivity vacation/)
-    {
-        my @elems = $html->getElementsByAttribute("class", $class);
-        foreach my $elem (@elems) {
-            if($elem->innerText() =~ /^(.*?) \((\w)\)/) {
-                $status{$1} = $2;
-                # printf("%s\n", $elem->innerText());
+            my $href = $extractor->find(sprintf('//li[@id="cityLocation%s"]//a[@class="messageSend"]/@href', $i));
+            # ?view=sendMessage&type=0&with=20204&destinationCityId=64165&oldView=island
+            if ($href =~ /with=(\d+)&destinationCityId=(\d+)/) {
+                $info{'user'} = $1;
+                $info{'cityId'} = $2;
+            # ?view=sendAllyMessage&allyId=1192&oldView=island&id=721
+            } else {
+                # 聯盟
+                # this is me.
+                my $id = $extractor->find(sprintf('//li[@id="cityLocation%s"]/a/@id', $i));
+                if($id =~ /city_(\d+)/) {
+                    $info{'user'} = undef; # FIXME
+                    $info{'cityId'} = $1; 
+                }
             }
-        }
-    }
 
-    # find content
-    my @elems = $html->getElementsByClassName( "cityinfo" );
-    my @cities;
-    foreach my $elem (@elems) {
-        my %info;
-
-        my @e = getElementsByTagName($elem, "li");
-        $info{'cityname'} = substr($e[0]->innerText(), 8);
-        $info{'citylevel'} = substr($e[1]->innerText(), 14);
-        $info{'owner'} = substr($e[2]->innerText(), 8);
-        $info{'ally'} = substr($e[3]->innerText(), 8);
-        delete($info{'ally'}) if($info{'ally'} eq '-');
-
-        @e = getElementsByAttribute($elem, "class", "messageSend");
-        if ( $e[0]->getAttribute("href") =~ /with=(\d+)&destinationCityId=(\d+)/)
-        {
-            $info{'user'} = $1;
-            $info{'cityId'} = $2;
+            if(defined(($extractor->find(sprintf('//li[@id="cityLocation%s"]//span[@class="vacation"]', $i)))[0])) {
+                $info{'status'} = 'v';
+            }
+            push @cities, \%info;
+        } else {
+            # TODO: delete the old city.
         }
 
-        # update status;
-        if(defined($status{$info{'cityname'}})) {
-            $info{'status'} = $status{$info{'cityname'}};
-        } else {
-            $info{'status'} = undef;
-        }
-        # print(Dumper(\%info));
-        push @cities, \%info;
     }
 
     return @cities;
--- a/scan.pl	Sun Oct 26 15:02:06 2008 +0800
+++ b/scan.pl	Sun Oct 26 16:12:03 2008 +0800
@@ -52,22 +52,24 @@
 {
     my $users = shift;
 
-    foreach my $h_user (values(%{$users}))
-    {
-        # print(Dumper($user));
-        printf("Saving user %s\n", $h_user->{'name'});
-        $h_user->{'time'} = time;
-        my $user;
-        if($user = Ikariam::User->retrieve($h_user->{id}))
+    if(defined($users)) {
+        foreach my $h_user (values(%{$users}))
         {
-            foreach my $i (keys(%$h_user)) {
-                # eval($c->$i($h_user->{$i}));
-                $user->set($i => $h_user->{$i});
+            # print(Dumper($user));
+            printf("Saving user %s\n", $h_user->{'name'});
+            $h_user->{'time'} = time;
+            my $user;
+            if($user = Ikariam::User->retrieve($h_user->{id}))
+            {
+                foreach my $i (keys(%$h_user)) {
+                    # eval($c->$i($h_user->{$i}));
+                    $user->set($i => $h_user->{$i});
+                }
+            } else {
+                $user = Ikariam::User->insert($h_user);
             }
-        } else {
-            $user = Ikariam::User->insert($h_user);
+            $user->update();
         }
-        $user->update();
     }
 }