|
| 1 | +#!/usr/bin/perl -w |
| 2 | + |
| 3 | +# Blagg: the Blosxom RSS aggregator |
| 4 | +# Author: Rael Dornfest <[email protected]> |
| 5 | +# Contributors: Benjamin Trott <[email protected]> |
| 6 | +# Version: 0+4i |
| 7 | +# Home/Docs/Licensing: http://www.oreillynet.com/~rael/lang/perl/blagg/ |
| 8 | + |
| 9 | +# --- Configurable variables ----- |
| 10 | + |
| 11 | +# Where are my blog entries kept? |
| 12 | +my $datadir = "/Library/WebServer/Documents/blosxom"; |
| 13 | + |
| 14 | +# What external program should I use to grab RSS feeds from remote URLs? |
| 15 | +# Mac OS X default is 'curl'; *nix should like 'lynx -source' or |
| 16 | +# 'wget --quiet -O -' |
| 17 | +my $get_prog = 'curl'; |
| 18 | + |
| 19 | +# -------------------------------- |
| 20 | + |
| 21 | +use strict; |
| 22 | +use FileHandle; |
| 23 | +use CGI qw/:standard :netscape/; |
| 24 | + |
| 25 | +die qq{usage: blagg [-mode=(automatic|interactive) [-blog={blogname}]\n} |
| 26 | + unless param('-mode') && param('-mode') =~ /^(automatic|interactive)$/ && (!param('-blog') || param('-blog') =~ /^([a-zA-Z]\w*)$/); |
| 27 | + |
| 28 | +param('-blog') and $datadir .= '/'.param('-blog'); |
| 29 | + |
| 30 | +# Import and initialize plugin, if specified. |
| 31 | +if ( param('-plugin') ) { |
| 32 | + require('./plugins/' . param('-plugin') . '.pl'); |
| 33 | + blaggplug::init(); |
| 34 | +} |
| 35 | + |
| 36 | +my $fh = new FileHandle; |
| 37 | + |
| 38 | +# Does this blog specify any RSS feeds to watch? |
| 39 | +$fh->open("< $datadir/rss.dat") or exit; |
| 40 | + |
| 41 | +# Loop through the feeds in the list and aggregate |
| 42 | +foreach ( <$fh> ) { |
| 43 | + my($f_nick,$f_url,$f_mode) = split; |
| 44 | + next unless $f_nick =~ /^\w+$/ && $f_url =~ m#^\w+://# && $f_mode =~ /^(interactive|automatic)$/ && $f_mode eq param('-mode'); |
| 45 | + |
| 46 | + $fh->open("$get_prog '$f_url' |") || next; |
| 47 | + print "\n_____${f_url}_______________\n"; |
| 48 | + my $rss = join '', <$fh>; |
| 49 | + $fh->close; |
| 50 | + |
| 51 | + # Feed's title and link |
| 52 | + my($f_title, $f_link) = ($rss =~ m#<title>(.*?)</title>.*?<link>(.*?)</link>#ms); |
| 53 | + |
| 54 | + # RSS items' title, link, and description |
| 55 | + while ( $rss =~ m{<item(?!s).*?>.*?(?:<title>(.*?)</title>.*?)?(?:<link>(.*?)</link>.*?)?(?:<description>(.*?)</description>.*?)?</item>}mgis ) { |
| 56 | + my($i_title, $i_link, $i_desc, $i_fn) = ($1||'', $2||'', $3||'', undef); |
| 57 | + |
| 58 | + # Unescape & < > to produce useful HTML |
| 59 | + my %unescape = ('<'=>'<', '>'=>'>', '&'=>'&', '"'=>'"'); |
| 60 | + my $unescape_re = join '|' => keys %unescape; |
| 61 | + $i_title && $i_title =~ s/($unescape_re)/$unescape{$1}/g; |
| 62 | + $i_desc && $i_desc =~ s/($unescape_re)/$unescape{$1}/g; |
| 63 | + |
| 64 | + # If no title, use the first 50 non-markup characters of the description |
| 65 | + unless ($i_title) { |
| 66 | + $i_title = $i_desc; |
| 67 | + $i_title =~ s/<.*?>//msg; |
| 68 | + $i_title = substr($i_title, 0, 50); |
| 69 | + } |
| 70 | + next unless $i_title; |
| 71 | + |
| 72 | + # Determine filename |
| 73 | + ($i_fn = $i_title) =~ s/\W/_/g; |
| 74 | + $i_fn = "$datadir/$f_nick." . substr($i_fn, 0, 15) . '...' . substr($i_fn, -5) . ".txt"; |
| 75 | + |
| 76 | + # Skip already-aggregated items (aka filename already exists) |
| 77 | + next if -e $i_fn; |
| 78 | + |
| 79 | + my $item = "$i_title\n$i_desc<br />\n(" . a({-href=>$i_link},'link') . ") [" . a({-href=>$f_link}, $f_title) . "]\n"; |
| 80 | + my $yn = ''; |
| 81 | + if (param('-mode') eq 'automatic') { |
| 82 | + $yn = 'y'; |
| 83 | + } else { |
| 84 | + print qq{\n"$i_title"\n[$i_link]\n$i_desc\n\nDo you want to blog this item? (y|n|q)?}; |
| 85 | + while (<STDIN>) { |
| 86 | + /^([ynq])$/ and $yn = $1, last; |
| 87 | + }; |
| 88 | + } |
| 89 | + $yn eq 'q' && exit; |
| 90 | + # Save entry to file (and via plug-in if specified) |
| 91 | + $yn eq 'y' && |
| 92 | + $fh->open("> $i_fn") && print($fh $item) && $fh->close() && |
| 93 | + param('-plugin') && |
| 94 | + blaggplug::post($i_title, $i_link, $i_desc, $f_title, $f_link); |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +# Clean up plugin, if specified. |
| 99 | +param('-plugin') and blaggplug::destroy(); |
0 commit comments