From: Steinar H. Gunderson Date: Mon, 19 Nov 2018 19:19:51 +0000 (+0100) Subject: Support getting PGNs from local files. X-Git-Url: https://git.sesse.net/?p=remoteglot;a=commitdiff_plain;h=b0c4ec18261ba40c2323c83f88e4e647d2039638 Support getting PGNs from local files. --- diff --git a/remoteglot.pl b/remoteglot.pl index 049cdab..c6815af 100755 --- a/remoteglot.pl +++ b/remoteglot.pl @@ -124,7 +124,7 @@ if (defined($remoteglotconf::server)) { ); } if (defined($remoteglotconf::target)) { - if ($remoteglotconf::target =~ /^https?:/) { + if ($remoteglotconf::target =~ /^(?:\/|https?:)/) { fetch_pgn($remoteglotconf::target); } elsif (defined($t)) { $t->cmd("observe $remoteglotconf::target"); @@ -248,9 +248,26 @@ sub handle_fics { # Starts periodic fetching of PGNs from the given URL. sub fetch_pgn { my ($url) = @_; - AnyEvent::HTTP::http_get($url, sub { - handle_pgn(@_, $url); - }); + if ($url =~ m#^/#) { # Local file. + eval { + local $/ = undef; + open my $fh, "<", $url + or die "$url: $!"; + my $pgn = <$fh>; + close $fh; + handle_pgn($pgn, '', $url); + }; + if ($@) { + warn "$url: $@"; + $http_timer = AnyEvent->timer(after => 1.0, cb => sub { + fetch_pgn($url); + }); + } + } else { + AnyEvent::HTTP::http_get($url, sub { + handle_pgn(@_, $url); + }); + } } my ($last_pgn_white, $last_pgn_black);