From 2316d13ca950399e3c04965c09038fd0a279ff23 Mon Sep 17 00:00:00 2001 From: Gryllida A Date: Tue, 15 Jan 2013 07:18:36 +1030 Subject: [PATCH] first commit --- README.md | 0 WikiLinker.pm | 61 +++++++++++++++++++++++ WikinewsSpam.pm | 147 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ bot.pl | 25 ++++++++++ 4 files changed, 233 insertions(+) create mode 100644 README.md create mode 100644 WikiLinker.pm create mode 100644 WikinewsSpam.pm create mode 100644 bot.pl diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/WikiLinker.pm b/WikiLinker.pm new file mode 100644 index 0000000..6ad583a --- /dev/null +++ b/WikiLinker.pm @@ -0,0 +1,61 @@ +#!/usr/local/bin/perl + +package Bot::BasicBot::Pluggable::Module::WikiLinker; +use Bot::BasicBot::Pluggable::Module; +use base qw(Bot::BasicBot::Pluggable::Module); + +use strict; +use warnings; +use MediaWiki::API; +use HTML::TreeBuilder 5 -weak; +use Data::Dumper; + +# Secondary variables. +my $mw = MediaWiki::API->new(); +$mw->{config}->{api_url} = 'https://en.wikinews.org/w/api.php'; + +sub told{ + shift->process_message(@_); +} + +sub emoted { + shift->process_message(@_); +} + +sub process_message{ + my ($self, $msg, $pri) = @_; + my $body = $msg->{body}; + my $who = $msg->{who}; + my $channel = $msg->{channel}; + my @urls; + while ($body =~ m{\[\[(.*?)\]\]}g){ + push @urls, get_url_by_text($1); + } + while ($body =~ m{\{\{(.*?)\}\}}g){ + push @urls, get_url_by_text("Template:$1"); + } + return join " ", @urls; +} + +sub get_url_by_text{ + my $wiki_text = shift; + # wiki text to html + my $info_ref = $mw->api ( { + action => 'parse', + prop => 'text', + text => '{{fullurl:'.$wiki_text.'}}', + } ) or die $mw->{error}->{code} . ': ' . $mw->{error}->{details}; + my $html = $info_ref->{parse}{text}{'*'}; + # parse html + my $tree = HTML::TreeBuilder->new_from_content($html); + return 'https:'.$tree->look_down('_tag','p')->content_array_ref->[0]; +} + +sub help{ +return "parser for [[*]] in messages"; +} + +1; + +#_ _END_ _ + diff --git a/WikinewsSpam.pm b/WikinewsSpam.pm new file mode 100644 index 0000000..cbce3b3 --- /dev/null +++ b/WikinewsSpam.pm @@ -0,0 +1,147 @@ +#!/usr/local/bin/perl + +package Bot::BasicBot::Pluggable::Module::WikinewsSpam; +use Bot::BasicBot::Pluggable::Module; +use base qw(Bot::BasicBot::Pluggable::Module); + +use strict; +use warnings; +use MediaWiki::API; +use Data::Dumper; +use WWW::Shorten::TinyURL; +use DateTime::Format::Strptime; +use DateTime::Format::Duration; +use DateTime::Format::Human::Duration; + + +# Settings. You can edit these. +my $s_chan = "#wikinews-spam"; +my %check_period = ( # both in seconds +'all' => 60*60*24, +'new' => 60*5, +); + +# DO NOT EDIT BELOW THIS LINE UNLESS YOU KNOW WHAT YOU ARE DOING + +# Secondary variables. +my %check_next = ( # both in seconds +'all' => 0, +'new' => 0, +); + +my $mw = MediaWiki::API->new(); +$mw->{config}->{api_url} = 'https://en.wikinews.org/w/api.php'; +my $strp = DateTime::Format::Strptime->new( + pattern => '%Y-%m-%dT%H:%M:%SZ', + locale => 'en_AU', + time_zone => 'UTC', +); +my @known_pageids; +my $human_duration_parser = DateTime::Format::Human::Duration->new(); +my $last_full_announce_ago = 0; # minutes + +# Announce an article in channel +sub announce_page{ + my $self = shift; + my $article_ref = shift; + + # Get page url by id + my $info_ref = $mw->api ( { + action => 'query', + prop => 'info', + inprop => 'url', + pageids => $article_ref->{pageid}, + } ) or die $mw->{error}->{code} . ': ' . $mw->{error}->{details}; + my $url = WWW::Shorten::TinyURL::makeashorterlink($info_ref->{query}{pages}{$article_ref->{pageid}}{fullurl}); + $url =~ s/^http/https/; + + # Human readable time ago + my $time_now = DateTime->now; + my $time_published = $strp->parse_datetime($article_ref->{timestamp}); + my $time_ago = $human_duration_parser->format_duration_between($time_now,$time_published); + + # Title + my $title = $article_ref->{title}; + + my $message = "$url submitted for review *$time_ago* ago"; + + # Under review or not? + my $cat_info_ref = $mw->api ( { + action => 'query', + prop => 'categories', + cllimit => 10, + clcategories=> 'Category:Under review', + pageids => $article_ref->{pageid}, + } ) or die $mw->{error}->{code} . ': ' . $mw->{error}->{details}; + if (exists $$cat_info_ref{query}{pages}{$article_ref->{pageid}}{categories}){ + $message .= " - *under review*"; + } + + # Add title and say in channel + $message .= " - $title"; + $self->say(channel => $s_chan, body => $message); + + if (!($article_ref->{pageid} ~~ @known_pageids)){ + push @known_pageids, $article_ref->{pageid}; + } + +} + +sub get_queue{ + my $articles_ref = $mw->list ( { + action => 'query', + list => 'categorymembers', + cmtitle => 'Category:Review', + cmprop => 'ids|title|timestamp', + cmnamespace => 0, + cmlimit => 10, + cmsort => 'timestamp', + } ) or die $mw->{error}->{code} . ': ' . $mw->{error}->{details}; + return $articles_ref; +} + +sub tick{ + my $self = shift; + my @new_articles; + + + + # Announce all + if ($check_next{'all'} == 0){ + my $articles_ref = get_queue(); + my $message = (scalar $articles_ref == 0) ? "Empty review queue" : "Review queue:"; + @new_articles = @{$articles_ref}; + $self->say(channel => $s_chan, + body => $message, + ); + } + + # Announce recent + if ($check_next{'new'} == 0){ + my $articles_ref = get_queue(); + @new_articles + = grep {!($_->{pageid} ~~ @known_pageids)} @{$articles_ref}; + } + + for my $article (@new_articles) {$self->announce_page($article);} + + #$self->say(channel=>$s_chan,body=>"First known pageid: $known_pageids[0]"); + + # Increment checked_last times + foreach(keys %check_next){ + $check_next{$_} + = $check_next{$_} <= 0 + ? $check_period{$_} + : $check_next{$_} - 5; + } +} + + +sub help{ +return "Wikinews Review queue spam module."; +} + +1; + +#_ _END_ _ + diff --git a/bot.pl b/bot.pl new file mode 100644 index 0000000..189b7f1 --- /dev/null +++ b/bot.pl @@ -0,0 +1,25 @@ +#!/usr/local/bin/perl +use strict; +use warnings; +use Bot::BasicBot::Pluggable; + +my $bot = Bot::BasicBot::Pluggable->new( + + channels => ["#wikinews-spam"], + server => "irc.gnu.org", + port => "6667", + + nick => "wnbot", + altnicks => ["pbot", "pluggable"], + username => "bot", + name => "Yet Another Pluggable Bot", + + ); + +$bot->load('WikiLinker'); +$bot->load('WikinewsSpam'); +$bot->load('Log'); +$bot->run(); + +#_ _END_ _ + -- 2.11.4.GIT