Influenced by
Piers Cawleys talk on the 12 Step , I confess that the following piece of code is crappy. I wrote this a few months ago, the Monastery NNTP bridge. Basically it fetches newest XML nodes and uses
Net::NNTP to store it on a (local) newsserver.
#!/usr/bin/perl -w
use Net::NNTP;
use strict;
use XML::Parser;
use LWP::Simple;
use Data::Dumper; #debugging purposes
my $server = "192.168.0.2"; #Server
my %nodetypes = (
'bookreview' => 'monastery.reviews',
'categorized answer' => 'monastery.questions',
'categorized question' => 'monastery.questions',
'CUFP' => 'monastery.coolusesforperl',
'modulereview' => 'monastery.reviews',
'monkdiscuss' => 'monastery.discussions',
'obfuscated' => 'monastery.obfuscation',
'perlcraft' => 'monastery.craft',
'perlmeditation' => 'monastery.meditations',
'perlnews' => 'monastery.news',
'perltutorial' => 'monastery.tutorials',
'perlquestion' => 'monastery.seekersofperlwisdom',
'poem' => 'monastery.poetry',
'review' => 'monastery.reviews',
'snippet' => 'monastery.snippets',
'sourcecode' => 'monastery.code',
'tutorial' => 'monastery.tutorials',
'user' => 'monastery.users',
'note' => 'monastery.seekersofperlwisdom',
);
my $parser = new XML::Parser ( Handlers => { Start => \&hdl_start, Cha
+r => \&hdl_char});
my $nntp = new Net::NNTP($server,119,5); # debuglevel 5
#crappy code starts here...
my $id = undef;
my @Nodes = ();
my $author_id = undef;
my %attributes = ();
my %Authors = ();
my %Titles = ();
my $type = undef;
my $xml_stream = get("http://perlmonks.com/index.pl?node=newest+nodes+
+xml+generator");
$parser->parse($xml_stream);
sub hdl_start{
my ($p, $elt, %atts) = @_;
$type = $elt;
if ($elt eq "NODE") { $id = $atts{node_id}; push(@Nodes,\%atts); }
if ($elt eq "AUTHOR") { $author_id = $atts{node_id}; }
}
sub hdl_char {
my ($p, $str) = @_;
$str =~ s/\n//g;
if ($type eq "NODE" && $str) { $Titles{$id} = $str; }
if ($type eq "AUTHOR" && $str) { $Authors{$author_id} = $str; }
}
#open(FILE,">dump.db"); #Dump to disk
#print FILE Dumper(@Nodes);
#close(FILE);
my $i = 0;
foreach(@Nodes) {
if ($i>5) { last; }
$_->{author_user} = $Authors{ $_->{author_user} };
$_->{title} = $Titles{$_->{node_id} };
if (!$nntp->head("<$_->{node_id}\@localhost.localdomain>"))
{ my $page = get("http://www.perlmonks.org/index.pl?node_id=$_->{node_
+id}");
$page =~ s/<(?:[^>'"]*|(['"]).*?\1)*>//gs; #Insert HTML::Parser code
+ here...
my @lines = split(/\n/,$page);
@lines = map { $_."\n" } @lines;
my @header = (
"Newsgroups: $nodetypes{$_->{nodetype}}\n",
"Message-ID: <$_->{node_id}\@localhost.localdomain>\n",
"Subject: $_->{title}\n",
"From: $_->{author_user}\@perlmonks.org\n",
exists $_->{parent_node} ? "References: <$_->{parent_node}\@l
+ocalhost.localdomain>\n" : "",
"\n\n",
@lines);
$nntp->post(\@header);
print $_->{nodetype},"=>",$nodetypes{$_->{nodetype}},"\n";
$i++;
}
}
$nntp->quit();
The replying bit is kind of tricky, also the author s email address isn't valid (unless
vroom starts creating an perlmonks address for all monks).
Update: yes, it's monastery and not monestary... I blame
String::Approx :)
Greetz
Beatnik
... Quidquid perl dictum sit, altum viditur.