 2008/09/06
|
Last update 1999/02/20
TPJ: Issue_04_Usenet
- newsgroups
- retrieve2
- xpat
- retrieve
- filter
- More Samples on Usenet
| Issue_04_Usenet1. newsgroups
|
Download newsgroups
|
#!/usr/local/bin/perl -w
|
|
use Net::NNTP;
|
|
|
|
# most systems provide the name 'news' as an alias for the
|
|
# news server, but if yours does not you will need to change
|
|
# the following line to the name of your server
|
|
|
|
$NNTPhost = 'news';
|
|
|
|
# Create the connection
|
|
|
|
$nntp = Net::NNTP->new($NNTPhost) or
|
|
die "Cannot contact $NNTPhost: $!";
|
|
$groups = $nntp->list() or
|
|
die "Cannot get group list";
|
|
# The 'groups' method returns a reference to a hash.
|
|
# The keys are the group names, the values will be short
|
|
# descriptions of the groups
|
|
print join("\n", keys %$groups),"\n";
|
|
# Always remember to quit the connection
|
|
$nntp->quit;
|
| Issue_04_Usenet2. retrieve2
|
Download retrieve2
|
# Find all articles in 'comp.lang.perl.announce'
|
|
# that were posted in the last 24 hours
|
|
$news = $nntp->newnews( time - 86400,
|
|
'comp.lang.perl.announce') or
|
|
die "Cannot get newnews: $!";
|
|
foreach $msgid (@$news) {
|
|
# Get the text of the article
|
|
$article = $nntp->article($msgid) or
|
|
die "Cannot get '$msgid': $!";
|
|
|
|
# Save the text in a file
|
|
($file = $msgid) =~ s/[\/\$]/_/g;
|
|
open(ARTICLE, ">$file") or
|
|
die "Cannot open $file: $!";
|
|
|
|
print ARTICLE @$article;
|
|
close(ARTICLE);
|
|
}
|
Download xpat
|
($count,$first,$last) =
|
|
$nntp->group('comp.lang.perl.misc');
|
|
|
|
$subj = $nntp->xpat( 'Subject', '*[Cc][Pp][Aa][Nn]*',
|
|
[ $last - 20, $last ] ) or
|
|
die "Cannot get subject lines: $!";
|
|
foreach $msgnum (keys %$subj@$news) {
|
|
# Get the text of the article
|
|
$article = $nntp->article($msgnum) or
|
|
die "Cannot get '$msgnum': $!";
|
|
open(ARTICLE, ">$msgnum") or
|
|
die "Cannot open $file: $!";
|
|
|
|
print ARTICLE @$article;
|
|
close(ARTICLE);
|
|
}
|
| Issue_04_Usenet4. retrieve
|
Download retrieve
|
# Set the current group
|
|
($count,$first,$last,$group) =
|
|
$nntp->group("comp.lang.perl.misc");
|
|
print join("\t", $count,$first, $last, $group),"\n";
|
|
print "-" x 60,"\n";
|
|
|
|
# Get the header of the last article
|
|
$arr = $nntp->head($last);
|
|
print @$arr if $arr;
|
|
|
|
print "-" x 60,"\n";
|
|
|
|
# Now get the previous article
|
|
$nntp->last;
|
|
$arr = $nntp->body;
|
|
print @$arr if $arr;
|
|
print "-" x 60,"\n";
|
|
# And finally the oldest article still avaliable
|
|
$arr = $nntp->article($first);
|
|
print @$arr if $arr;
|
Download filter
|
# Find all articles in 'comp.lang.perl.misc'
|
|
# that were posted in the last hour
|
|
$news = $nntp->newnews( time - 3600,
|
|
'comp.lang.perl.misc') or
|
|
die "Cannot get newnews: $!";
|
|
foreach $msgid (@$news) {
|
|
# Get the subject line from the message
|
|
$subj = $nntp->xhdr( 'Subject', $msgid ) or
|
|
die "Cannot get subject: $!";
|
|
next unless $subj =~ /CPAN/ios;
|
|
# Get the text of the article
|
|
$article = $nntp->article($msgid) or
|
|
die "Cannot get '$msgid': $!";
|
|
# Save the text in a file
|
|
($file = $msgid) =~ s/[<>\/\$]/_/g;
|
|
|
|
open(ARTICLE, ">$file") or
|
|
die "Cannot open $file: $!";
|
|
print ARTICLE @$article;
|
|
close(ARTICLE);
|
|
}
|
| Issue_04_Usenet6. More Samples on Usenet
|

Last update 1999/02/20 
All Rights Reserved - (C) 1997 - 2008 by The Labs.Com |