Perl Parser Code hack

Sync Blizzards' Armory data with WoWRoster (addon depreciated no longer works see ApiSync)

Moderators: Ulminia, poetter

Perl Parser Code hack

Postby arcnon » Wed May 16, 2007 6:46 am

This morning I posted a topic asking the question about updating from the armory. I hacked something real quick but might prove useful to someone.
The idea is based on Games::WOW::Armory. The main differance is mine parses all the xml data files.

requirements perl, XML::Simple,LWP::UserAgent,
carp is a default install

file structure:
some_folder/Parser.pm
some_folder/test.pl

Parser.pm
package Parser;
use warnings;
use strict;
use Carp;

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

sub new {
my $proto = shift;
my $class = ref($proto) || $proto;
my %options = @_;
my $self = {
options => {
verbose => 1,
},

HEROIC_REPUTATIONS => {
"Keepers of Time" => "Key of Time",
"Lower City" => "Auchenai Key",
"The Sha'tar" => "Warpforged Key",
"Honor Hold" => "Flamewrought Key",
"Thrallmar" => "Flamewrought Key",
"Cenarion Expedition" => "Reservoir Key"
},
};
bless ($self, $class);
$self->init(%options);
return $self;
}
sub init {
my $self = shift;
my %options = @_;
while ( my ($key, $value) = each(%options) ) {

#print "$key => $value\n";

$key = lc($key);
$self->param(lc($key), $value);
}
croak "you need to specify a realm"
unless defined $self->param('realm');
croak "you need to specify a character name"
unless defined $self->param('character');
croak "a country was not supplied"
unless defined $self->param('country');
croak "a guild was not supplied"
unless defined $self->param('guild');

croak "Unknow region code, please choose US or EU"
unless $self->param('country') eq 'US' or $self->param('country') eq 'EU';


}


sub query_server{
my $self = shift;
my $query = shift;

my $name = $query eq 'guild' ? $self->param('guild') : $self->param('character');

my $base_url = $self->country_code($self->param('country')) .
$self->page_code($query) . "?r=" .
$self->param('realm') . "&n=" .
$name;

#print $base_url;exit;
$self->{ ua } = LWP::UserAgent->new() || croak $!;
$self->{ ua }->agent(
"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1"
);
$self->{xml} = $self->{ ua }->get( $base_url );
$self->{ xp } = XML::Simple->new;
$self->{ data } = $self->{ xp }->XMLin( $self->{ xml }->content );

}

sub character{
my $self = shift;
$self->query_server('character');

}
sub reputation{
my $self = shift;
$self->query_server('reputation');
}

sub skills{
my $self = shift;
$self->query_server('skills');
}
sub guild{
my $self = shift;
$self->query_server('guild');
}
sub talents{
my $self = shift;
$self->query_server('talents');
}
sub arenateams{
my $self = shift;
$self->query_server('arenateams');
}

sub param{
my $self = shift;
my $name = shift;
if (@_){
$self->{options}->{$name} = shift;
}
return $self->{options}->{$name} || undef;
}
sub page_code{
my $self = shift;
my $name = shift;

my %XML_FILES = (
character => 'character-sheet.xml',
reputation => 'character-reputation.xml',
skills => 'character-skills.xml',
guild => 'guild-info.xml',
talents => 'character-talents.xml',
arenateams => 'character-arenateams.xml',
);

return $XML_FILES{$name};
}
sub country_code{
my $self = shift;
my $name = shift;

my %COUNTRY = (
EU => 'http://armory.wow-europe.com/',
US => 'http://armory.worldofwarcraft.com/',
);

return $COUNTRY{$name} || $COUNTRY{US};
}


1;

__END__

test.pl
#!/usr/bin/perl
use strict;

print "Content-type: text/html\n\n";
use Data::Dumper;
use Parser;

my $obj = Parser->new(
realm => 'Norgannon', #case sensative
character => 'Tevlar', #case sensative
country => 'US', #case sensative
guild => 'Eternal Knights of Valor', #case sensative
);


##### the pound sign is perls comment mark
##### un/comment and run the script

print Dumper($obj->character);
#print Dumper($obj->reputation);
#print Dumper($obj->skills);
#print Dumper($obj->guild);
#print Dumper($obj->reputation);
#print Dumper($obj->talents);
print Dumper($obj->arenateams);


__END__
Last edited by arcnon on Wed May 16, 2007 6:47 am, edited 1 time in total.
arcnon
WR.net Apprentice
WR.net Apprentice
 
Posts: 3
Joined: Mon Jan 15, 2007 10:38 am
Location: Amarillo

Return to ArmorySync - Depreciated

Who is online

Users browsing this forum: No registered users and 1 guest

cron