Perl API Example

From EVEDev
Jump to: navigation, search

This is an simple example program to query the EVE API.

It will first query the character list, then ask you which character to use. Next it will query a list of POSes, again ask you which one to use. Last it will query the POS's fuel levels and display them.

Example Session

Please select your character:
 [1] Foo
 [2] Bar
> 2
Please select your starbase:
 [1] Moon 40xxxxx7
 [2] Moon 40xxxxx9
 [3] Moon 40xxxxx5
 [4] Moon 40xxxxx6
 [5] Moon 40xxxxx9
 [6] Moon 40xxxxx2
> 1
Your fuel levels are:
3689: 2595 units
16275: 15422 units
9848: 1851 units
3683: 13000 units
16272: 24052 units
17888: 115173 units
16273: 8365 units
9832: 4152 units
44: 2080 units

Values are as of 18:30:04 2007-12-18 (local time).

Code

Don't forget to insert your userid and apikey!

 
#!/usr/bin/perl
 
use strict;
use warnings;
use HTTP::Request::Common qw(POST);
use LWP::UserAgent;
use XML::Simple;
#use Data::Dumper qw(Dumper);
use Time::Local;
 
our $userid = 'EDIT ME';
our $apikey = 'EDIT ME';
 
our $apiroot = 'http://api.eve-online.com';
 
our $ua = LWP::UserAgent->new;
 
my $req = POST $apiroot.'/account/Characters.xml.aspx', [ userID => $userid, apiKey => $apikey ];
my $res = $ua->request($req);
 
unless ($res->is_success) {
   die "Error: " . $res->status_line . "\n";
}
 
my $text = $res->decoded_content;
#print "$text\n\n";
my $xml = XMLin($text, KeepRoot => 1, KeyAttr => {rowset => '+name', row => '+characterID'}, ForceArray => [ 'row' ]); 
$xml->{eveapi}{$xml->{eveapi}{result}{rowset}{name}} = $xml->{eveapi}{result}{rowset}; delete $xml->{eveapi}{result};
#print Dumper($xml), "\n\n";
 
print "Please select your character:\n";
my $chars = [];
foreach my $id (keys %{ $xml->{eveapi}{characters}{row} }) {
	push @$chars, $id;
	print ' ['.@$chars.'] '.$xml->{eveapi}{characters}{row}{$id}{name}."\n";
}
die "You have no characters?" unless @$chars;
print "> ";
my $input = <>;
chomp $input;
my $charid = $chars->[$input - 1]; 
 
 
$req = POST $apiroot.'/corp/StarbaseList.xml.aspx', [ userID => $userid, apiKey => $apikey, characterID => $charid, version => 2 ];
$res = $ua->request($req);
 
unless ($res->is_success) {
   die "Error: " . $res->status_line . "\n";
}
 
$text = $res->decoded_content;
#print "$text\n\n";
$xml = XMLin($text, KeepRoot => 1, KeyAttr => {rowset => '+name', row => '+itemID'}, ForceArray => [ 'row' ]); 
$xml->{eveapi}{$xml->{eveapi}{result}{rowset}{name}} = $xml->{eveapi}{result}{rowset}; delete $xml->{eveapi}{result};
#print Dumper($xml), "\n\n";
 
print "Please select your starbase:\n";
my $starbases = [];
foreach my $id (keys %{ $xml->{eveapi}{starbases}{row} }) {
	push @$starbases, $id;
	print ' ['.@$starbases.'] Moon '.$xml->{eveapi}{starbases}{row}{$id}{moonID}."\n";
}
die "You have no starbases?" unless @$starbases;
print "> ";
$input = <>;
chomp $input;
my $posid = $starbases->[$input - 1]; 
 
$req = POST $apiroot.'/corp/StarbaseDetail.xml.aspx', [ userID => $userid, apiKey => $apikey, characterID => $charid, itemID => $posid, version => 2 ];
$res = $ua->request($req);
 
unless ($res->is_success) {
   die "Error: " . $res->status_line . "\n";
}
 
$text = $res->decoded_content;
#print "$text\n\n";
$xml = XMLin($text, KeepRoot => 1, KeyAttr => {rowset => '+name', row => '+typeID'}, ForceArray => [ 'row' ]); 
$xml->{eveapi}{$xml->{eveapi}{result}{rowset}{name}} = $xml->{eveapi}{result}{rowset}; delete $xml->{eveapi}{result}{rowset};
#print Dumper($xml), "\n\n";
 
print "Your fuel levels are:\n";
foreach my $id (keys %{ $xml->{eveapi}{fuel}{row} }) {
	print "Type $id: ", $xml->{eveapi}{fuel}{row}{$id}{quantity}." units\n";
}
 
if ($xml->{eveapi}{cachedUntil} =~ m!(\d+)-(\d+)-(\d+) (\d+):(\d+):(\d+)!) {
	my ($year, $mon, $mday, $hour, $min, $sec) = ($1,$2,$3,$4,$5,$6);
	my $ts = timegm($sec,$min,$hour,$mday,$mon-1,$year) - 6*60*60;
	($year, $mon, $mday, $hour, $min, $sec) = (localtime($ts))[5,4,3,2,1,0];
	$year += 1900; $mon++;
	printf "\nValues are as of %d:%02.2d:%02.2d %d-%02.2d-%02.2d (local time).\n\n", $hour, $min, $sec, $year, $mon, $mday;
}
 

Hint: Uncomment the commented lines to see the raw data.

Personal tools