comparison reproctool.cgi @ 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
comparison
equal deleted inserted replaced
16:257e748cd08e 17:a79168f03fc4
3 use warnings; 3 use warnings;
4 use strict; 4 use strict;
5 5
6 use CGI; 6 use CGI;
7 use DBI; 7 use DBI;
8 use XML::DOM; 8 use LWP::UserAgent;
9 9
10 # Settings 10 # Settings
11 my $img_http_path = '/itemimgs'; 11 my $img_http_path = '/itemimgs';
12 12
13 my $cgi = new CGI; 13 my $cgi = new CGI;
25 exit; 25 exit;
26 } 26 }
27 27
28 # Generate the first page, where eve-central mineral prices are prefilled. 28 # Generate the first page, where eve-central mineral prices are prefilled.
29 # TODO: depending on the traffic, cache these eve-central prices. 29 # TODO: depending on the traffic, cache these eve-central prices.
30 my $str_items; 30 my $str_items = $cgi->param('items');
31 if (! ($str_items = $cgi->param('items')) ) 31 unless ($str_items)
32 { 32 {
33 my $min;
34 my $eveCentralURL = 'http://eve-central.com/api/evemon'; 33 my $eveCentralURL = 'http://eve-central.com/api/evemon';
35 my $xmlParser = XML::DOM::Parser->new(); 34 my $ua = LWP::UserAgent->new;
36 my $xmlDoc = $xmlParser->parsefile($eveCentralURL); 35 $ua->agent('reproctool');
37 36 my $resp = $ua->request(HTTP::Request->new(GET => $eveCentralURL));
37
38 my %marks;
39 $marks{$_} = 1 foreach ('Tritanium', 'Pyerite', 'Mexallon', 'Isogen',
40 'Nocxium', 'Zydrine', 'Megacyte', 'Morphite');
41
42 if ($resp->is_success)
43 {
44 foreach (split(/[\n\r]/, $resp->content))
45 {
46 next unless (/<name>(.+)<\/name>.*<price>([0-9\.]+)<\/price>/i);
47 $marks{$1} = $2;
48 }
49 }
50
38 # What evenutally will be printed. 51 # What evenutally will be printed.
39 my $inputPage = <<END; 52 print<<END;
40 <html> 53 <html>
41 <body> 54 <body>
42 55
43 <form action="/cgi-bin/reproctool/reproctool.cgi" method="post"> 56 <form method="post">
44 <textarea rows="5" cols="80" name="items"></textarea> 57 <textarea rows="5" cols="80" name="items"></textarea>
45 <table border="0" cellspacing="4"> 58 <table border="0" cellspacing="4">
46 <tr> 59 <tr>
47 END 60 END
48 # Lists the name of the minerals. 61 # Lists the name of the minerals.
49 foreach $min ($xmlDoc->getElementsByTagName('mineral')) 62 print "<td>$_</td>\n" foreach (keys %marks);
50 { 63 print "</tr><tr>";
51 $inputPage .= sprintf( " <td>%s</td>\n", 64
52 $min->getElementsByTagName('name')->item(0)->getFirstChild->getNodeValue ); 65 foreach (keys %marks)
53 } 66 {
54 67 my $sname = lc substr($_, 0, 4);
55 $inputPage .= <<END; 68 my $fmt = sprintf('%.2f', $marks{$_});
56 </tr> 69 print "<td><input type='text' name='$sname' size='7' value='$fmt' /></td>\n";
57 <tr> 70 }
58 END 71
59 foreach $min ($xmlDoc->getElementsByTagName('mineral')) 72 print <<END;
60 {
61 $inputPage .= sprintf( " <td><input type=\"text\" name=\"%s\" size=\"5\" value=\"%.2f\" /></td>\n",
62 $min->getElementsByTagName('name')->item(0)->getFirstChild->getNodeValue,
63 $min->getElementsByTagName('price')->item(0)->getFirstChild->getNodeValue );
64 }
65
66 $inputPage .= <<END;
67 </tr> 73 </tr>
68 </table> 74 </table>
69 <input type="submit" /> 75 <input type="submit" />
70 </form> 76 </form>
71 77
72 </body> 78 </body>
73 </html> 79 </html>
74 END 80 END
75 81
76 print $inputPage; 82 exit;
77 exit 0;
78 } 83 }
79 84
80 # Inputs 85 # Inputs
81 my $str_items = $cgi->param('items') or user_error('Items missing'); 86 my $str_items = $cgi->param('items') or user_error('Items missing');
82 my $cols = $cgi->param('cols') || 4; 87 my $cols = $cgi->param('cols') || 4;