comparison reproctool.cgi @ 29:420807d41e04

Updating comments
author Dominic Cleal <dominic@computerkb.co.uk>
date Sun, 25 Jan 2009 17:38:56 +0000
parents 61fb1a6de2c5
children d1028107ab75
comparison
equal deleted inserted replaced
28:61fb1a6de2c5 29:420807d41e04
39 # Inputs 39 # Inputs
40 my $cols = $cgi->param('cols') || 4; 40 my $cols = $cgi->param('cols') || 4;
41 my $str_items = $cgi->param('items'); 41 my $str_items = $cgi->param('items');
42 42
43 # Generate the first page, where eve-central mineral prices are prefilled. 43 # Generate the first page, where eve-central mineral prices are prefilled.
44 # TODO: depending on the traffic, cache these eve-central prices.
45
46 unless ($str_items) 44 unless ($str_items)
47 { 45 {
46 # Try and pull the marks out of a stored cache
48 my $marks; 47 my $marks;
49 if (-e $marks_cache) 48 if (-e $marks_cache)
50 { 49 {
51 open CACHE, "< $marks_cache" || die("Can't open cache $marks_cache: $!"); 50 open CACHE, "< $marks_cache" || die("Can't open cache $marks_cache: $!");
52 $marks = fd_retrieve(*CACHE) || die("Can't read marks from cache: $!"); 51 $marks = fd_retrieve(*CACHE) || die("Can't read marks from cache: $!");
53 close CACHE; 52 close CACHE;
54 53
54 # Check expiry time
55 if (time > ($marks->{timestamp} + $marks_cache_expiry)) 55 if (time > ($marks->{timestamp} + $marks_cache_expiry))
56 { 56 {
57 $marks = undef; 57 $marks = undef;
58 } 58 }
59 } 59 }
60 60
61 unless ($marks) 61 unless ($marks)
62 { 62 {
63 # If cache was unavailable or had expired, pull again from eve-central
63 my $ua = LWP::UserAgent->new; 64 my $ua = LWP::UserAgent->new;
64 $ua->agent('reproctool'); 65 $ua->agent('reproctool');
65 my $resp = $ua->request(HTTP::Request->new(GET => $eve_central_url)); 66 my $resp = $ua->request(HTTP::Request->new(GET => $eve_central_url));
66 67
67 if ($resp->is_success) 68 if ($resp->is_success)
68 { 69 {
70 # Store as Mineral => 1234
69 $marks = { timestamp => time }; 71 $marks = { timestamp => time };
70 foreach (split(/[\n\r]/, $resp->content)) 72 foreach (split(/[\n\r]/, $resp->content))
71 { 73 {
72 next unless (/<name>(.+)<\/name>.*<price>([0-9\.]+)<\/price>/i); 74 next unless (/<name>(.+)<\/name>.*<price>([0-9\.]+)<\/price>/i);
73 $marks->{$1} = $2; 75 $marks->{$1} = $2;
77 { 79 {
78 unlink $marks_cache 80 unlink $marks_cache
79 || die("Unable to unlink cache $marks_cache: $!"); 81 || die("Unable to unlink cache $marks_cache: $!");
80 } 82 }
81 83
84 # Store into the cache
82 open CACHE, "> $marks_cache" 85 open CACHE, "> $marks_cache"
83 || die("Can't open cache $marks_cache to write: $!"); 86 || die("Can't open cache $marks_cache to write: $!");
84 store_fd($marks, *CACHE) || die("Can't write to cache: $!"); 87 store_fd($marks, *CACHE) || die("Can't write to cache: $!");
85 close CACHE; 88 close CACHE;
86 } 89 }
87 else 90 else
88 { 91 {
92 # User can enter their own numbers if eve-central was down
89 $marks->{$_} = 1 foreach ('Tritanium', 'Pyerite', 'Mexallon', 93 $marks->{$_} = 1 foreach ('Tritanium', 'Pyerite', 'Mexallon',
90 'Isogen', 'Nocxium', 'Zydrine', 94 'Isogen', 'Nocxium', 'Zydrine',
91 'Megacyte', 'Morphite'); 95 'Megacyte', 'Morphite');
92 } 96 }
93 } 97 }