Article View: news.software.nntp
Article #3570Re: perl script < active newsgroups list > hierarchical html links
From: Tom Christiansen
Date: Fri, 21 Apr 1995 00:00
Date: Fri, 21 Apr 1995 00:00
374 lines
11067 bytes
11067 bytes
In comp.lang.perl, david@services.more.net writes:
:I am wondering if some genius out there on the net has written a perl script
:which reads in the list of active newsgroups (/usr/local/news/active or
:/usr/local/news/newsgroups) and outputs a series of html documents that
:lay out USENET in a hierarchical fashion. I hope I have stated this clearly.
Genius and insanity are darned close sometimes. I'll let you run this
and decide for yourself. :-)
--tom
#!/usr/local/bin/perl5
# install in cgi-bin/picknewsgroups
# tchrist@mox.perl.com
require 5.0;
##############################################
# BEGIN CONFIG SECTION
##############################################
$EGREP = 'egrep'; # prefer to use gnu egrep for speed
$SORT = 'sort -t. -u +0 -1 '; # maybe +1 -2 if you live in a sad world
# run from cron now and then : nntplist newsgroups > $NGFILE
$NGFILE = '/usr/local/lib/news/newsgroups';
# I'd rather you chose yourself. :-(
$HOST = 'mox.perl.com';
# $HOST = 'localhost';
# ($HOST) = gethostbyname(chop($host=`hostname`), $host);
# chop($HOST=`hostname`);
##############################################
# END CONFIG SECTION
##############################################
get_request();
%Groups = ();
die "No $NGFILE: $!" unless -f $NGFILE && -r _;
if (!($Target = $rqpairs{'newsgroup'})) {
($Target) = each %rqpairs; # top level
}
die "Cannot fork: $!" unless defined ($pid = open(INPUT, "-|"));
unless ($pid) {
if ($Target) {
exec $EGREP, "^\Q$Target", $NGFILE;
} else {
exec "$SORT $NGFILE 2>/dev/null";
}
die "exec failed: $!";
}
# Here's the kinda data structure we're going to be building ...
#%Groups = (
# alt => {
# DESC => "useless drivel",
# NEXT => {
# activism => {
# DESC => "Activities for activists",
# NEXT => {
# d => {
# DESC => "A place to discuss issues in alt.activism",
# },
# "death-penalty" => {
# DESC => "For people opposed to capital punishment",
# },
# },
# },
# adoption => {
# DESC => "For those involved with or contemplating adoption",
# }
# },
# },
# comp => {
# DESC => "computer stuff",
# NEXT => {
# lang => {
# DESC => "programming languages",
# NEXT => {
# C => "it's better than a boot to the head",
# perl => "welcome to Larry's World",
# python => "here be snakes, but not poisonous ones",
# tcl => "but for tk, none would bother",
# scheme => "job security tips for ivory towers",
# }
# }
# }
# }
#);
while (<INPUT>) {
chomp;
# now for the darned tabs in the newsgroups file
1 while s/\t+/' ' x (length($&) * 8 - length($`) % 8)/e;
($ng, $desc) = unpack("A24 A*", $_);
if (length($ng) == 24) {
$desc =~ s/^(\S+)\s*//;
$ng .= $1;
}
@parts = split /\./, $ng;
$last = pop @parts;
$gp = \%Groups;
for $hier (@parts) {
if (!ref $gp->{$hier}{NEXT}) {
$gp->{$hier}{NEXT} = {};
}
$gp = $gp->{$hier}{NEXT};
}
$gp->{$last}{DESC} = $desc;
}
close(INPUT) || warn "Trouble processing input";
#hierprint(\%Groups);
$gp = \%Groups;
# this copies the DESC down a level into a blank key for groups
# like comp.lang.c that have their own raison d'etre
# apart from holding comp.lang.c.moderated, etc.
if ($Target) {
$what = "Newsgroups under $Target";
for $hier (split(/\./, $Target)) {
if ( $gp->{$hier}{DESC} ) {
$gp->{$hier}{NEXT}{""}{DESC} = $gp->{$hier}{DESC};
}
$gp = $gp->{$hier}{NEXT};
}
raise($gp);
} else {
$what = "Top Level News Hierarchy";
}
html_header($what);
print "<H2>$what</H2>\n";
print "<HR>\n";
if (ref $gp) {
$Target =~ s/\.$//;
topprint($gp, $Target);
} else {
print "Couldn't find anything interesting to print.\n"
}
print "<HR>\n";
print "</Body>\n";
html_trailer();
exit;
######################################
sub topprint {
my ($gp, $hier) = @_;
if (!$Target) {
print <<xxxYYYxxx;
<FORM METHOD="POST" ACTION="http://$HOST/cgi-bin/picknewsgroups">
xxxYYYxxx
}
for $name (sort keys %$gp) {
$desc = $gp->{$name}{DESC};
$kids = $gp->{$name}{NEXT};
if ($desc eq '?') { $desc = ""; }
($fullname = $hier . ($hier && '.' ) . $name) =~ s/\.$//;
# the pushed down DESCS are null ^^^^^^^^
if (!$Target) {
print <<xxxYYYxxx;
<INPUT TYPE="submit" VALUE="$name" NAME="$name">
xxxYYYxxx
next;
}
if ($kids) {
print <<xxxYYYxxx;
<FORM METHOD="POST" ACTION="http://$HOST/cgi-bin/picknewsgroups">
<DL>
<INPUT TYPE="hidden" NAME=newsgroup VALUE="$fullname">
<DT>
<INPUT TYPE="submit" VALUE="$fullname">
<DD> $desc
</DL>
</FORM>
xxxYYYxxx
next;
}
print <<xxxYYYxxx if $Target; # skip control and junk
<DL>
<DT>
<A HREF="news:$fullname">$fullname</a>
<DD> $desc
</DL>
xxxYYYxxx
if (!$Target) { print "</form>\n" }
}
}
# this isn't used, but if you read in the whole tree,
# this will dump it all
sub hierprint {
my ($gp,$hier) = @_;
for $name (sort keys %$gp) {
$desc = $gp->{$name}{DESC};
$kids = $gp->{$name}{NEXT};
$fullname = $hier . ($hier && '.' ) . $name;
if ($kids) {
$width = 2 * $indent - 40;
iprint (sprintf("%${width}s %s\n", $fullname, $desc && "<<<$desc>>>"));
indent();
hierprint($kids, "$fullname");
undent();
} else {
$width = 2 * $indent - 40;
iprint (sprintf("%${width}s %s\n", $name, $desc));
}
}
}
sub indent { $indent++ }
sub undent { --$indent }
sub iprint {
print " " x $indent;
print @_;
}
# promote alt.barney.die.die.die all the way up
# (actually, there's a bug and i only get the first couple levels)
sub raise {
my $gp = shift;
for $name (keys %$gp) {
if ($gp->{$name}{NEXT}) {
$children = keys %{$gp->{$name}{NEXT}};
if ($children == 1 && !$gp->{$name}{DESC}) {
($n, $p) = each %{$gp->{$name}{NEXT}};
delete $gp->{$name};
$name = "$name.$n";
%{$gp->{$name}} = %$p;
} else {
raise($gp->{$name}{NEXT});
}
}
}
}
#####################################################################
# The CGI_HANDLERS deal with basic CGI POST or GET method request
# elements such as those delivered by an HTTPD form, i.e. a url
# encoded line of "=" separated key=value pairs separated by &'s
# Routines:
# get_request: reads the request and returns both the raw and
# processed version.
# url_decode: URL decodes a string or array of strings
# html_header: Transmits a HTML header back to the caller
# html_trailer: Transmits a HTML trailer back to the caller
# Author:
# James Tappin: sjt@xun8.sr.bham.ac.uk
# School of Physics & Space Research University of Birmingham
# Feb 1993.
# Copyright & Disclaimer.
# This set of routines may be freely distributed, modified and
# used, provided this copyright & disclaimer remains intact.
# This package is used at your own risk, if it does what you
# want, good; if it doesn't, modify it or use something else--but
# don't blame me. Support level = negligable (i.e. mail bugs but
# not requests for extensions)
# Usage:
# needs a 'require "cgi_handlers.pl";' line in the main script
#
# &get_request; will get the request and decode it into an
# indexed array %rqpairs, the raw request is in
# $request
#
# ... = &url_decode(LIST); will return a URL decoded version of
# the contents of LIST
#
# &html_header(TITLE); will write to standard output an HTML
# header (including the content-type
# field) giving the document the title
# specified by TITLE.
#
# &html_trailer; Writes a trailer to the html document
# with the name of the script generating
# it and the date (in UT).
sub get_request {
# Subroutine get_request reads the POST or GET form request from STDIN
# into the variable $request, and then splits it into its
# name=value pairs in the associative array %rqpairs.
# The number of bytes is given in the environment variable
# CONTENT_LENGTH which is automatically set by the request generator.
# Encoded HEX values and spaces are decoded in the values at this
# stage.
# $request will contain the RAW request. N.B. spaces and other
# special characters are not handler in the name field.
if ($ENV{'REQUEST_METHOD'} eq "POST") {
read(STDIN, $request, $ENV{'CONTENT_LENGTH'});
} elsif ($ENV{'REQUEST_METHOD'} eq "GET" ) {
$request = $ENV{'QUERY_STRING'};
}
%rqpairs = &url_decode(split(/[&=]/, $request));
}
sub url_decode {
# Decode a URL encoded string or array of strings
# + -> space
# %xx -> character xx
foreach (@_) {
tr/+/ /;
s/%(..)/pack("c",hex($1))/ge;
}
@_;
}
sub html_header {
# Subroutine html_header sends to Standard Output the necessary
# material to form an HHTML header for the document to be
# returned, the single argument is the TITLE field.
local($title) = @_;
print "Content-type: text/html\n\n";
print "<html><head>\n";
print "<title>$title</title>\n";
print "</head>\n<body>\n";
}
sub html_trailer {
# subroutine html_trailer sends the trailing material to the HTML
# on STDOUT.
local($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst)
= gmtime;
local($mname) = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",
"Aug", "Sep", "Oct", "Nov", "Dec")[$mon];
local($dname) = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri",
"Sat")[$wday];
print "<p>\nGenerated by: <var>$0</var><br>\n";
printf "Date: %02d:%02d:%02d GMT on $dname $mday $mname $year.<p>\n",
$hour, $min, $sec;
print "</body></html>\n";
}
1;
Technical Details
Message-ID:
<3n7mq5$hfo@csnews.cs.colorado.edu>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!i2pn2.pugleaf.net!archive.newsdeef.eu!mbox2nntp-comp.infosystems.www.misc.mbox.zip!not-for-mailReferences:
<3n1ec6$fuv@news.missouri.edu>Show Full Headers
Complete Article Headers
Message-ID: <3n7mq5$hfo@csnews.cs.colorado.edu> Subject: Re: perl script < active newsgroups list > hierarchical html links Date: Fri, 21 Apr 1995 00:00:00 +0000 From: Tom Christiansen <tchrist@mox.perl.com> References: <3n1ec6$fuv@news.missouri.edu> Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!i2pn2.pugleaf.net!archive.newsdeef.eu!mbox2nntp-comp.infosystems.www.misc.mbox.zip!not-for-mail X-Deja-AN: 101352309 Distribution: inet Organization: Perl Consulting and Training Reply-to: tchrist@mox.perl.com (Tom Christiansen) Newsgroups: news.software.nntp,comp.infosystems.www.misc,comp.lang.perl Originator: tchrist@mox