use strict;
use LWP;
use Term::ReadKey;
use Getopt::Std;
use XML::Simple;
use HTTP::Request::Common;
use HTML::Template;
use constant URL => 'http://www.perlmonks.org/';
$| = 1;
use vars qw(%opts);
getopts('p:u:h',\%opts);
my ($user,$passwd,$help) = parse_args(\%opts);
USAGE() and exit unless $user and $passwd and not $help;
my $ua = LWP::UserAgent->new;
$ua->agent("all_node_grabber($user)/1.0 (" . $ua->agent .')');
# log in and access your nodes info xml page
my $request = POST(URL, Content => [
op => 'login',
user => $user,
passwd => $passwd,
node => 'User nodes info xml generator',
]);
my $response = $ua->request($request);
# prepare xml
my $xml = $response->content();
# i have two nodes that have the registerd and copyright
# symbols in them - the next two lines were necesary - YMMV
$xml =~ s/©//g;
$xml =~ s/®//g;
$xml = XMLin($xml,forcearray=>1) or die "xml error!";
# munge data
my %bookmarks;
my $marker = qr/<!--\s*([a-z]+)\s*-->/; # YMMV here too
while(my($node_id,$hash) = each %{$xml->{'NODE'}}) {
my $content = $hash->{'content'};
my ($category) = $content =~ /$marker/;
next unless $category;
# the next three lines are specific to me - YMMV
$category = ucfirst $category . 's';
$content =~ s/$marker(?:\($user\))*\s*\d*//;
$content =~ s/(.{24}).{3,}/$1.../;
push @{$bookmarks{$category}}, {id=>$node_id, content=>$content};
}
# prepare template
my $html = do {local $/; <DATA>};
my $template = HTML::Template->new( scalarref => \$html);
$template->param(
categories => [
map {{
category => $_,
nodes => $bookmarks{$_}
}} sort keys %bookmarks # feel free to customize this sort
]);
# print template
print $template->output();
sub parse_args {
my %opt = %{+shift};
if (exists $opt{'p'} and not defined $opt{'p'} and defined $opt{'u'
+}) {
print "Enter password: ";
ReadMode 'noecho';
chomp($opt{'p'} = ReadLine 0);
ReadMode 'normal';
}
return @opt{qw(u p h)};
}
sub USAGE { print "USAGE: $0 -u user -p -password\n" }
=pod
=head1 NAME
node_bookmark_lister.pl - LWP script
=head1 DESCRIPTION
This is a simple script that uses LWP, XML::Simple, and
HTML::Template to produce an HTML table of nodes whose
titles you have marked with an HMTL comment, such as
<!--code--> or <!--neatstuff-->. The HTML template is
included in the DATA handle for you convenience, simply
change it to suite your needs.
=head1 SYNOPSIS
for *nix:
./node_bookmark_lister.pl -u uname -p
for win32:
perl node_bookmark_lister.pl -u uname -p
Invokes the script for the specified username and
interactively prompt for password if none specified.
=cut
__DATA__
<table width="200" border="1">
<tmpl_loop name="categories">
<tr><td>
<font face="arial,geneva,sans-serif">
<u><tmpl_var name="category"></u>
<font size="2">
<ul>
<tmpl_loop name="nodes">
<li>[id://<tmpl_var name="id">|<tmpl_var name="content">]
</tmpl_loop>
</ul>
</font></font>
</td></tr>
</tmpl_loop>
</table>
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.