Jump to content

User:Adodge/WLWP

From Wikipedia, the free encyclopedia

WLWP v0.1b

[edit]

What it does

[edit]

WLWP is a perl script to take your watchlist and turn it into a single RSS feed on your local computer, which can then be subscribed to with a feed reader. Generally, it should be run in a crontab. This is my crontab:

25,55 * * * * ~/bin/wlwp -u adodge -p PASSWORD -o ~/Sites/wprss.rss -q

Of course, I removed my password.

WLWP can also load a file with a list of articles and create a feed from that. This way, you can create different feeds for different topics, etc. Also, if you load from a file, WLWP doesn't need your login credentials every time it refreshes.

WLWP is currently a beta version, with a total of 45 minutes of work put into it. If you find any bugs or have a feature request, let me know.

Contact

[edit]

You can contact me at my email address alexdodge at gmail dot com, or by posting to the talk page here. I also have a website at alexdodge.net, where I will blog about new developments if I make them.

Installation

[edit]

Just copy the code below into a file called "wlwp" and stick it somewhere in your PATH. Then you can set up your crontab and subscribe to the resulting feed.

You may have to install some perl modules to get this to work. It uses XML::FeedPP, URI::Escape, and Getopt::Std.

You can run "wlwp -h" for more information.

Code

[edit]
#!/usr/bin/perl
# Watchlist Wikipedia v0.1b
# Alexdodge.net
# Licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported
# http://creativecommons.org/licenses/by-nc-sa/3.0/
use strict;
use warnings;
use XML::FeedPP;
use URI::Escape;
use Getopt::Std;

my $version = "WLWP v0.1b (http://alexdodge.net/)";

my %opts = (
	u => undef,  #-u <username>
	p => undef,  #-p <password>
	o => undef,  #-o <FILE> Select output file.
	t => undef,  #-t Don't list talk pages.
	s => undef,  #-s Output to STDOUT
	q => undef,  #-q Quiet mode
	d => undef,  #-d <FILE> Output subscription list to file. (Doesn't create RSS Feed.)
	l => undef,  #-l <FILE> Load subscription list from file.
	n => undef,  #-n <name> Name of feed
	h => undef,  #-h Show Help
	v => undef,
	n => undef,
);
getopts('n:u:p:o:tsqd:l:hv', \%opts);

if(defined $opts{'h'}){
	print <<_HELP_;
USAGE: wlwp [-u <USERNAME> -p <PASSWORD> [-d <FILE>]] [-l <FILE>] [-o <FILE>] [-n <NAME>] -tsqhv
wlwp creates an rss feed from a wikipedia watchlist or a file with a list of wikipedia articles.

$version

Licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported
http://creativecommons.org/licenses/by-nc-sa/3.0/

EXAMPLE OF USE
wlwp -u adodge -o wlwp.rss

OPTIONS
	-u <USERNAME>
USERNAME is the username of the user on wikipedia.  If this is not specified and -l is not set, the user will be prompted interactively.

	-p <PASSWORD>
PASSWORD is the password of the user on wikipedia.  If this is not specified and -l is not set, the user will be prompted interactively.

	-d <FILE>
If set, wlwp will dump the watchlist into FILE.  No rss feed will be created.

	-l <FILE>
If set, wlwp will load the watchlist from FILE.  If set, the username and password need not be provided.

	-o <FILE>
wlwp will output the rss feed to FILE.  If this is not specified and -s is not set, the user will be prompted interactively.

	-n <NAME>
NAME is the title of the RSS feed.  If this is not set, the title will be either the username or the watchlist filename.

SWITCHES
	-t
Do not subscribe automatically to article talk pages.

	-s
If -o is not set, do not prompt the user interactively.  Instead, output to STDOUT.

	-q
Quiet mode.  Interactive prompts will still be printed.

	-h
Show this help and quit.

	-v
Print version information and quit.

_HELP_
	exit;
}

if(defined $opts{'v'}){
	print $version."\n";
	exit;
}


my ($username, $password, $outfile);

if(!defined $opts{'l'}){
	if(defined($opts{'u'})){
		$username = $opts{'u'};
	}else{
		print STDERR "Enter username: ";
		$username = <STDIN>;
		chomp $username;
	}

	if(defined($opts{'p'})){
		$password = $opts{'p'};
	}else{
		print STDERR "Enter password: ";
		$password = <STDIN>;
		chomp $password;
	}
}

my $title;

if(defined $opts{'n'}){
	$title = $opts{'n'};
}elsif(defined $opts{'l'}){
	$title = $opts{'l'};
}else{
	$title = $username;
}

if(!defined $opts{'d'}){
	if(defined($opts{'o'})){
		$outfile = $opts{'o'};
	}else{
		if(!defined($opts{'s'})){
			print STDERR "Enter file for output (blank for STDOUT): ";
			$outfile = <STDIN>;
			chomp $outfile;
		}
	}
}

my @list;

if(!defined $opts{'l'}){
	`curl -c ~/.wlwpcookies -A "$version" -d wpName=$username -d wpPassword=$password -d wpRemember=1 -d wpLoginattempt=Log+in "http://en.wikipedia.org/w/index.php?title=Special:Userlogin&action=submitlogin&type=login&returnto=Special:Watchlist" 2> /dev/null`;
	my $data = `curl -b ~/.wlwpcookies -A "$version" "http://en.wikipedia.org/w/index.php?title=Special:Watchlist&action=raw" 2> /dev/null`;
	die("Failure") if !($data =~ /<textarea id="titles" name="titles" rows="25" cols="80">(.*)<\/textarea>/s);
	@list = split("\n", $1);
}else{
	open (FD, '<', $opts{'l'});
	while(<FD>){
		chomp;
		push @list, $_;
	}
	close FD;
}

if(defined $opts{'d'}){
	open (FD, '+>', $opts{'d'});
	foreach my $item (@list){
		print FD $item."\n";
	}
	exit;
}

my @talkpages;

if(!defined $opts{'t'}){
	foreach my $o (@list){
		my $item = $o;
		if($item =~ /\w+:.*/){
			$item =~ s/(\w*):/$1_talk:/;
		}else{
			$item = "Talk:$item";
		}
		push(@talkpages, $item);
	}
}

my $feed = XML::FeedPP::RSS->new();
$feed->title("WLWP - $title");
$feed->description("$version");
$feed->link("http://en.wikipedia.org/w/index.php?title=Special:Watchlist");

foreach my $item_orig (@list, @talkpages){
	print STDERR "$item_orig ... " if(!defined $opts{'q'});
	my $item = uri_escape($item_orig);
	my $tmpfeed = XML::FeedPP::RSS->new("http://en.wikipedia.org/w/index.php?title=$item&action=history&feed=rss");
	if( uri_unescape(($tmpfeed->get_item(0))->description()) =~ /<p><\/p>\n<p>Can\x27t load revision \d+<\/p>/){
		print STDERR "DNE" if(!defined $opts{'q'});
	}else{
		print STDERR "ok" if(!defined $opts{'q'});
		my $i = 0;
		while(my $itemobj = $tmpfeed->get_item($i)){
			$itemobj->title($item_orig." - ".$itemobj->title());
			$i ++;
		}
		$feed->merge($tmpfeed);
	}
	print STDERR "\n" if(!defined $opts{'q'});
}

my $rss = $feed->to_string();

if(!defined $outfile || $outfile eq ""){
	print $rss;
}else{
	open(FD,"+>",$outfile);
	print FD $rss;
	close FD;
}