summaryrefslogtreecommitdiff
path: root/foostats.pl
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2024-12-13 09:58:01 +0200
committerPaul Buetow <paul@buetow.org>2024-12-13 09:58:01 +0200
commit3e20b49cbafbbeff06a8da4e247098ec5210d68d (patch)
tree3f171d946a8bdc8180d90765bbff4dda9506ec3a /foostats.pl
parenta823fb02c13cf6fb1b6ea8af4a0eb5212e1815c6 (diff)
can replicate
Diffstat (limited to 'foostats.pl')
-rw-r--r--foostats.pl59
1 files changed, 57 insertions, 2 deletions
diff --git a/foostats.pl b/foostats.pl
index a4ec41b..7cb6b30 100644
--- a/foostats.pl
+++ b/foostats.pl
@@ -335,10 +335,59 @@ package Foostats::Outputter {
}
package Foostats::Replicator {
+ use File::Basename;
+ use Time::Piece;
+ use LWP::UserAgent;
+
sub new ($class, %args) { bless \%args, $class }
sub replicate ($self, $partner_node) {
- die 'Replicate is not yet implemented';
+ say "Replicating from $partner_node";
+
+ for my $proto (qw(gemini web)) {
+ my $count = 0;
+
+ for my $date (_last_week_dates()) {
+ my $dest_file = "${proto}_${date}.$partner_node.json.gz";
+
+ $self->replicate_file(
+ "https://$partner_node/foostats/$dest_file",
+ $self->{stats_dir} . '/' . $dest_file,
+ $count++ < 3, # Always replicate the newest 3 files.
+ )
+ }
+ }
+ }
+
+ sub replicate_file ($self, $remote_url, $dest_file, $force) {
+ # $dest_file already exists, not replicating it
+ return if !$force && -f $dest_file;
+
+ print "Replicating $remote_url to $dest_file (force:$force)... ";
+ my $response = LWP::UserAgent->new->get($remote_url);
+ unless ($response->is_success) {
+ say "\nFailed to fetch the file: " . $response->status_line;
+ return;
+ }
+
+ open my $fh, '>', "$dest_file.tmp" or die "\nCannot open file: $!";
+ print $fh $response->decoded_content;
+ close $fh;
+
+ rename "$dest_file.tmp", $dest_file;
+ say 'done';
+ }
+
+ sub _last_week_dates () {
+ my $today = localtime;
+ my @last_week;
+
+ for my $days_ago (0..7) {
+ my $date = $today - ($days_ago * 24 * 60 * 60);
+ push @last_week, $date->strftime('%Y%m%d');
+ }
+
+ return @last_week;
}
}
@@ -358,7 +407,13 @@ package main {
}
sub replicate ($stats_dir, $partner_node) {
- die 'Partner node not specified' unless defined $partner_node;
+ unless (defined $partner_node) {
+ # Default values if partner node not set.
+ $partner_node = hostname eq 'fishfinger.buetow.org'
+ ? 'blowfish.buetow.org'
+ : 'fishfinger.buetow.org';
+ }
+
Foostats::Replicator->new(stats_dir => $stats_dir)->replicate($partner_node);
}