comparison Ikariam.pm @ 117:5a66fea952fe

implemented a dirty hack to fixed the stupid highscore search function.
author "Rex Tsai <chihchun@kalug.linux.org.tw>"
date Thu, 30 Oct 2008 15:00:17 +0800
parents 279a000eb3b0
children 4374173bab47
comparison
equal deleted inserted replaced
116:da0dd597401c 117:5a66fea952fe
48 or die "gunzip failed: $GunzipError\n"; 48 or die "gunzip failed: $GunzipError\n";
49 } else { 49 } else {
50 $string = $content; 50 $string = $content;
51 } 51 }
52 52
53 $self->{doc} = $parser->parse_html_string ($string, { suppress_errors => 1, encoding => 'utf-8' }); 53 # dirty hack for stupid highScore search function.
54 $string =~ s/name="searchUser" value=".*?"/name="searchUser" value=""/;
55
56 $self->{doc} = $parser->parse_html_string ($string, { suppress_errors => 1, encoding => 'UTF-8' });
54 return; 57 return;
55 } 58 }
56 59
57 # find($XPathQuery); 60 # find($XPathQuery);
58 sub find { 61 sub find {
88 91
89 package Ikariam; 92 package Ikariam;
90 use strict; 93 use strict;
91 use Data::Dumper; 94 use Data::Dumper;
92 use LWP; 95 use LWP;
93 # use LWP::Debug qw(+ -conns -trace -debug);
94 # use LWP::Debug qw(+trace);
95 use HTTP::Cookies; 96 use HTTP::Cookies;
96 use WWW::Mechanize; 97 use WWW::Mechanize;
97 use HTML::TagParser; 98 use HTML::TagParser;
98 use XML::LibXML qw(:encoding); 99 use XML::LibXML qw(:encoding);
99 use IO::Uncompress::Gunzip qw(gunzip $GunzipError) ; 100 use IO::Uncompress::Gunzip qw(gunzip $GunzipError) ;
100 # use Time::ParseDate;
101 use POSIX; 101 use POSIX;
102 use utf8; 102 use utf8;
103 103
104 sub new 104 sub new
105 { 105 {
112 timeout => 10, 112 timeout => 10,
113 ), 113 ),
114 server => $server, 114 server => $server,
115 user => $user, 115 user => $user,
116 pass => $pass, 116 pass => $pass,
117 debug => undef,
117 buildingIDs => { 118 buildingIDs => {
118 townHall => 0, 119 townHall => 0,
119 townhall => 0, 120 townhall => 0,
120 port => 3, 121 port => 3,
121 academy => 4, 122 academy => 4,
137 tradegood => 2 138 tradegood => 2
138 } 139 }
139 }; 140 };
140 141
141 142
143 # if debug
144 # LWP::Debug::level('+conns');
145
142 $self->{mech}->cookie_jar(HTTP::Cookies->new(file => "/tmp/ikariam-cookies.txt", autosave => 1)); 146 $self->{mech}->cookie_jar(HTTP::Cookies->new(file => "/tmp/ikariam-cookies.txt", autosave => 1));
143 $self->{mech}->default_headers->push_header('Accept-Encoding', 'deflate'); 147 $self->{mech}->default_headers->push_header('Accept-Encoding', 'deflate');
144 148
145 return bless $self, $class; 149 return bless $self, $class;
146 } 150 }
169 my $type = shift || 'score'; 173 my $type = shift || 'score';
170 my $user = shift || ''; 174 my $user = shift || '';
171 my $offset = shift || 0; 175 my $offset = shift || 0;
172 my %users; 176 my %users;
173 177
174 my $res = $self->{mech}->post(sprintf("http://%s/index.php", $self->{server}), [ 178 $self->{mech}->get(sprintf("http://%s/index.php?view=highscore&showMe=1", $self->{server}));
175 highscoreType => $type, 179 my $res = $self->{mech}->submit_form(
176 offset => $offset, 180 form_number => 1,
177 searchUser => $user, 181 fields => {
178 view => 'highscore' 182 highscoreType => $type,
179 ]); 183 offset => $offset,
184 searchUser => $user,
185 }
186 );
187
180 my $extractor = new Ikariam::Extractor(content => $res->content); 188 my $extractor = new Ikariam::Extractor(content => $res->content);
181 my $result = $extractor->{doc}->find('//table[@class="table01"][2]//tr'); 189 my $result = $extractor->{doc}->find('//table[@class="table01"][2]//tr');
182 190
183 foreach my $tr ( @$result ) { 191 foreach my $tr ( @$result ) {
184 my %user; 192 my %user;
185 my $extractor = new Ikariam::Extractor(content => $tr->toString(1)); 193 my $extractor = new Ikariam::Extractor(content => $tr->toString(0));
186 194
187 my $href = $extractor->find('//td[@class="action"]/a/@href'); 195 my $href = $extractor->find('//td[@class="action"]/a/@href');
188 if($href =~ /index\.php\?view=sendMessage&with=(\d+)&oldView=highscore/) { 196 if($href =~ /index\.php\?view=sendMessage&with=(\d+)&oldView=highscore/) {
189 $user{'id'} = $1; 197 $user{'id'} = $1;
190 $user{'name'} = $user; 198 # $user{'name'} = $user;
191 # encoding issue. 199 # encoding issue.
192 $user{'name'} = $extractor->find('//td[@class="name"]/text()'); 200 $user{'name'} = $extractor->find('//td[@class="name"]/text()');
201 next if($user{'name'} eq '');
193 202
194 $user{'ally'} = $extractor->find('//td[@class="allytag"]/a/text()'); 203 $user{'ally'} = $extractor->find('//td[@class="allytag"]/a/text()');
195 my $allyHref = $extractor->find('//td[@class="allytag"]/a/@href'); 204 my $allyHref = $extractor->find('//td[@class="allytag"]/a/@href');
196 if($allyHref =~ /\?view=allyPage&oldView=highscore&allyId=(\d+)/) { 205 if($allyHref =~ /\?view=allyPage&oldView=highscore&allyId=(\d+)/) {
197 $user{'allyId'} = $1; 206 $user{'allyId'} = $1;
296 $info{'citylevel'} = $1; 305 $info{'citylevel'} = $1;
297 $info{'cityname'} = $extractor->find(sprintf('//li[@id="cityLocation%s"]//li[@class="name"]/text()', $i)); 306 $info{'cityname'} = $extractor->find(sprintf('//li[@id="cityLocation%s"]//li[@class="name"]/text()', $i));
298 $info{'owner'} = $extractor->find(sprintf('//li[@id="cityLocation%s"]//li[@class="owner"]/text()', $i)); 307 $info{'owner'} = $extractor->find(sprintf('//li[@id="cityLocation%s"]//li[@class="owner"]/text()', $i));
299 $info{'owner'} =~ s/\s+//g; 308 $info{'owner'} =~ s/\s+//g;
300 $info{'ally'} = $extractor->find(sprintf('//li[@id="cityLocation%s"]//li[@class="ally"]/a/text()', $i)); 309 $info{'ally'} = $extractor->find(sprintf('//li[@id="cityLocation%s"]//li[@class="ally"]/a/text()', $i));
301 delete($info{'ally'}) if($info{'ally'} eq '-'); 310 if($info{'ally'} eq '-') {
311 delete($info{'ally'})
312 } else {
313 my $href = $extractor->find(sprintf('//li[@id="cityLocation%s"]//li[@class="ally"]/a/@href', $i));
314 # http://s2.ikariam.tw/index.php?view=allyPage&allyId=264&oldView=island&id=569
315 if($href =~ /&allyId=(\d+)&/) {
316 $info{'allyId'} = $1;
317 }
318 }
319
320 # Ally Id
302 321
303 my $href = $extractor->find(sprintf('//li[@id="cityLocation%s"]//a[@class="messageSend"]/@href', $i)); 322 my $href = $extractor->find(sprintf('//li[@id="cityLocation%s"]//a[@class="messageSend"]/@href', $i));
304 # ?view=sendMessage&type=0&with=20204&destinationCityId=64165&oldView=island 323 # ?view=sendMessage&type=0&with=20204&destinationCityId=64165&oldView=island
305 if ($href =~ /with=(\d+)&destinationCityId=(\d+)/) { 324 if ($href =~ /with=(\d+)&destinationCityId=(\d+)/) {
306 $info{'user'} = $1; 325 $info{'user'} = $1;