Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Code for Perlmonks XML to RSS

by xdg (Monsignor)
on Mar 30, 2004 at 02:02 UTC ( #340820=monkdiscuss: print w/replies, xml ) Need Help??

As an XML/RSS exercise, I wrote up some code that converts the latest questions from the perlmonks Newest Nodes XML Feed to RSS. It's quick and dirty, but works and a script using it is now in my crontab (hourly). (It parses the authors, too, but I haven't done anything with that yet.) I thought other users (or the gods) might be interested in seeing it. People should be able to easily modify this example to pick out other parts of the newest nodes feed if they desire.

use warnings; use strict; use LWP::UserAgent; use XML::Simple; use XML::RSS; # pass in the URL for the newest nodes page, i.e. # sub process_xml { my $url = shift; my %authors; my @questions; my $browser = LWP::UserAgent->new(); $browser->timeout(30); my $response = $browser->get($url); die unless $response->is_success; my $tree = XMLin( $response->content ); foreach my $node ( @{ $tree->{AUTHOR} } ) { $authors{ $node->{'node_id'} } = $node->{'content'}; } foreach my $node ( @{ $tree->{NODE} } ) { my $item = { author => $authors{ $node->{'author_user'} }, node_id => $node->{'node_id'}, subject => $node->{'content'} }; push @questions, $item if $node->{'nodetype'} eq 'perlquestion +'; } my $rss = new XML::RSS (version => '1.0'); $rss->channel( title => "perlmonks newest questions", link => "", description => "perlmonks newest questions", ); for my $item ( @questions ) { $rss->add_item( title => $item->{subject}, link => "" . +$item->{node_id} ); } print $rss->as_string(); }


Code posted by xdg on PerlMonks is public domain. It has no warranties, express or implied. Posted code may not have been tested. Use at your own risk.

DG: Updated to add strict and warnings to set a good example for new monks.

Replies are listed 'Best First'.
Re: Code for Perlmonks XML to RSS
by Vautrin (Hermit) on Mar 30, 2004 at 17:09 UTC

    I was just playing around with your++ script. It's very cool, but I have the following comments / suggestions:

    1. The line:

      die unless $response->is_success;

      Really should be:

      die ("Could not fetch the web page because: " . $response->status_line) unless $response->is_success; die ("Content type not text/xml. It was" . $response->content_type) unless ($response->content_type eq 'text/xml');

      Otherwise you aren't notified why a get() fails, and it allows the user to try parsing a non XML web page -- which leads to some humorous results.

    2. Why is it even necessary to pass in the URL for the newest nodes page? Your script doesn't look like it will work on any page besides the newest nodes page, so doing a shift (@_) for the URL seems useless. Why not just set my $url = ""; and be done with it?
    3. Excellent work. I thoroughly enjoyed it. Also, not to nitpick, but why don't you put

      use strict; use warnings;

      at the top of your script? It runs with no problems under them, and it's good practice (plus I think it's a good idea to help any new monks seeing it to get in the habit of using them).

    Good job again!

    Want to support the EFF and FSF by buying cool stuff? Click here.

      Thanks for the comments. As I said, it was quick and dirty and your suggestions are worthwhile additions. For the record, this was pasted out of a larger script which did have strict and warnings on and was originally set for passing in URL's from the command line, which is why that appears that way. I probably should have cleaned it up further. Certainly, adding strict and warnings even in my example to encourage good practice among others is a great suggestion.


      Code posted by xdg on PerlMonks is public domain. It has no warranties, express or implied. Posted code may not have been tested. Use at your own risk.

Re: Code for Perlmonks XML to RSS
by exussum0 (Vicar) on Mar 30, 2004 at 16:50 UTC
    And if you have XSLT, it can be done that way too. But as allways, TIMTOWTDI.

    -- "So far my experience has been that most people who go for certification have broad but not deep knowledge in the field and the flavor of the knowledge is academic. But every once in a while one finds a gem of a person who learns all the time and uses certification to prove it." -- on Orkut
problem spawning mconnect
by timmyb85 (Novice) on Mar 30, 2004 at 23:03 UTC
    What is wrong with this? It isn't working, it gives me an error:
    open (MPRINT, "|$mconn $ipconnect") || die "Can't open $mconn!\n"; .... .... close (MPRINT);
    • $mconn is the program "mconnect"
    • $ipconnect is an IP address & port # (ie. 123.456.78.90 -p 1234)
    Any ideas?

    Edited by theorbtwo: moved from Code for Perlmonks XML to RSS to Seekers of Perl Wisdom.

      What error do you get? Try including the system error message like this: open (MPRINT, "|$mconn $ipconnect") || die "Can't open pipe to '$mconn': $!";

      -- Hofmator

        What is the difference between:

        open (MPRINT, "$mconn|") or die $!;


        open (MPRINT, "|$mconn") or die $!;

        ...$mconn is some unix program with parameters The first line works, the second one doesn't. I don't know what the difference is. I went to a web page, and they said it was the same thing, but if one works and the other doesn' seems like they aren't the same thing. Does anybody know the difference? Thanks timmyb85
        I found the error message, it is:

        connecting to host 123.456.78.90 (123.456.78.90), port 1234 connection open

        Any ideas about how to fix this error?

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: monkdiscuss [id://340820]
Approved by VSarkiss
Front-paged by matija
[Eily]: since the value of a node is also the sum from the two nodes above, you just have to demonstrate that the equality is true at the top of the triangle
[Discipulus]: yes Eily, thanks oiskuu but i dont get it.. ;=( maybe I'll ask a SOPW
[LanX]: all combinations with same amount of left and right?
[Eily]: if you want to store in a structure with the coordinates as key, arrays might do, since the keys are going to be 0..n
[LanX]: (Pascale path)
[Eily]: paths like that
[Discipulus]: yes Eily++ (very keen) I want to integrate my project with a 17th experiments. I want to colorize in sequence all paths
[oiskuu]: Yeah, modifry the recursive func combinations() to return not the number, but the paths themselves.
[Eily]: Discipulus I'd do that by starting from the bottom node I think. That way it can inherit the paths from the two nodes above (and so on, recursively)
[LanX]: oh I meant fixed amount

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (10)
As of 2018-03-19 11:18 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (239 votes). Check out past polls.