#!/usr/bin/perl
#
#	$Id: maximumedge_update.pl,v 1.1 2003/04/17 21:21:17 kevin Exp $
#
#	Author: Kevin Walsh <kevin@cursor.biz>
#
#	Copyright (c) 2003 Cursor Software Limited.
#	All Rights Reserved.
#
#	Support script for the Plugins::MaximumEdgeNews module.
#
#	Grabs the news ticker data from www.maximumedge.com and saves it
#	to a file.
#
#	This script requires the LWP::UserAgent module to be installed
#	and operational.
#
#	You should run this script periodically, as a cron job, so that
#	the news file is automatically kept up to date.
#
#	Note that this script is not required if you configure your
#	MaximumEdgeNews module to perform "live" lookups.  "Live" lookups are
#	inefficient and slow but users of certain crippled O/Ss have no
#	choice but to run that way.
#
#	----------------------------------------------------------------------
#
#	This program is free software; you can redistribute it and/or modify
#	it under the terms of the GNU General Public License as published by
#	the Free Software Foundation; either version 2 of the License, or
#	(at your option) any later version.
#
#	This program is distributed in the hope that it will be useful,
#	but WITHOUT ANY WARRANTY; without even the implied warranty of
#	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#	GNU General Public License for more details.
#
#	You should have received a copy of the GNU General Public License
#	along with this program; if not, write to the Free Software
#	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
#	02111-1307 USA
#
use File::Spec::Functions qw(catfile);
use LWP::UserAgent;
use strict;

#
#	if you need to use a proxy server then specify it here
#
# my $proxy = 'http://proxy.yourdomain.com:8080/';
#
my $proxy;

#
#	timeout HTTP GET requests after this many seconds
#
my $timeout = 30;

#
#	the remote URI of the base directory used by www.maximumedge.com
#	to store its news file
#
my $remote_uri = 'http://www.maximumedge.com/cgi/news/';

#
#	the $news_location must match the local filename configured
#	into the Plugins::MaximumEdgeNews module.  By default, that
#	module uses the SLIMP3 user's home directory, which is the
#	same directory where the ".slimp3.pref" file can be found
#
my $news_location = catfile('','opt','slimp3','maximumedgenews.txt');

my @news_description = qw(
    top
    entertainment
    business
    sports
    health
);

#
#	end of configurable options
#

my $VERSION = substr(q$Revision: 1.1 $,10);

my $ua = new LWP::UserAgent(
    timeout => $timeout,
    keepalive => 1,
);
$ua->agent("maximumedge_update/$VERSION");
$ua->proxy('http',$proxy) if $proxy;

$remote_uri =~ s|/*$|/|;

my @news = ();
foreach my $name (@news_description) {
    my $uri = "$remote_uri$name.txt";
    my $response = $ua->request(HTTP::Request->new('GET',$uri));
    unless ($response->is_success()) {
        warn "Failed to GET from $uri";
	next;
    }
    push(@news,"$name|$_") for (split('\n',$response->content()));
}

unless (scalar(@news)) {
    print "Couldn't find any news.\n";
    exit 1;
}

open(FILE,">$news_location") or die "Cannot open $news_location for writing: $!";
print FILE join("\n",@news) . "\n";
close FILE;

exit 0;

