added utf8 decoding of wikilinker urls
[gpy.git] / WikinewsSpam.pm
blobcbce3b3f1293b57c36040e50afc6def613b436e7
1 #!/usr/local/bin/perl
3 package Bot::BasicBot::Pluggable::Module::WikinewsSpam;
4 use Bot::BasicBot::Pluggable::Module;
5 use base qw(Bot::BasicBot::Pluggable::Module);
7 use strict;
8 use warnings;
9 use MediaWiki::API;
10 use Data::Dumper;
11 use WWW::Shorten::TinyURL;
12 use DateTime::Format::Strptime;
13 use DateTime::Format::Duration;
14 use DateTime::Format::Human::Duration;
17 # Settings. You can edit these.
18 my $s_chan = "#wikinews-spam";
19 my %check_period = ( # both in seconds
20 'all' => 60*60*24,
21 'new' => 60*5,
24 # DO NOT EDIT BELOW THIS LINE UNLESS YOU KNOW WHAT YOU ARE DOING
26 # Secondary variables.
27 my %check_next = ( # both in seconds
28 'all' => 0,
29 'new' => 0,
32 my $mw = MediaWiki::API->new();
33 $mw->{config}->{api_url} = 'https://en.wikinews.org/w/api.php';
34 my $strp = DateTime::Format::Strptime->new(
35 pattern => '%Y-%m-%dT%H:%M:%SZ',
36 locale => 'en_AU',
37 time_zone => 'UTC',
39 my @known_pageids;
40 my $human_duration_parser = DateTime::Format::Human::Duration->new();
41 my $last_full_announce_ago = 0; # minutes
43 # Announce an article in channel
44 sub announce_page{
45 my $self = shift;
46 my $article_ref = shift;
48 # Get page url by id
49 my $info_ref = $mw->api ( {
50 action => 'query',
51 prop => 'info',
52 inprop => 'url',
53 pageids => $article_ref->{pageid},
54 } ) or die $mw->{error}->{code} . ': ' . $mw->{error}->{details};
55 my $url = WWW::Shorten::TinyURL::makeashorterlink($info_ref->{query}{pages}{$article_ref->{pageid}}{fullurl});
56 $url =~ s/^http/https/;
58 # Human readable time ago
59 my $time_now = DateTime->now;
60 my $time_published = $strp->parse_datetime($article_ref->{timestamp});
61 my $time_ago = $human_duration_parser->format_duration_between($time_now,$time_published);
63 # Title
64 my $title = $article_ref->{title};
66 my $message = "$url submitted for review *$time_ago* ago";
68 # Under review or not?
69 my $cat_info_ref = $mw->api ( {
70 action => 'query',
71 prop => 'categories',
72 cllimit => 10,
73 clcategories=> 'Category:Under review',
74 pageids => $article_ref->{pageid},
75 } ) or die $mw->{error}->{code} . ': ' . $mw->{error}->{details};
76 if (exists $$cat_info_ref{query}{pages}{$article_ref->{pageid}}{categories}){
77 $message .= " - *under review*";
80 # Add title and say in channel
81 $message .= " - $title";
82 $self->say(channel => $s_chan, body => $message);
84 if (!($article_ref->{pageid} ~~ @known_pageids)){
85 push @known_pageids, $article_ref->{pageid};
90 sub get_queue{
91 my $articles_ref = $mw->list ( {
92 action => 'query',
93 list => 'categorymembers',
94 cmtitle => 'Category:Review',
95 cmprop => 'ids|title|timestamp',
96 cmnamespace => 0,
97 cmlimit => 10,
98 cmsort => 'timestamp',
99 } ) or die $mw->{error}->{code} . ': ' . $mw->{error}->{details};
100 return $articles_ref;
103 sub tick{
104 my $self = shift;
105 my @new_articles;
109 # Announce all
110 if ($check_next{'all'} == 0){
111 my $articles_ref = get_queue();
112 my $message = (scalar $articles_ref == 0) ? "Empty review queue" : "Review queue:";
113 @new_articles = @{$articles_ref};
114 $self->say(channel => $s_chan,
115 body => $message,
119 # Announce recent
120 if ($check_next{'new'} == 0){
121 my $articles_ref = get_queue();
122 @new_articles
123 = grep {!($_->{pageid} ~~ @known_pageids)} @{$articles_ref};
126 for my $article (@new_articles) {$self->announce_page($article);}
128 #$self->say(channel=>$s_chan,body=>"First known pageid: $known_pageids[0]");
130 # Increment checked_last times
131 foreach(keys %check_next){
132 $check_next{$_}
133 = $check_next{$_} <= 0
134 ? $check_period{$_}
135 : $check_next{$_} - 5;
140 sub help{
141 return "Wikinews Review queue spam module.";
146 #_ _END_ _