Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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.

In reply to Monastery NNTP Bridge by Beatnik

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (3)
As of 2024-04-26 06:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found