# HG changeset patch # User Dominic Cleal # Date 1232896161 0 # Node ID a79168f03fc462d106d01049ce80bbd2ea1cf04c # Parent 257e748cd08ec55c1f35888151b2d5cd95c7329e Replacing XML::DOM with LWP and regex, tidying code diff -r 257e748cd08e -r a79168f03fc4 reproctool.cgi --- a/reproctool.cgi Sun Jan 25 14:46:32 2009 +0000 +++ b/reproctool.cgi Sun Jan 25 15:09:21 2009 +0000 @@ -5,7 +5,7 @@ use CGI; use DBI; -use XML::DOM; +use LWP::UserAgent; # Settings my $img_http_path = '/itemimgs'; @@ -27,43 +27,49 @@ # Generate the first page, where eve-central mineral prices are prefilled. # TODO: depending on the traffic, cache these eve-central prices. -my $str_items; -if (! ($str_items = $cgi->param('items')) ) +my $str_items = $cgi->param('items'); +unless ($str_items) { - my $min; my $eveCentralURL = 'http://eve-central.com/api/evemon'; - my $xmlParser = XML::DOM::Parser->new(); - my $xmlDoc = $xmlParser->parsefile($eveCentralURL); - + my $ua = LWP::UserAgent->new; + $ua->agent('reproctool'); + my $resp = $ua->request(HTTP::Request->new(GET => $eveCentralURL)); + + my %marks; + $marks{$_} = 1 foreach ('Tritanium', 'Pyerite', 'Mexallon', 'Isogen', + 'Nocxium', 'Zydrine', 'Megacyte', 'Morphite'); + + if ($resp->is_success) + { + foreach (split(/[\n\r]/, $resp->content)) + { + next unless (/(.+)<\/name>.*([0-9\.]+)<\/price>/i); + $marks{$1} = $2; + } + } + # What evenutally will be printed. - my $inputPage = < -
+ END # Lists the name of the minerals. - foreach $min ($xmlDoc->getElementsByTagName('mineral')) + print "\n" foreach (keys %marks); + print ""; + + foreach (keys %marks) { - $inputPage .= sprintf( " \n", - $min->getElementsByTagName('name')->item(0)->getFirstChild->getNodeValue ); + my $sname = lc substr($_, 0, 4); + my $fmt = sprintf('%.2f', $marks{$_}); + print "\n"; } - -$inputPage .= < - -END - foreach $min ($xmlDoc->getElementsByTagName('mineral')) - { - $inputPage .= sprintf( " \n", - $min->getElementsByTagName('name')->item(0)->getFirstChild->getNodeValue, - $min->getElementsByTagName('price')->item(0)->getFirstChild->getNodeValue ); - } - - $inputPage .= <
$_
%s
@@ -73,8 +79,7 @@ END - print $inputPage; - exit 0; + exit; } # Inputs