our @EXPORT_OK = qw/naodash_xlog naodash_user/;
our @EXPORT = @EXPORT_OK;
+use File::Slurp;
+use File::Spec::Functions qw/tmpdir catdir catfile/;
use HTTP::Tiny;
use List::Util qw/max min sum/;
use List::MoreUtils qw/uniq/;
my $ht = HTTP::Tiny->new(agent => "NetHack-NAOdash/$VERSION ");
-sub naodash_user {
+sub _get_xlog_from_server {
my ($name) = @_;
my $ret = $ht->get("http://alt.org/nethack/player-all-xlog.php?player=$name");
die 'Error while retrieving xlogfile from alt.org: ' . $ret->{status} . ' ' . $ret->{reason} . "\n" unless $ret->{success};
- my ($xlog) = $ret->{content} =~ m{<pre>(.*)</pre>}i;
+ $ret->{content} =~ m{<pre>(.*)</pre>}i;
+}
+
+sub _get_xlog {
+ my ($name) = @_;
+ return _get_xlog_from_server $name if $ENV{NAODASH_CACHE} && lc $ENV{NAODASH_CACHE} eq 'none';
+ my $dir = $ENV{NAODASH_CACHE} || catdir tmpdir, 'naodash';
+ mkdir $dir or die "Cannot create cache directory: $!\n" unless -d $dir;
+ my $file = catfile $dir, $name;
+ write_file $file, _get_xlog_from_server $name unless -f $file && time - (stat $file)[9] < 86400;
+ scalar read_file $file
+}
+
+sub naodash_user {
+ my ($name) = @_;
+ my $xlog = _get_xlog $name;
die "No xlogfile found for user $name\n" unless defined $xlog;
naodash_xlog $xlog;
}
B<naodash_xlog>. Dies if no xlogfile is found or if the server cannot
be contacted.
+This method caches the downloaded xlogfiles for one day in the
+directory named by the NAODASH_CACHE environment variable.
+
+=back
+
+=head1 ENVIRONMENT
+
+=over
+
+=item NAODASH_CACHE
+
+Path to a directory that should be used to cache xlogfiles downloaded
+from NAO, or the special value 'none' (case-insensitive) to disable
+caching.
+
+By default a directory named 'naodash' in the default temporary
+directory (C<< File::Spec->tmpdir >>) is used.
+
=back
=head1 SEE ALSO
required. Do not parse this command's output. For computer-readable
output, please use the L<NetHack::NAOdash> library directly.
+=head1 ENVIRONMENT
+
+=over
+
+=item NAODASH_CACHE
+
+Path to a directory that should be used to cache xlogfiles downloaded
+from NAO, or the special value 'none' (case-insensitive) to disable
+caching.
+
+By default a directory named 'naodash' in the default temporary
+directory (C<< File::Spec->tmpdir >>) is used.
+
+=back
+
=head1 SEE ALSO
L<NetHack::NAOdash>, L<http://alt.org/nethack/>, L<App::NAOdash>, L<App::Web::NAOdash>