peoplewelcome-a-bit for Wikinews - People at Savannah: View a Job

 
 

add category count to the subject line for welcome-a-bit for Wikinews

Category: Developer
Submitter: svetlana
Date: Fri 22 Feb 2019 07:50:13 AM UTC
Status: Open

This group is not part of the GNU Project.

This project is designed to encourage involvement of people with newly submitted articles at Wikinews by sending them instant notifications of new submissions in their topics of interest.

To sign up, add your name to the page https://en.wikinews.org/wiki/User:Gryllida/welcome_a_bit .

Currently we support delivery of notifications by e-mail. We may deliver the notifications to wiki pages, Android push notifications, or IRC in the future; if you are interested in any of these methods please send me a message.

This project is in the beta stage. You are encouraged to test it and share your feedback. To report any bugs or suggestions, please leave a message using any of the below venues:

- e-mail -email is unavailable-
- open an issue using the 'issues' link at the top
- query live chat at #wikinews at freenode
- leave a message on-wiki at https://en.wikinews.org/wiki/User_talk:Gryllida/welcome_a_bit

Draft version



# Copyright (C) 2018 Svetlana Tkachenko svetlana@members.fsf.org
#
#    This file is part of WeABit, Welcome-a-bit for Wikinews.
#
#    WeABit 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 3 of the License, or
#    (at your option) any later version.
#
#    WeABit 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.
#
#    You should have received a copy of the GNU General Public License
#    along with WeABit.  If not, see <https://www.gnu.org/licenses/>.

# 1. read in a list of rss feeds and usernames
# 2.

use strict;
use warnings;

use open ':std', ':encoding(UTF-8)';

use MediaWiki::API;
use LWP::Simple;

use Data::Dumper;
use String::Util qw(trim);
use POSIX qw(strftime);

# use wabget;

my $mw = MediaWiki::API->new();
my $wikiURL = 'https://en.wikinews.org'; # /w/api.php';
$mw->{config}->{api_url} = "$wikiURL/w/api.php";

while(1){
  my $datestring = strftime "%a %b %e %H:%M:%S %Y", localtime;
  printf("date and time - $datestring\n");
  # get category members of 'Gryllida/WAB'
  my $members_ref = $mw->api ( {
    action      => 'query',
    list => 'categorymembers',
    cmtitle => 'Category:Gryllida/WAB4',
    cmlimit => 500
  });
  my $members = $members_ref->{query}->{categorymembers};
  my $n = scalar(@{$members});

  # For each WAB tracking page, extract the username and categories therein.
  my $tracker={};
  my $oldStamp = 0;
  for (my $i=0; $i < $n; $i++) {
    my $output = {};
    my $title = $members->[$i]->{title};
    my @partsOfTitle = split /\//, $title;
    $partsOfTitle[0] =~ s/User\://;
    my $username =$partsOfTitle[0];
    my $typeOfArticles =$partsOfTitle[2]; # dev or pub
    my $deliveryType =$partsOfTitle[3]; # email or wiki
    # Get the user's time stamp
    my $talkPageTitle = "User talk:$username/wab/$typeOfArticles/$deliveryType";
    my $url2 = "$wikiURL/wiki/$talkPageTitle?action=raw";
    my $content2 = get($url2);
    my @lines2 =  split /^/, $content2;
    my $userTs = $lines2[0];
    print "User time stamp: $userTs (for $username) \r\n";
    # Get list of categories of the user
    my $url = "$wikiURL/wiki/$title?action=raw";
    my $content = get($url);
    my @lines =  split /^/, $content;

    for my $line (@lines){
      chomp($line);
      if ($line =~ s/\*(.*)/$1/){
        my $cat = trim($line);

        my $uinfo = {
          'deliveryType' => $deliveryType,
          'talkPageTitle' => $talkPageTitle,
          'userTs' => $userTs,
          'username' => $username
        };
        push @{$tracker->{$cat}->{$typeOfArticles}}, $uinfo;
        if($userTs > $oldStamp){
          $oldStamp = $userTs;
        }
      }
    }
  }
  # rcstart = $oldStamp
  my $raw_newpages_ref = $mw->api ( {
    action      => 'query',
    list => 'recentchanges',
#   rcstart => $oldStamp, # XXX add this
    rctype => 'categorize',
    rcprop => 'user|title|ids'
#   rcnamespace => 0,
#   prop => 'title'
  });

  my $newpages_ref = $raw_newpages_ref->{query}->{recentchanges};

  my $deliveryBuffer = {};
  for my $newpageinfo (@{$newpages_ref}){
    print Dumper $newpageinfo;
    my $categorizer = $newpageinfo->{user};
    my $pageId = $newpageinfo->{pageid};
    $newpageinfo->{title} =~ s/.*?:(.*)/$1/;
    my $catName = $newpageinfo->{title};
    print "Category: $catName\r\n";

    if(!exists $tracker->{$catName}){next;}
    print "Processing category '$catName'\r\n";


    my $raw_pagecats = $mw->api ( {
      action      => 'query',
      prop => 'categories',
      pageids => $pageId,
    });
    print "Raw $pageId page category data: ";
    my $ns = $raw_pagecats->{query}->{pages}->{$pageId}->{ns};
    my $cats = $raw_pagecats->{query}->{pages}->{$pageId}->{categories};
    my $catsTitles = [map { $_->{title} } @$cats];
    map {s/Category\://g; } @{$catsTitles};
    print Dumper $catsTitles;
    print "...done.\r\n";
    my $articleType = '';
    if('Published' ~~ $catsTitles){
      $articleType = 'pub';
    }else{
      $articleType = 'dev';
    }

    #2. Add to delivery
    if(!exists $tracker->{$catName}->{$articleType}){next;}
    print "Processing category '$catName'\r\n";
    print Dumper  $tracker->{$catName}->{$articleType};
    my $deliveryTargets = $tracker->{$catName}->{$articleType};
    for my $deliveryTarget (@{$deliveryTargets}){
      if($deliveryBuffer->{$deliveryTarget->{username}}->{$pageId}){next;}
      print "Page ID: $pageId\r\n";
      $deliveryBuffer->{$deliveryTarget->{username}}->{$pageId} = $deliveryTarget;
      print Dumper $deliveryBuffer;
    }
  }
  # print Dumper $deliveryBuffer;
  exit;
  sleep(600); # check every 10 minutes
}


License GNU General Public License v3 or later


Development Status
: 4 - Beta

Details (job description, contact ...):

The $hr->{entries} hashref has categories for each entry in the format 'cats' => \['Foo', 'Bar']. These categories can be shown next to each entry in the content of the message, and the total count for each category ('5 about Australia, 4 about Science') can be added to the subject line.

To get help with this job, please check the main page of this project and get in touch.

Required Skills:

Skill Level Experience

(No skill inventory set up)

Back to the top

Powered by Savane 3.14-f13d.
Corresponding source code