Материал из Википедии — свободной энциклопедии
#! /usr/bin/perl -w
###
# AFD - MediaWiki bot.
# Copyright (C) 2006 Edward Chernenko.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
###
$VERSION = "1.0.1";
$lang = "ru";
$user = "<ИМЯ_БОТА>";
$pass = "<ПАРОЛЬ>";
$PAGE = "Участник:$user\/К удалению";
@months = qw(
января февраля марта апреля мая июня июля
августа сентября октября ноября декабря);
$report = "
Эту страницу автоматически генерирует [[Участник:$user|$user]].
Она представляет из себя попытку создания списка наблюдения за ВП:КУ при использовании страниц удаления по дням. __NOTOC__
== Список наблюдения ==
";
###
use POSIX qw(locale_h);
use locale;
setlocale(LC_CTYPE, "ru_RU.utf8");
my $time = time();
@dates = ();
for(my $i = 0; $i < 7; $i ++)
{
my($sec, $min, $hour, $mday, $mon, $year) = localtime($time);
$year += 1900;
my $date = $mday . " " . $months[$mon] . " " . $year;
push @dates, $date;
$time -= 24*60*60;
}
###
use CMS::MediaWiki;
use LWP::UserAgent;
my $ua = LWP::UserAgent->new('agent' => 'afd.pl') or die;
my $mw = CMS::MediaWiki->new(
host => "$lang.wikipedia.org",
path => "w",
debug => 0
) or die;
@items = ();
foreach $date(@dates)
{
my $res = $ua->get("http://ru.wikipedia.org/w/index.php?title=Википедия:К удалению/$date\&action=history&limit=2&useskin=simple");
next unless $res->is_success();
my $text = $res->content;
$text =~ s/%([0-9a-fA-F][0-9a-fA-F])/pack("C", hex($1))/eg;
$text =~ s/.*?<li>(.*?)<\/li>.*/$1/s; # or next;
$text =~ m/(?<=diff\=)[0-9]+/; my $diff = $&;
$text =~ m/(?<=oldid\=)[0-9]+/;
$diff = "diff=$diff\&oldid=$&";
$text =~ s/.*?name\=\"diff\" \/>//s; # or next;
$text =~ m/<a.*?title\="Википедия:К удалению\/$date">.*?<\/a>/;
my $time = link2title(a2link($&));
$text =~ m/<span class\='history\-user'>.*?<\/span>/s; # or next;
my $user = $&;
my $comment = "";
if($text =~ m/<span class\='comment'>.*?<\/span>\)<\/span>/s)
{
$comment = $&;
$comment =~ s/<span.*?>//g;
$comment =~ s'</span>''g;
}
while($comment =~ m/<a.*?>.*?<\/a>/g)
{
substr($comment, pos($comment) - length($&), length($&)) =
a2link($&);
}
$user =~ s/.*?title\="Участник:(.*?)">\1<\/a><\/span>/[[Участник:$1|$1]]/g;
$user =~ s/.*?target=(.*?).*?>\1<\/a>/[[Участник:$1|$1]]/g;
push @items, {
'diff' => $diff, # Id правки, напр. "diff=947412&oldid=946493"
'date' => $date, # Дата страницы, например "24 марта 2006"
'time' => $time, # Время и дата посл. изменения, "06:18, 25 марта 2006"
'user' => $user, # Ссылка на пользователя
'comm' => $comment # Комментарий, или описание правки
};
}
@items = sort report_cmp @items;
my $last_edit_date;
foreach $item(@items)
{
my($edit_time, $edit_date) = split /\, /, $item->{time};
if(!$last_edit_date || $last_edit_date ne $edit_date)
{
$report .= "=== $edit_date ===\n";
$last_edit_date = $edit_date;
}
$diff_link = "http://$lang.wikipedia.org/w/index.php?title=Википедия:К удалению/" . $item->{date} . "&" . $item->{diff};
$diff_link =~ s/ /%20/g;
$report .= "* ([$diff_link diff]) <tt>$edit_time<\/tt> {{nobr| }} '''[[Википедия:К удалению/" . $item->{date} . "|" . $item->{date} . "]]''' " . $item->{user} . " " . short_comment($item->{date}, $item->{comm}) . "\n";
}
$report .= "\n~~~~";
my %last = %{$items[0]};
{
my $_l;
open F, ".last_edit_date" or goto write_last_date;
read F, $_l, -s F;
close F;
exit(0) if(report_date_ind($last{time}) == report_date_ind($_l));
write_last_date:
open F, ">.last_edit_date";
print F $last{time};
close F;
}
$summary = short_date($last{date}) . ", $last{user}: " . short_comment($last{date}, $last{comm});
$mw->login( user => $user, pass => $pass );
$mw->editPage(
title => $PAGE,
section => '',
text => $report,
summary => $summary
);
sub report_cmp
{
return report_date_ind($b->{time}) <=> report_date_ind($a->{time});
}
sub report_date_ind
{
my $date = shift;
my($hour, $other, $min, $day, $mon, $year);
($hour, $other) = split /\:/, $date;
($min, $other) = split /\, /, $other;
($day, $mon, $year) = split / /, $other;
my $i = 0;
foreach my $month(@months)
{
if($month eq $mon)
{
$mon = $i;
next;
}
$i ++;
}
my $ind = ($year * 12 + $mon) * 32 + $day;
$ind = ($ind * 24 + $hour) * 60 + $min;
return $ind;
}
sub a2link
{
my $link = shift;
my @a = split("href=\"", $link);
return "" unless($a[1]);
@a = split("\"", $a[1]);
my $href = $a[0]; shift @a;
my $rest = join("\"", @a);
@a = split />/, $rest;
@a = split /</, $a[1];
my $text = $a[0];print $href
if($href =~ /target/);
if($href =~ /\/w\/index\.php\?title\=/)
{
substr $href, 0, length("/w/index.php?title="), "";
$href =~ s/\&oldid\=[0-9]+//g;
$href =~ s/\&action=edit//g;
}
if($href =~ /\/wiki\//)
{
substr $href, 0, length("/wiki/"), "";
}
if($href =~ /^Изображение/)
{
my $tmp = $href;
$href = $text;
$text = $href;
}
return "[[$href|$text]]";
}
sub short_date
{
my($day, $mon, $year) = split / /, shift;
my $i = 1;
foreach my $month(@months)
{
if($month eq $mon)
{
$mon = $i;
next;
}
$i ++;
}
$day = "0$day" if($day < 10);
$mon = "0$mon" if($mon < 10);
$year = substr $year, 2, 2;
return "$day.$mon.$year";
}
sub link2title
{
my $link = shift;
my @a = split /\|/, $link;
@a = split /\]\]/, $a[1];
return $a[0];
}
sub short_comment
{
my($date, $comment) = @_;
my $title;
my @parts = split /\|/, $comment;
if(@parts > 2)
{
my @a = split /\]\]/, $parts[2];
$title = $a[0];
}
return "[[Википедия:К удалению/$date|ВП:КУ/$date]]"
unless $title;
$title =~ s'\<s\>''g;
$title =~ s'\</s\>''g;
$title =~ s/\.([0-9a-fA-F][0-9a-fA-F])/pack("C", hex($1))/eg;
my $anchor = $title;
$anchor =~ s/^://;
return "[[Википедия:К удалению/$date#$anchor|→]][[$title]]";
}