#!/usr/bin/perl
#
# script:	freshmeat2mysql
# license:	GPL v2 or later
# description:	add versions of software in freshmeat to the database
# author:	Andrew Avramenko <liks@altlinux.ru> (c) 2007

use DBI;
use LWP::UserAgent;
use XML::Simple;

require "config";

my $upstream_url = "http://freshmeat.net/projects-xml/";

print "fetching versions from freshmeat\n";

my $dbh = DBI->connect($dsn,$dbUser,$dbPass) || die "can't connect to mysql\n";
my $sth = $dbh->prepare("SELECT name FROM srpm WHERE repo='Sisyphus'");
$sth->execute();
#@row = $sth->fetchrow_array();

$sth2 = $dbh->prepare("REPLACE other VALUES(?,?,?)");
while ($project = $sth->fetchrow_array()) {
    print "Project is $project\n";
    my $url = $upstream_url . $project . "/" . $project . "xml";

    my $ua = LWP::UserAgent->new;
    my $req = HTTP::Request->new(GET => "$url");

    my $res = $ua->request($req);

    if ($res->is_success) {
	$content = $res->content;
	if ($content ne "Error: project not found.") {
    	    my $xs1 = XML::Simple->new();
	    my $doc = $xs1->XMLin($content);
	    $sth2->execute($project,$doc->{project}->{latest_release}->{latest_release_version},"freshmeat");
	}
	else {
	    print "project $project doesn't exist in freshmeat repo\n\n";
	}
    }
    else {
	print "Project $project " . $res->status_line . "\n";
    }
}

$sth->finish();
$sth2->finish();
$dbh->disconnect();
