User:AnomieBOT/source/tasks/TFATitleSubpageCreator.pm: Difference between revisions

Content deleted Content added
AnomieBOT (talk | contribs)
Updating published sources: TFATitleSubpageCreator: * Strip comments from the title, apparently someone thought it was a good idea to put one in Wikipedia:Today's featured article/October 31, 2022.
AnomieBOT (talk | contribs)
Updating published sources: TFATitleSubpageCreator: * Start maintaining a JSON version of the data, currently in the bot's userspace.
Line 23:
 
use AnomieBOT::Task qw/:time/;
use Time::Piece;
use vars qw/@ISA/;
@ISA=qw/AnomieBOT::Task/;
Line 49 ⟶ 50:
 
$api->task('TFATitleSubpageCreator', 0, 10, qw/d::Talk d::Templates/);
 
my $jsontok = $api->edittoken( 'User:AnomieBOT/TFA title.json' );
if($jsontok->{'code'} eq 'shutoff'){
$api->warn("Task disabled: ".$jsontok->{'content'}."\n");
return 300;
}
if($jsontok->{'code'} ne 'success'){
$api->warn("Failed to get edit token for User:AnomieBOT/TFA title.json: ".$jsontok->{'error'}."\n");
return 300;
}
 
my $json;
eval { $json = JSON->new->decode( $jsontok->{'revisions'}[0]{'slots'}{'main'}{'*'} // '' ); };
if ( $@ ) {
$api->warn("Failed to parse User:AnomieBOT/TFA title.json: $@\n");
return 300;
}
my @jsonupdates = ();
 
my $broken = 0;
Line 128 ⟶ 147:
next;
}
}
 
my $dt = Time::Piece->strptime( $date, '%B %d, %Y' )->strftime( '%Y-%m-%d' );
if ( $link ne ( $json->{$dt} // '' ) ) {
$json->{$dt} = $link;
push @jsonupdates, "[[$title|$dt]] is [[$link]]";
}
}
 
if ( @jsonupdates ) {
my $outtxt = JSON->new->pretty->canonical->encode( $json );
my $r = $api->edit( $jsontok, $outtxt, "Updates: " . join( '; ', @jsonupdates ), 1, 1 );
if ( $r->{'code'} ne 'success' ) {
$api->warn( "Write failed on User:AnomieBOT/TFA title.json: " . $r->{'error'} . "\n" );
$broken = 1;
}
}