]> ruin.nu Git - ndwebbie.git/blob - scripts/fetchdumps.pl
LWP::Simple fix for cloudfare
[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 local::lib;
26 use DBI;
27 use DBD::Pg qw(:pg_types);
28
29 use LWP::Simple qw/head get $ua/;
30 $ua->agent("Stupid user agent check is stupid");
31
32 use FindBin;
33 use lib "$FindBin::Bin/../lib";
34
35 use ND::DB;
36
37 our $dbh = ND::DB::DB();
38
39 $dbh->do("SET CLIENT_ENCODING TO 'LATIN1';");
40
41 my $insert = $dbh->prepare("INSERT INTO dumps(tick,type,modified,dump) VALUES(?,?,?,?)");
42 my $select = $dbh->prepare("SELECT 1 FROM dumps WHERE type = ? AND modified = ?");
43 my $updated = 0;
44 for my $type ("planet","alliance","galaxy"){
45         my @head = head("http://game.planetarion.com/botfiles/${type}_listing.txt");
46         $select->execute($type,$head[2]);
47         unless ($select->fetchrow){
48                 my $file = get("http://game.planetarion.com/botfiles/${type}_listing.txt");
49                 if (defined $file && $file =~ /Tick: (\d+)/){
50                         $updated = $1;
51                         $insert->execute($1,$type,$head[2],$file);
52                 }
53         }
54         $select->finish;
55 }
56
57 if ($updated){
58         `perl $FindBin::Bin/parsealliances.pl $updated`;
59         `perl $FindBin::Bin/parseplanets.pl $updated`;
60         `perl $FindBin::Bin/parsegalaxies.pl $updated`;
61         `perl $FindBin::Bin/ndrank.pl`;
62         $dbh->do(q{UPDATE misc SET value = ? WHERE id = 'TICK'}, undef, $updated);
63         system 'killall','-USR1', 'ndbot.pl';
64         local $dbh->{Warn} = 0;
65 }
66
67
68 $dbh->disconnect;