Mercurial > hg > reproctool
changeset 17:a79168f03fc4
Replacing XML::DOM with LWP and regex, tidying code
author | Dominic Cleal <dominic@computerkb.co.uk> |
---|---|
date | Sun, 25 Jan 2009 15:09:21 +0000 |
parents | 257e748cd08e |
children | 67f13a371c18 |
files | reproctool.cgi |
diffstat | 1 files changed, 32 insertions(+), 27 deletions(-) [+] |
line wrap: on
line diff
--- 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>(.+)<\/name>.*<price>([0-9\.]+)<\/price>/i); + $marks{$1} = $2; + } + } + # What evenutally will be printed. - my $inputPage = <<END; + print<<END; <html> <body> -<form action="/cgi-bin/reproctool/reproctool.cgi" method="post"> +<form method="post"> <textarea rows="5" cols="80" name="items"></textarea> <table border="0" cellspacing="4"> <tr> END # Lists the name of the minerals. - foreach $min ($xmlDoc->getElementsByTagName('mineral')) + print "<td>$_</td>\n" foreach (keys %marks); + print "</tr><tr>"; + + foreach (keys %marks) { - $inputPage .= sprintf( " <td>%s</td>\n", - $min->getElementsByTagName('name')->item(0)->getFirstChild->getNodeValue ); + my $sname = lc substr($_, 0, 4); + my $fmt = sprintf('%.2f', $marks{$_}); + print "<td><input type='text' name='$sname' size='7' value='$fmt' /></td>\n"; } - -$inputPage .= <<END; - </tr> - <tr> -END - foreach $min ($xmlDoc->getElementsByTagName('mineral')) - { - $inputPage .= sprintf( " <td><input type=\"text\" name=\"%s\" size=\"5\" value=\"%.2f\" /></td>\n", - $min->getElementsByTagName('name')->item(0)->getFirstChild->getNodeValue, - $min->getElementsByTagName('price')->item(0)->getFirstChild->getNodeValue ); - } - - $inputPage .= <<END; + + print <<END; </tr> </table> <input type="submit" /> @@ -73,8 +79,7 @@ </html> END - print $inputPage; - exit 0; + exit; } # Inputs