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>
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
Outside of code tags, you may need to use entities for some characters:
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
|
|