#!/usr/bin/perl -w use strict; $|=1; my ($opt_d, $opt_u, $opt_p, ); my ($opt_c, $opt_U,); use Getopt::Long qw(:config no_ignore_case bundling); GetOptions( 'd=s' => \$opt_d, # db name 'u=s' => \$opt_u, # db user 'p=s' => \$opt_p, # db pass 'c' => \$opt_c, # do char-to-account scan 'U=s' => \$opt_U, # apiuser restriction ); $opt_d ||= 'eve'; $opt_u ||= 'eve'; my $dbh; use DBI; $dbh = DBI->connect("DBI:mysql:database=$opt_d", $opt_u, $opt_p); die "cant connect to db: ".DBI->errstr unless $dbh; my $restrictions = 'a.active = 1'; if (defined $opt_U) { if ($opt_U =~ /^(\d+)$/) { if (length($opt_U) > 4) { $restrictions = "a.apiUser = $1"; } else { $restrictions = "a.id = $1"; } } else { $restrictions = "a.name like '$opt_U' "; } } die "no restrictions" unless $restrictions; my $sth = $dbh->prepare(qq{ select a.userID, a.apiKey, a.name from `apiuser` a where $restrictions }) or die $dbh->errstr; $sth->execute() or die $sth->errstr; my $ac = $sth->fetchall_hashref('userID'); $sth->finish; for (keys %$ac) { my $a = $ac->{$_}; eval { &do_apiuser($a); }; if ($@) { chomp $@; printf "BOOM: do_apiuser => %s\n", $@; } #last; } exit 0; sub do_apiuser($) { my $acc = shift; printf "APIUSER: %i - %s\n", $acc->{userID}, $acc->{name}; require EVE::API; my $api = new EVE::API( %$acc ) or die "can't get API object\n"; my $res = $api->account->Characters->load; for my $char (@{$res->characters}) { eval { &do_char($api, $char); }; if ($@) { chomp $@; printf "BOOM: do_char => %s\n", $@; } #last; } } my $sth_updatechar; sub do_char($$) { my ($api, $c,) = @_; printf "CHARACTER: %i - %s in %i - %s\n", $c->characterID , $c->name, $c->corporationID, $c->corporationName; # 'characterID' => 706459225 # 'corporationID' => 1152348435 # 'corporationName' => 'Sixth Congregational Church of Rodney' # 'name' => 'Dandridge Sukarno' &update_name($c->characterID, $c->name); &update_name($c->corporationID, $c->corporationName); my $sth = $sth_updatechar ||= $dbh->prepare(qq{ insert into `character` set characterID = ?, name = ?, corporationID = ?, first = UTC_TIMESTAMP(), last = UTC_TIMESTAMP() on duplicate key update corporationID = ?, last = UTC_TIMESTAMP() }) or die $dbh->errstr; $sth->execute($c->characterID, $c->name, $c->corporationID, $c->corporationID) or die $sth->errstr; $sth->finish(); my $res = $api->char->AccountBalance( characterID => $c->characterID ); for ( @{ $res->accounts } ) { eval { &do_account($api, $c, "char", $_); }; if ($@) { chomp $@; printf "BOOM: do_account char => %s\n", $@; } } #return; # if ($c->corporationID > 1000000000) { # is-player-corp guessing $res = eval { $api->corp->AccountBalance( characterID => $c->characterID ); }; if ((!$res && $@) || $res->isError){ chomp $@; if (($res && $res->errorCode == 206) || ($@ eq 'Error (206): Character must have Accountant or Junior Accountant roles.')) { return; } else { die $@; } } for ( @{ $res->accounts } ) { eval { &do_account($api, $c, "corp", $_); }; if ($@) { chomp $@; printf "BOOM: do_account corp => %s\n", $@; } } } my $sth_updateaccount; my $sth_updatejournal; my $sth_updatetransaction; my %accountcache; sub do_account ($$$$) { my ($api, $c, $t, $a,) = @_; return if $accountcache{$a->accountID}++; printf "ACCOUNT-%s: %i - %i - %.2f\n", uc($t), $a->accountKey, $a->accountID, $a->balance; # 'accountID' => 13972117 # 'accountKey' => 1000 # 'balance' => 371309.99 my $ownerID; if ($t eq 'char') { $ownerID = $c->characterID; } elsif ($t eq 'corp') { $ownerID = $c->corporationID; } else { die "bad account type $t"; } my $sth = $sth_updateaccount ||= $dbh->prepare(qq{ insert into account set accountID = ?, accountKey = ?, ownerID = ?, ownerType = ?, balance = ?, characterID = ?, first = UTC_TIMESTAMP(), last = UTC_TIMESTAMP() on duplicate key update balance = ?, characterID = ?, last = UTC_TIMESTAMP() }) or die $dbh->errstr; $sth->execute($a->accountID, $a->accountKey, $ownerID, $t, $a->balance, $c->characterID, $a->balance, $c->characterID ) or die $sth->errstr; $sth->finish(); #date="2007-08-10 04:48:00" #refID="1216398378" #refTypeID="54" #ownerName1="Jercy Fravowitz" #ownerID1="1040761399" #ownerName2="Secure Commerce Commission" #ownerID2="1000132" #argName1="" #argID1="0" #amount="-1680.00" #balance="60265769.68" #reason="" /> my $res = $api->$t->WalletJournal( characterID => $c->characterID, accountKey => $a->accountKey ); for my $e ( @{ $res->entries } ) { &update_name($e->ownerID1, $e->ownerName1); &update_name($e->ownerID2, $e->ownerName2); &update_name($e->argID1, $e->argName1); my $sth = $sth_updatejournal ||= $dbh->prepare(qq{ insert into journal set accountID = ?, `date` = ?, refID = ?, refTypeID = ?, ownerID1 = ?, ownerID2 = ?, argID1 = ?, amount = ?, balance = ?, reason = ?, first = UTC_TIMESTAMP(), last = UTC_TIMESTAMP() on duplicate key update last = UTC_TIMESTAMP() }) or die $dbh->errstr; $sth->execute($a->accountID, $e->date, $e->refID, $e->refTypeID, $e->ownerID1, $e->ownerID2, $e->argID1, $e->amount, $e->balance, $e->reason, ) or die $sth->errstr; $sth->finish(); } #transactionDateTime="2007-08-10 04:32:00" #transactionID="411018034" #quantity="2" #typeName="Miner II" #typeID="482" #price="200000.00" #clientID="310795569" #clientName="Rebcoby" #characterID="0" #characterName="" #stationID="60003790" #stationName="Suroken IX - Moon 18 - Caldari Navy Assembly Plant" #transactionType="sell" /> $res = $api->$t->WalletTransactions( characterID => $c->characterID, accountKey => $a->accountKey ); for my $r ( @{ $res->transactions } ) { &update_type($r->typeID, $r->typeName); &update_name($r->clientID, $r->clientName); &update_name($r->stationID, $r->stationName); my $sth = $sth_updatetransaction ||= $dbh->prepare(qq{ insert into transaction set accountID = ?, transactionDateTime = ?, transactionID = ?, typeID = ?, quantity = ?, price = ?, transactionType = ?, clientID = ?, stationID = ?, first = UTC_TIMESTAMP(), last = UTC_TIMESTAMP() on duplicate key update last = UTC_TIMESTAMP() }) or die $dbh->errstr; $sth->execute($a->accountID, $r->transactionDateTime, $r->transactionID, $r->typeID, $r->quantity, $r->price, $r->transactionType, $r->clientID, $r->stationID ) or die $sth->errstr; $sth->finish(); } } ### my $sth_nameupdate; sub update_name ($$) { my ($id, $name,) = @_; return unless $id && $name; my $sth = $sth_nameupdate ||= $dbh->prepare(qq{ insert into apiNames set itemID = ?, itemName = ?, first = UTC_TIMESTAMP(), last = UTC_TIMESTAMP on duplicate key update last = UTC_TIMESTAMP }) or die $dbh->errstr; $sth->execute($id, $name) or die $sth->errstr; $sth->finish; } my $sth_typeupdate; sub update_type ($$) { my ($id, $type,) = @_; return unless $id && $type; my $sth = $sth_typeupdate ||= $dbh->prepare(qq{ insert into apiTypes set typeID = ?, typeName = ?, first = UTC_TIMESTAMP(), last = UTC_TIMESTAMP on duplicate key update last = UTC_TIMESTAMP }) or die $dbh->errstr; $sth->execute($id, $type) or die $sth->errstr; $sth->finish; }