From ed00c3e578839dd60adacfe7ba8265e1fb4396d7 Mon Sep 17 00:00:00 2001 From: Gryllida A Date: Sat, 23 Feb 2013 15:37:19 +1030 Subject: [PATCH] Added WikiLinker multi channels support --- WikiLinker.pm | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/WikiLinker.pm b/WikiLinker.pm index 6fdf08c..9518e14 100644 --- a/WikiLinker.pm +++ b/WikiLinker.pm @@ -11,9 +11,17 @@ use HTML::TreeBuilder 5 -weak; use URI::Escape; use Data::Dumper; +my %channels_urls = ( + '#wikinews-spam' => 'https://en.wikinews.org/w/api.php', + '#wikipedia-ru-spam' => 'https://ru.wikipedia.org/w/api.php', +); +my %mw; + # Secondary variables. -my $mw = MediaWiki::API->new(); -$mw->{config}->{api_url} = 'https://en.wikinews.org/w/api.php'; +for my $channel_name (keys %channels_urls) { + $mw{$channel_name} = MediaWiki::API->new(); + $mw{$channel_name}->{config}->{api_url} = $channels_urls{$channel_name}; +} sub told{ shift->process_message(@_); @@ -27,25 +35,29 @@ sub process_message{ my ($self, $msg, $pri) = @_; my $body = $msg->{body}; my $who = $msg->{who}; - my $channel = $msg->{channel}; + my $channel = $msg->{'channel'}; + if (!grep { $_ eq $channel } keys %channels_urls){ + return; + } my @urls; while ($body =~ m{\[\[(.*?)\]\]}g){ - push @urls, get_url_by_text($1); + push @urls, get_url_by_text($1, $channel); } while ($body =~ m{\{\{(.*?)\}\}}g){ - push @urls, get_url_by_text("Template:$1"); + push @urls, get_url_by_text("Template:$1", $channel); } return join " ", @urls; } sub get_url_by_text{ my $wiki_text = shift; + my $channel_name = shift; # wiki text to html - my $info_ref = $mw->api ( { + my $info_ref = $mw{$channel_name}->api ( { action => 'parse', prop => 'text', text => '{{fullurl:'.$wiki_text.'}}', - } ) or die $mw->{error}->{code} . ': ' . $mw->{error}->{details}; + } ) or die $mw{$channel_name}->{error}->{code} . ': ' . $mw{$channel_name}->{error}->{details}; my $html = $info_ref->{parse}{text}{'*'}; # parse html my $tree = HTML::TreeBuilder->new_from_content($html); -- 2.11.4.GIT