#!/usr/bin/perl -w
use strict;
use XML::RSS::Parser;
use DBI;
use LWP::Simple qw(get);

my $database;



while (1){


execute();

print "Sleeping\n";
sleep(60);
}


sub execute{

restore_database();

my $servicequery = $database->prepare("SELECT name, address FROM `newsservices` WHERE `lastfetch` < SUBTIME(NOW(),`refresh`)");
$servicequery->execute();
while (my @row = $servicequery->fetchrow_array ) {
    my ($servicename, $url)  = @row;
    print $servicename.":". $url."\n";
    my $p = XML::RSS::Parser->new;
    my $feed = $p->parse_uri($url);
    if (!$feed) {next;}
    foreach my $i ( $feed->query('//item')) {
        my $title="";
        my $link="";
        my $description="";
        my $node = $i->query('title');
        if ($node) {$title = $node->text_content;}
        $node = $i->query('link');
        if ($node) {$link = $node->text_content;}
        $node = $i->query('description');
        if ($node) {$description = $node->text_content;}
        my $query = $database->prepare("SELECT COUNT(*) FROM `newsseen` WHERE `address` = ?");
        $query->execute($link);
        if (!$query->fetchrow_array){
            print "new:".$servicename.":".$title."\n";
            $query = $database->prepare("INSERT INTO `newsseen` ( `newsservice` , `address` ) VALUES (?,?)");
            $query->execute($servicename, $link);
            $query = $database->prepare("INSERT INTO `newsnew` ( `newsservice` , `address` , `title` , `description` ) VALUES (?,?,?,?)");
            $query->execute($servicename, $link, $title, $description);
            }
        }
    my $query = $database->prepare("UPDATE `newsservices` SET `lastfetch` = NOW( ) WHERE  `name` = ?");
    $query->execute($servicename);
    
    }

}





sub restore_database{

if (!$database or $database->ping){
    $database = DBI->connect("dbi:mysql:database=imbbs;host=localhost;port=3306", "root", "")
                or die "Couldn't connect to database: " . DBI->errstr;
    }


}
