#!/usr/bin/perl ########################################################################### ### radio_comment_count.pl: ### Count comments for a posting in a Radio UserLand posting ### ### l.m.orchard http://www.decafbad.com ### This script is public domain material. Share and Enjoy. ########################################################################### use strict; use LWP::Simple; use CGI; my $q = new CGI(); my $log_file = "radio_comment_count_log.txt"; my $user_id = $q->param('u') || 1000; my $post_id = $q->param('p') || 316; my $comment_server = $q->param('server') || "http://radiocomments.userland.com/comments"; my $refresh = $q->param('refresh'); my $style = $q->param('css'); ########################################################################### if (open(FOUT, ">>$log_file")) { my ($sec, $min, $hr, $dd, $mm, $yy, $wd, $yd, $isdst) = localtime(time); $yy += 1900; $mm++; my $date = sprintf('%04d%02d%02dT%02d:%02d:%02d', $yy,$mm,$dd,$hr,$min,$sec); print FOUT "user:$user_id | post:$post_id | server:$comment_server | ". "refresh:$refresh | css:$style | date:$date\n"; close(FOUT); } my $comment_url = "$comment_server?u=$user_id&p=$post_id"; my $comment_content = get($comment_url); my $comment_count = count_comments($comment_content); my $refresh_meta = (defined $refresh) ? qq^^ : ''; my $style_tag = (defined $style) ? qq^^ : ''; my $s = ($comment_count == 1) ? '' : 's'; print $q->header(); print qq^ $refresh_meta $style_tag $comment_count comment$s ^; exit(0); ########################################################################### ### count_comments(): Count comments in a Radio Userland comment page ### This is dirty as hell, just counting the stylesheet attribute ### for comment blocks :) sub count_comments { my ($content) = @_; my @lines = split(/\n/, $comment_content); my $count = 0; foreach my $line (@lines) { if ($line =~ /class="comment"/) { $count++; } if ($line =~ /No comments found/) { return 0; } } return $count; }