]> ruin.nu Git - ndwebbie.git/blob - scripts/fetchdumps.pl
Adding the perl and sql scripts
[ndwebbie.git] / scripts / fetchdumps.pl
1 #!/usr/bin/perl
2 q{
3 /***************************************************************************
4  *   Copyright (C) 2006 by Michael Andreen <harvATruinDOTnu>               *
5  *                                                                         *
6  *   This program is free software; you can redistribute it and/or modify  *
7  *   it under the terms of the GNU General Public License as published by  *
8  *   the Free Software Foundation; either version 2 of the License, or     *
9  *   (at your option) any later version.                                   *
10  *                                                                         *
11  *   This program is distributed in the hope that it will be useful,       *
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
14  *   GNU General Public License for more details.                          *
15  *                                                                         *
16  *   You should have received a copy of the GNU General Public License     *
17  *   along with this program; if not, write to the                         *
18  *   Free Software Foundation, Inc.,                                       *
19  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
20  ***************************************************************************/
21 };
22
23 use strict;
24 use warnings;
25 use DBI;
26 use DBD::Pg qw(:pg_types);
27
28 use LWP::Simple;
29
30 $0 =~ /(.*\/)[^\/]/;
31 my $dir = $1;
32 our $dbh;
33 for my $file ("/home/whale/db.pl")
34 {
35         unless (my $return = do $file){
36                 warn "couldn't parse $file: $@" if $@;
37                 warn "couldn't do $file: $!"    unless defined $return;
38                 warn "couldn't run $file"       unless $return;
39         }
40 }
41 $dbh->do("SET CLIENT_ENCODING TO 'LATIN1';");
42
43 my $insert = $dbh->prepare("INSERT INTO dumps(tick,type,modified,dump) VALUES(?,?,?,?)");
44 my $select = $dbh->prepare("SELECT 1 FROM dumps WHERE type = ? AND modified = ?");
45 my $updated = 0;
46 for my $type ("planet","alliance","galaxy"){
47         my @head = head("http://game.planetarion.com/botfiles/${type}_listing.txt");
48         $select->execute($type,$head[2]);
49         unless ($select->fetchrow){
50                 my $file = get("http://game.planetarion.com/botfiles/${type}_listing.txt");
51                 if ($file =~ /Tick: (\d+)/){
52                         $updated = $1;
53                         $insert->execute($1,$type,$head[2],$file);
54                 }
55         }
56         $select->finish;
57 }
58
59 if ($updated){
60         `${dir}parsedumps.pl $updated`;
61         `${dir}ndrank.pl`;
62         $dbh->do("UPDATE misc SET value = ? WHERE id = 'TICK'", undef, $updated);
63 }
64
65
66 $dbh->disconnect;