X-Git-Url: http://git.ieval.ro/?a=blobdiff_plain;f=lib%2FWWW%2FBackpackTF.pm;h=5a2dc73dbb81665e076356d6e651e6f7afe5067e;hb=3a7fbf42bada7fe08e26c3c5c8c822f871bcb1c1;hp=448ebbb10b6ed0e61c79c23dcffa3904114ea7e4;hpb=011cd8b54a2bac28ce3a83f8b3afcab3b93677f3;p=www-backpacktf.git diff --git a/lib/WWW/BackpackTF.pm b/lib/WWW/BackpackTF.pm index 448ebbb..5a2dc73 100644 --- a/lib/WWW/BackpackTF.pm +++ b/lib/WWW/BackpackTF.pm @@ -4,12 +4,13 @@ use 5.014000; use strict; use warnings; use parent qw/Exporter/; -our $VERSION = '0.000_001'; -our @EXPORT_OK = qw/TF2 DOTA2/; +our $VERSION = '0.002001'; +our @EXPORT_OK = qw/TF2 DOTA2 CSGO/; -use constant +{ +use constant +{ ## no critic (Capitalization) TF2 => 440, DOTA2 => 570, + CSGO => 730, QUALITIES => [qw/Normal Genuine rarity2 Vintage rarity3 Unusual Unique Community Valve Self-Made Customized Strange Completed Haunted Collector's/], }; @@ -18,24 +19,31 @@ BEGIN { for (0 .. $#qualities) { my $name = uc $qualities[$_]; $name =~ y/A-Z0-9//cd; + push @EXPORT_OK, $name; constant->import($name, $_) } } -use JSON qw/decode_json/; -use LWP::Simple qw/get/; +use JSON::MaybeXS qw/decode_json/; +use HTTP::Tiny; use PerlX::Maybe; use WWW::BackpackTF::Currency; use WWW::BackpackTF::Item; +use WWW::BackpackTF::MarketItem; +use WWW::BackpackTF::Listing; use WWW::BackpackTF::User; +my $ht = HTTP::Tiny->new(agent => "WWW-BackpackTF/$VERSION"); + sub request { my ($self, $url, %params) = @_; - $params{key} = $self->{key}; + $params{key} = $self->{key} if $self->{key}; $url = $self->{base} . $url; $url .= "&$_=$params{$_}" for keys %params; - my $response = decode_json(get $url)->{response}; - die $response->{message} unless $response->{success}; + my $htr = $ht->get($url); + die $htr->{reason} unless $htr->{success}; ## no critic (RequireCarping) + my $response = decode_json($htr->{content})->{response}; + die $response->{message} unless $response->{success}; ## no critic (RequireCarping) $response } @@ -48,25 +56,42 @@ sub new{ sub get_prices { my ($self, $appid, $raw) = @_; my $response = $self->request('IGetPrices/v4/?compress=1', maybe appid => $appid, maybe raw => $raw); - map { WWW::BackpackTF::Item->new($_, $response->{items}{$_}) } keys $response->{items} + map { WWW::BackpackTF::Item->new($_, $response->{items}{$_}) } keys %{$response->{items}} } sub get_users { my ($self, @users) = @_; my $response = $self->request('IGetUsers/v3/?compress=1', steamids => join ',', @users); - @users = map { WWW::BackpackTF::User->new($_) } values $response->{players}; + @users = map { WWW::BackpackTF::User->new($_) } values %{$response->{players}}; wantarray ? @users : $users[0] } sub get_currencies { my ($self, $appid) = @_; my $response = $self->request('IGetCurrencies/v1/?compress=1', maybe appid => $appid); - map { WWW::BackpackTF::Currency->new($_, $response->{currencies}{$_}) } keys $response->{currencies}; + map { WWW::BackpackTF::Currency->new($_, $response->{currencies}{$_}) } keys %{$response->{currencies}}; +} + +# get_price_history not implemented +# get_special_items not implemented + +sub get_market_prices { + my ($self, $appid) = @_; + my $response = $self->request('IGetMarketPrices/v1/?compress=1', maybe appid => $appid); + map { WWW::BackpackTF::MarketItem->new($_, $response->{items}{$_}) } keys %{$response->{items}} +} + +sub get_user_listings { + my ($self, $steamid, $appid) = @_; + my $response = $self->request('IGetUserListings/v2/?compress=1', steamid => $steamid, maybe appid => $appid); + map { WWW::BackpackTF::Listing->new($_) } @{$response->{listings}} } 1; __END__ +=encoding utf-8 + =head1 NAME WWW::BackpackTF - interface to the backpack.tf trading service @@ -85,15 +110,27 @@ WWW::BackpackTF - interface to the backpack.tf trading service =head1 DESCRIPTION -WWW::BackpackTF is an interface to the backpack.tf Team Fortress 2/Dota 2 trading service. +WWW::BackpackTF is an interface to the backpack.tf Team Fortress 2/Dota 2/Counter-Strike: Global Offensive trading service. =head2 METHODS =over -=item B(I<[$api_key]>) +=item B([key => I<$api_key>], [base => I<$base_url>]) + +Create a new WWW::BackpackTF object. Takes a hash of parameters. Possible parameters: + +=over + +=item B + +The API key. Defaults to nothing. Most methods require an API key. + +=item B -Create a new WWW::BackpackTF object. Takes a single optional parameter, the API key. +The base URL. Defaults to http://backpack.tf/api/. + +=back =item B([I<$appid>, [I<$raw>]]) @@ -107,6 +144,14 @@ Get profile information for a list of users. Takes any number of 64-bit Steam ID Get currency information. Takes one optional parameter, the appid, which defaults to WWW::BackpackTF::TF2. Returns a list of L objects. +=item B([I<$appid>]) + +Get Steam Community Market price information for all items. Takes one optional parameter, the appid, which defaults to WWW::BackpackTF::TF2. Returns a list of L objects. + +=item B(I<$steamid>, [I<$appid>]) + +Get classified listing of a given user. Takes a mandatory 64-bit Steam ID of the user, and an optional parameter, the appid, which defaults to WWW::BackpackTF::TF2. Returns a list of L objects. + =back =head2 EXPORTS @@ -123,6 +168,10 @@ Constant (440) representing Team Fortress 2. Constant (570) representing Dota 2. +=item B + +Constant (730) representing Counter-Strike: Global Offensive + =item B The Normal item quality (0). @@ -195,7 +244,7 @@ Marius Gavrilescu, Emarius@ieval.roE =head1 COPYRIGHT AND LICENSE -Copyright (C) 2014 by Marius Gavrilescu +Copyright (C) 2014-2017 by Marius Gavrilescu This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.18.2 or,