Jump to content

User:FACBot/tfa.pl

From Wikipedia, the free encyclopedia
#!/usr/bin/perl -w

# Handle the recent list in TFAs
#

use English;
use strict;
use utf8;
use warnings;

use Carp;
use Data::Dumper;
use File::Basename qw(dirname);
use File::Spec;
use MediaWiki::Bot;
use POSIX;

binmode (STDERR, ':utf8');
binmode (STDOUT, ':utf8');

my $editor = MediaWiki::Bot->new ({
        host        => 'wiki.riteme.site',
        protocol     => 'https',
}) or die "new MediaWiki::Bot failed";

my $dirname = dirname (__FILE__, '.pl');
push @INC, $dirname;
require Cred;
my $cred = new Cred ();
my $log = $cred->log;

sub error_exit ($) {
    my @message = @ARG;
    if ($editor->{error}->{code}) {
        push @message, ' (', $editor->{error}->{code} , ') : ' , $editor->{error}->{details};
    }
    croak @message;
}

sub date ($) {
    my ($day) = @ARG;
    my $date = strftime ("%B %d, %Y", gmtime ($day));
    $date =~ s/ 0/ /;  # Get rid of the space
    return $date;
}

my %tfa;
my $today = time ();
my $one_day = 24 * 60 * 60;
my $three_days_ago = $today - 3 * $one_day;

$editor->login ({
    username => $cred->user,
    password => $cred->password
}) or error_exit ('login failed');

for (my $day = $today + 60 * $one_day; $day > $three_days_ago; $day -= $one_day) {
    my $date = date ($day);
    my $tfa = "Wikipedia:Today's featured article/$date";
   
    my $text = $editor->get_text ($tfa) or
        next;
   
    my ($article) = $text =~ /'''\[\[(.+)\]\]'''/;
    $tfa{$date} = $article;
}

for (my $day = $today + 60 * $one_day; $day > $today; $day -= $one_day) {
    my $this_day = date ($day);
   
    my $tfa = "Wikipedia:Today's featured article/$date";
   
    my $text = $editor->get_text ($tfa) or
        error_exit ("unable to fetch $tfa");
   
    my @matches = $text =~ /(\[\[\]\])/g;
    my $matches = scalar @matches;
    next DAY unless $matches == 3;

    for my $prev (1..3) {
        my $prev_date = date($day - $prev * $one_day);
        my $prev_tfa = $tfa{$prev_date};
        next DAY unless defined $prev_tfa;
        $text =~ s/\[\[\]\]/[[$prev_tfa]]/;
    }

    $editor->edit ({
        page => $tfa,
        text => $text,
        summary => 'update recently featured list',
        minor => 0,
    }) or
        error_exit ("unable to edit '$tfa'");
   
}

exit 0;