Utilisateur:ABotSupreme/Sources

Une page de Wikipédia, l'encyclopédie libre.

Robot en cours de réécriture ...

<syntaxhighlight lang="perl">

  1. !/usr/bin/perl
  2. ABotSupreme: Wikipedia Bot
  3. 07/11/2009

use strict; use warnings; use MediaWiki::API; use Time::Local; use LWP::Simple; use Encode; use Data::Dumper; use Getopt::Long; binmode(STDOUT, ":utf8");

(my $basename = $0) =~ s%.*/%%;

        1. Logs ####

my $logfile = "abotsupreme.log"; sub wr2log { my $date = localtime(); my $line = $_[0]; open(LOG, '>>', "$logfile"); binmode(LOG, ":utf8"); print LOG "[$date]\t$line\n"; close LOG; }

wr2log("$basename launched");

        1. Options ####

my ($help,$verbose,$index,$addz,$category,$username,$password); GetOptions( 'help|h' => \$help,

           'username=s'        => \$username,
           'password=s'        => \$password,
           'verbose|v'         => \$verbose,
           'index|i'           => \$index,
           'addz|a'            => \$addz,
           'categorie|c=s'     => \$category);
  1. Traitement des paramètres

Encode::from_to($category, "utf8", "iso-8859-1"); Encode::from_to($username, "utf8", "iso-8859-1"); Encode::from_to($password, "utf8", "iso-8859-1");

        1. Connexion ####

my $mw = MediaWiki::API->new(); $mw->{config}->{api_url} = 'http://fr.wikipedia.org/w/api.php'; $mw->login( { lgname => "$username", lgpassword => "$password" } ) or die $mw->{error}->{code} . ': ' . $mw->{error}->{details}; wr2log("User \"$username\" logged in");

        1. Liste des catégories à traiter ####

my $pages = $mw->list ( { action => 'query', list => 'categorymembers', cmtitle => "$category", cmnamespace => '14', format => 'yaml', cmlimit => 'max' } ) or die $mw->{error}->{code} . ': ' . $mw->{error}->{details};


my @categories = ($category); my $category_to_list; my @tmp_categories = @categories; wr2log("Creating categories list"); while (@tmp_categories) { $category_to_list = pop(@tmp_categories); print "$category_to_list:\n" if ($verbose); my $pages = $mw->list ( { action => 'query',

                               	list => 'categorymembers',
                              	 	cmtitle => "$category_to_list",

cmnamespace => '14',

                               	format => 'yaml',
                               	cmlimit => 'max' } ) or die $mw->{error}->{code} . ': ' . $mw->{error}->{details}; 

foreach (sort(@{$pages})) { print "\t$_->{title}\n" if ($verbose); if ($_->{title} =~ /Projet:|Wikip.dia:|Wikiprojet/) { wr2log("$_->{title} [skipped]") } else { wr2log("$_->{title}") } push @tmp_categories, $_->{title} unless ($_->{title} =~ /Projet:|Wikip.dia:|Wikiprojet/); push @categories, $_->{title} unless ($_->{title} =~ /Projet:|Wikip.dia:|Wikiprojet/); } }

  1. Création de l'index et de le liste des articles récents

my %articles; my $index_page_content = ; my ($prop,$page_date); my ($sec,$min,$hour,$day,$month,$year); sub make_index { wr2log("Creating Index"); foreach (@categories) { wr2log("$_"); my $articles = $mw->list ( { action => 'query',

                                    	 	  	list => 'categorymembers',
                                       		cmtitle => "$_",
                                       		cmnamespace => '0',
                                       		format => 'yaml',
                                       		cmlimit => 'max' } ) or die $mw->{error}->{code} . ': ' . $mw->{error}->{details};	

foreach (sort(@{$articles})) { $prop = get("http://fr.wikipedia.org/w/api.php?action=query&prop=revisions&titles=$_->{title}&rvprop=timestamp&rvdir=newer&rvlimit=1&format=txtfm"); if ($prop =~ /.*timestamp.*(\d{4})-(\d{2})-(\d{2})\w(\d{2}):(\d{2}):(\d{2}).*/i) { $year = $1; $month = ($2 - 1); $day = $3; $hour = $4; $min = $5; $sec = $6; $page_date = timelocal($sec,$min,$hour,$day,$month,$year); } wr2log("$_->{title} (Created @ $page_date)"); print "$_->{title} : $page_date\n"; $articles{"$_->{title}"} = $page_date; } }

        1. Contenu de la page Index ####

foreach (sort(keys(%articles))) { $index_page_content = "$index_page_content" . "* $_ (Discuter:$_)\n"; }

        1. Contenu de la pages Articles Récents ####
  1. Fonction de tri du hash par valeurs

sub sorthash_desc {

  $articles{$b} <=> $articles{$a};

}

  1. Création du tableau trié

my $recent_page_content = "Liste mise à jour automatiquement par ABotSupreme\n\n"; Encode::from_to($recent_page_content, "utf8", "iso-8859-1");

  1. Liste des 15 articles les + récents

my @sortedh = sort sorthash_desc keys(%articles); for (my $i=0 ; $i<15; $i++) { $recent_page_content = "$recent_page_content" . "* \[\[$sortedh[$i]\]\]\n" } print " $recent_page_content", "\n";

        1. Edition de la pages Index ####

my $idx_pagename = "Portail:Jazz/Index";

        my $ref1 = $mw->get_page( { title => $idx_pagename } );
        unless ( $ref1->{missing} ) {
        	my $timestamp = $ref1->{timestamp};
          	$mw->edit( {
            		action => 'edit',
            		title => $idx_pagename,
            		basetimestamp => $timestamp, # to avoid edit conflicts
            		text => "$index_page_content" } ) or die $mw->{error}->{code} . ': ' . $mw->{error}->{details};
        }
        1. Edition de la pages Articles Récents ####

my $rct_pagename = "Portail:Jazz/Articles_récents"; Encode::from_to($rct_pagename, "utf8", "iso-8859-1");

        my $ref2 = $mw->get_page( { title => $rct_pagename } );
        unless ( $ref2->{missing} ) {
               my $timestamp = $ref2->{timestamp};
               $mw->edit( {
                       action => 'edit',
                       title => $rct_pagename,
                       basetimestamp => $timestamp, # to avoid edit conflicts
                       text => "$recent_page_content" } ) or die $mw->{error}->{code} . ': ' . $mw->{error}->{details};
        }

}

        1. Appel des fonctions ####

&make_index;