Mercurial > hg > reproctool
comparison reproctool.cgi @ 23:ccbd8fa63b9f
Adding a cache for mineral marks
author | Dominic Cleal <dominic@computerkb.co.uk> |
---|---|
date | Sun, 25 Jan 2009 17:29:07 +0000 |
parents | 266c93756c1b |
children | be92d2f1ab3f |
comparison
equal
deleted
inserted
replaced
22:266c93756c1b | 23:ccbd8fa63b9f |
---|---|
4 use strict; | 4 use strict; |
5 | 5 |
6 use CGI; | 6 use CGI; |
7 use DBI; | 7 use DBI; |
8 use LWP::UserAgent; | 8 use LWP::UserAgent; |
9 use Storable qw/store_fd fd_retrieve/; | |
9 | 10 |
10 # Settings | 11 # Settings |
11 my $img_http_path = '/itemimgs'; | 12 my $img_http_path = '/itemimgs'; |
13 my $eve_central_url = 'http://eve-central.com/api/evemon'; | |
14 my $marks_cache = 'minerals.cache'; | |
15 my $marks_cache_expiry = 4 * 60 * 60; # 4 hours | |
12 | 16 |
13 my $cgi = new CGI; | 17 my $cgi = new CGI; |
14 print $cgi->header(-type => 'text/html', | 18 print $cgi->header(-type => 'text/html', |
15 -pragma => 'no-cache', | 19 -pragma => 'no-cache', |
16 -expires => '-365d'); | 20 -expires => '-365d'); |
32 # Generate the first page, where eve-central mineral prices are prefilled. | 36 # Generate the first page, where eve-central mineral prices are prefilled. |
33 # TODO: depending on the traffic, cache these eve-central prices. | 37 # TODO: depending on the traffic, cache these eve-central prices. |
34 | 38 |
35 unless ($str_items) | 39 unless ($str_items) |
36 { | 40 { |
37 my $eveCentralURL = 'http://eve-central.com/api/evemon'; | 41 my $marks; |
38 my $ua = LWP::UserAgent->new; | 42 if (-e $marks_cache) |
39 $ua->agent('reproctool'); | 43 { |
40 my $resp = $ua->request(HTTP::Request->new(GET => $eveCentralURL)); | 44 open CACHE, "< $marks_cache" || die("Can't open cache $marks_cache: $!"); |
41 | 45 $marks = fd_retrieve(*CACHE) || die("Can't read marks from cache: $!"); |
42 my %marks; | 46 close CACHE; |
43 $marks{$_} = 1 foreach ('Tritanium', 'Pyerite', 'Mexallon', 'Isogen', | 47 |
44 'Nocxium', 'Zydrine', 'Megacyte', 'Morphite'); | 48 if (time > ($marks->{timestamp} + $marks_cache_expiry)) |
45 | 49 { |
46 if ($resp->is_success) | 50 $marks = undef; |
47 { | 51 } |
48 foreach (split(/[\n\r]/, $resp->content)) | 52 else |
49 { | 53 { |
50 next unless (/<name>(.+)<\/name>.*<price>([0-9\.]+)<\/price>/i); | 54 delete $marks->{timestamp}; |
51 $marks{$1} = $2; | 55 } |
56 } | |
57 | |
58 unless ($marks) | |
59 { | |
60 my $ua = LWP::UserAgent->new; | |
61 $ua->agent('reproctool'); | |
62 my $resp = $ua->request(HTTP::Request->new(GET => $eve_central_url)); | |
63 | |
64 if ($resp->is_success) | |
65 { | |
66 $marks = { timestamp => time }; | |
67 foreach (split(/[\n\r]/, $resp->content)) | |
68 { | |
69 next unless (/<name>(.+)<\/name>.*<price>([0-9\.]+)<\/price>/i); | |
70 $marks->{$1} = $2; | |
71 } | |
72 | |
73 if (-e $marks_cache) | |
74 { | |
75 unlink $marks_cache | |
76 || die("Unable to unlink cache $marks_cache: $!"); | |
77 } | |
78 | |
79 open CACHE, "> $marks_cache" | |
80 || die("Can't open cache $marks_cache to write: $!"); | |
81 store_fd($marks, *CACHE) || die("Can't write to cache: $!"); | |
82 close CACHE; | |
83 } | |
84 else | |
85 { | |
86 $marks->{$_} = 1 foreach ('Tritanium', 'Pyerite', 'Mexallon', | |
87 'Isogen', 'Nocxium', 'Zydrine', | |
88 'Megacyte', 'Morphite'); | |
52 } | 89 } |
53 } | 90 } |
54 | 91 |
55 # What evenutally will be printed. | 92 # What evenutally will be printed. |
56 print<<END; | 93 print<<END; |
61 <textarea rows="5" cols="80" name="items"></textarea> | 98 <textarea rows="5" cols="80" name="items"></textarea> |
62 <table border="0" cellspacing="4"> | 99 <table border="0" cellspacing="4"> |
63 <tr> | 100 <tr> |
64 END | 101 END |
65 # Lists the name of the minerals. | 102 # Lists the name of the minerals. |
66 print "<td>$_</td>\n" foreach (keys %marks); | 103 print "<td>$_</td>\n" foreach (keys %{$marks}); |
67 print "</tr><tr>"; | 104 print "</tr><tr>"; |
68 | 105 |
69 foreach (keys %marks) | 106 foreach (keys %{$marks}) |
70 { | 107 { |
71 my $sname = lc substr($_, 0, 4); | 108 my $sname = lc substr($_, 0, 4); |
72 my $fmt = sprintf('%.2f', $marks{$_}); | 109 my $fmt = sprintf('%.2f', $marks->{$_}); |
73 print "<td><input type='text' name='$sname' size='7' value='$fmt' /></td>\n"; | 110 print "<td><input type='text' name='$sname' size='7' value='$fmt' /></td>\n"; |
74 } | 111 } |
75 | 112 |
76 print <<END; | 113 print <<END; |
77 </tr> | 114 </tr> |