changeset 26:be92d2f1ab3f

Merging -df
author Dominic Cleal <dominic@computerkb.co.uk>
date Sun, 25 Jan 2009 17:31:03 +0000
parents ccbd8fa63b9f (diff) b505b076c493 (current diff)
children 789877ff0e1a
files reproctool.cgi
diffstat 1 files changed, 57 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- a/reproctool.cgi	Sun Jan 25 17:26:28 2009 +0000
+++ b/reproctool.cgi	Sun Jan 25 17:31:03 2009 +0000
@@ -7,9 +7,13 @@
 use CGI;
 use DBI;
 use LWP::UserAgent;
+use Storable qw/store_fd fd_retrieve/;
 
 # Settings
 my $img_http_path = '/itemimgs';
+my $eve_central_url = 'http://eve-central.com/api/evemon';
+my $marks_cache = 'minerals.cache';
+my $marks_cache_expiry = 4 * 60 * 60;  # 4 hours
 
 my $cgi = new CGI;
 print $cgi->header(-type    => 'text/html',
@@ -26,26 +30,63 @@
     exit;
 }
 
+# Inputs
+my $cols = $cgi->param('cols') || 4;
+my $str_items = $cgi->param('items');
+
 # Generate the first page, where eve-central mineral prices are prefilled.
 # TODO: depending on the traffic, cache these eve-central prices.
-my $str_items = $cgi->param('items');
+
 unless ($str_items)
 {
-    my $eveCentralURL = 'http://eve-central.com/api/evemon';
-    my $ua = LWP::UserAgent->new;
-    $ua->agent('reproctool');
-    my $resp = $ua->request(HTTP::Request->new(GET => $eveCentralURL));
+    my $marks;
+    if (-e $marks_cache)
+    {
+        open CACHE, "< $marks_cache" || die("Can't open cache $marks_cache: $!");
+        $marks = fd_retrieve(*CACHE) || die("Can't read marks from cache: $!");
+        close CACHE;
+        
+        if (time > ($marks->{timestamp} + $marks_cache_expiry))
+        {
+            $marks = undef;
+        }
+        else
+        {
+            delete $marks->{timestamp};
+        }
+    }
     
-    my %marks;
-    $marks{$_} = 1 foreach ('Tritanium', 'Pyerite', 'Mexallon', 'Isogen',
-                            'Nocxium',   'Zydrine', 'Megacyte', 'Morphite');
-    
-    if ($resp->is_success)
+    unless ($marks)
     {
-        foreach (split(/[\n\r]/, $resp->content))
+        my $ua = LWP::UserAgent->new;
+        $ua->agent('reproctool');
+        my $resp = $ua->request(HTTP::Request->new(GET => $eve_central_url));
+        
+        if ($resp->is_success)
         {
-            next unless (/<name>(.+)<\/name>.*<price>([0-9\.]+)<\/price>/i);
-            $marks{$1} = $2;
+            $marks = { timestamp => time };
+            foreach (split(/[\n\r]/, $resp->content))
+            {
+                next unless (/<name>(.+)<\/name>.*<price>([0-9\.]+)<\/price>/i);
+                $marks->{$1} = $2;
+            }
+            
+            if (-e $marks_cache)
+            {
+                unlink $marks_cache
+                    || die("Unable to unlink cache $marks_cache: $!");
+            }
+            
+            open CACHE, "> $marks_cache"
+              || die("Can't open cache $marks_cache to write: $!");
+            store_fd($marks, *CACHE) || die("Can't write to cache: $!");
+            close CACHE;
+        }
+        else
+        {
+            $marks->{$_} = 1 foreach ('Tritanium', 'Pyerite', 'Mexallon',
+                                      'Isogen',    'Nocxium', 'Zydrine',
+                                      'Megacyte',  'Morphite');
         }
     }
     
@@ -60,13 +101,13 @@
         <tr>
 END
     # Lists the name of the minerals.
-    print "<td>$_</td>\n" foreach (keys %marks);
+    print "<td>$_</td>\n" foreach (keys %{$marks});
     print "</tr><tr>";
     
-    foreach (keys %marks)
+    foreach (keys %{$marks})
     {
         my $sname = lc substr($_, 0, 4);
-        my $fmt = sprintf('%.2f', $marks{$_});
+        my $fmt = sprintf('%.2f', $marks->{$_});
         print "<td><input type='text' name='$sname' size='7' value='$fmt' /></td>\n";
     }
     
@@ -85,9 +126,6 @@
     exit;
 }
 
-# Inputs
-my $cols = $cgi->param('cols') || 4;
-
 # Load mineral prices
 my $trit = $cgi->param('trit') || user_error('No tritanium price');
 my $pyer = $cgi->param('pyer') || user_error('No pyerite price');