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

RFC fetcher

by yakko (Friar)
on Mar 05, 2001 at 06:11 UTC ( [id://62165]=CUFP: print w/replies, xml ) Need Help??

I found myself needing various RFCs at hand, so I ended up scratching this itch by coding up a few lines of perl to get the job done. You can pipe the output thru less or your favourite pager.

usage: rfc <rfc#>

(Yes, I'm too lazy to view them in a browser window which is covered by terminals. :o)

Update: neophyte had a couple of good ideas; I've implemented one of them here.
Update2: (20010517) Implemented merlyn's suggestion of using mirror(), changed the server to one which makes that work.

#!/usr/bin/perl -w # example usage: rfc 1459 (gets the IRC RFC) use strict; $|++; use LWP::Simple; use HTTP::Status; die("usage: rfc <rfc#>\n") unless defined($ARGV[0]); my $rfcnum=$ARGV[0]; my $base="ftp://ftp.rfc-editor.org/in-notes/rfc"; my $rfcurl=$base.$rfcnum.".txt"; my $rfcdir="$ENV{HOME}/rfcs"; my $rfcfile="${rfcdir}/rfc${rfcnum}.txt"; my $rfc; my $rc=mirror($rfcurl,$rfcfile); # any 4xx or 5xx error probably means we got no RFC if($rc < 400) { open(RFC,"<$rfcfile") or die("open($rfcfile): $!\n"); local $/ = undef; $rfc=<RFC>; close(RFC); print "$rfc"; } else { print "Failed to fetch ${rfcurl}: $rc ", status_message($rc), "\n" +; } __END__
(View source for the old version)

Replies are listed 'Best First'.
Re: RFC fetcher
by larsen (Parson) on Mar 05, 2001 at 20:20 UTC
    mmm... why print "$rfc" and not print $rfc?
      It's simply a habit that I can now slowly break as I think the perl way.

      --
      Me spell chucker work grate. Need grandma chicken.

Re: RFC fetcher
by neophyte (Curate) on Apr 04, 2001 at 13:38 UTC
    I was looking for some rfc today so I thought I might use your code.
    Of course I modified it, because I want to keep the rfc's I get. So I added a check if that rfc exists in my rfc-directory, if not it gets the rfc and writes it to a file. Works nicely.
    I would like to prepend a search tool for the index, so you can search for a rfc by topic. Perhaps I'll add that when I have more time.

    neophyte Niederrhein.pm

      Great ideas! Here's my implementation of the first idea:
      #!/usr/bin/perl -w # example usage: rfc 1459 (gets the IRC RFC) use strict; use LWP::Simple qw(get); die("usage: rfc <rfc#>\n") unless defined($ARGV[0]); my $rfcnum=$ARGV[0]; my $base="http://www.rfc.net/rfc"; my $rfcdir="$ENV{HOME}/rfcs"; my $rfcfile="${rfcdir}/rfc${rfcnum}.txt"; my $rfc; if(-e $rfcfile) { open(RFC,"<$rfcfile") or die("open($rfcfile): $!\n"); local $/ = undef; $rfc=<RFC>; close(RFC); } else { $rfc=get($base.$rfcnum.".txt"); open(RFC,">$rfcfile") or die("open($rfcfile): $!\n"); print RFC "$rfc"; close(RFC); } print "$rfc"; __END__
      (Update: Just when I thought I had it nailed, better ideas crop up. Now reading docs to retool to merlyn's idea below. (tho rfc.net doesn't appear to return the proper headers that support mirror()))

      --
      Me spell chucker work grate. Need grandma chicken

        I'd use LWP::Simple's mirror($url, $localfile) in place of the if/else test. That way, if they update it, you'll get the latest version, and if it hasn't changed, it's still minimal traffic. It also automatically avoids creating the local file if the remote fetch failed, so you won't end up with an empty RFC file if something's broken.

        -- Randal L. Schwartz, Perl hacker

Re: RFC fetcher
by bladx (Chaplain) on Mar 05, 2001 at 07:34 UTC
    Hey there yakko! Great job on your little code snippet for retrieving RFC's!! It is suberb in it's compact style and so on. I came accross it and studied it briefly to see what it really did ( ^_^ since i'm still really new to Perl,) and I could actually understand what this code did for once (compared to some other pieces of code I end up not being able to comprehend :)) so keep up the awesome work, yakko! That's all from me for now I guess!

    bladx ~ ¡muchas veces tengo preguntas!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: CUFP [id://62165]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (2)
As of 2024-04-19 22:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found