From 6e8c48c3bedff8feac1d19abd5e201637582c32e Mon Sep 17 00:00:00 2001 From: Marius Gavrilescu Date: Sun, 15 Jun 2014 02:34:40 +0300 Subject: [PATCH 1/1] Initial commit --- Changes | 4 + MANIFEST | 7 ++ Makefile.PL | 22 ++++++ README | 32 ++++++++ lib/WWW/BackpackTF.pm | 100 +++++++++++++++++++++++++ lib/WWW/BackpackTF/User.pm | 147 +++++++++++++++++++++++++++++++++++++ t/WWW-BackpackTF.t | 6 ++ 7 files changed, 318 insertions(+) create mode 100644 Changes create mode 100644 MANIFEST create mode 100644 Makefile.PL create mode 100644 README create mode 100644 lib/WWW/BackpackTF.pm create mode 100644 lib/WWW/BackpackTF/User.pm create mode 100644 t/WWW-BackpackTF.t diff --git a/Changes b/Changes new file mode 100644 index 0000000..938b56b --- /dev/null +++ b/Changes @@ -0,0 +1,4 @@ +Revision history for Perl extension WWW::BackpackTF. + +0.000_001 2014-06-15T02:35+03:00 + - Initial release diff --git a/MANIFEST b/MANIFEST new file mode 100644 index 0000000..0ec317e --- /dev/null +++ b/MANIFEST @@ -0,0 +1,7 @@ +Changes +lib/WWW/BackpackTF.pm +lib/WWW/BackpackTF/User.pm +Makefile.PL +MANIFEST +README +t/WWW-BackpackTF.t diff --git a/Makefile.PL b/Makefile.PL new file mode 100644 index 0000000..fa272f3 --- /dev/null +++ b/Makefile.PL @@ -0,0 +1,22 @@ +use 5.014000; +use ExtUtils::MakeMaker; + +WriteMakefile( + NAME => 'WWW::BackpackTF', + VERSION_FROM => 'lib/WWW/BackpackTF.pm', + ABSTRACT_FROM => 'lib/WWW/BackpackTF.pm', + AUTHOR => 'Marius Gavrilescu ', + MIN_PERL_VERSION => '5.14.0', + LICENSE => 'perl', + SIGN => 1, + PREREQ_PM => { + qw/JSON 0 + LWP::Simple 0/, + }, + META_MERGE => { + dynamic_config => 0, + resources => { + repository => 'https://git.ieval.ro/?p=www-backpacktf.git', + } + } +); diff --git a/README b/README new file mode 100644 index 0000000..ae4459d --- /dev/null +++ b/README @@ -0,0 +1,32 @@ +WWW-BackpackTF version 0.000_001 +================================ + +WWW-BackpackTF is an interface to the backpack.tf Team Fortress 2 and Dota 2 trading service. + +It wraps the API described at http://backpack.tf/api. + +INSTALLATION + +To install this module type the following: + + perl Makefile.PL + make + make test + make install + +DEPENDENCIES + +This module requires these other modules and libraries: + +* JSON +* LWP + +COPYRIGHT AND LICENCE + +Copyright (C) 2014 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, +at your option, any later version of Perl 5 you may have available. + + diff --git a/lib/WWW/BackpackTF.pm b/lib/WWW/BackpackTF.pm new file mode 100644 index 0000000..49e6b3a --- /dev/null +++ b/lib/WWW/BackpackTF.pm @@ -0,0 +1,100 @@ +package WWW::BackpackTF; + +use 5.014000; +use strict; +use warnings; +use parent qw/Exporter/; +our $VERSION = '0.000_001'; +our @EXPORT_OK = qw/TF2 DOTA2/; + +use constant TF2 => 440; +use constant DOTA2 => 570; + +use JSON qw/decode_json/; +use LWP::Simple qw/get/; +use WWW::BackpackTF::User; + +sub new{ + my ($class, $key) = @_; + bless {key => $key}, $class +} + +sub get_users{ + my ($self, @users) = @_; + my $response = decode_json get "http://backpack.tf/api/IGetUsers/v3/?compress=1&format=json&steamids=" . join ',', @users; + $response = $response->{response}; + die $response->{message} unless $response->{success}; + @users = map { WWW::BackpackTF::User->new($_) } values $response->{players}; + wantarray ? @users : $users[0] +} + +1; +__END__ + +=head1 NAME + +WWW::BackpackTF - interface to the backpack.tf trading service + +=head1 SYNOPSIS + + use WWW::BackpackTF; + my $api_key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; + my $user_id = ; + my $bp = WWW::BackpackTF->new($api_key); + my $user = $bp->get_users($user_id); + print 'This user is named ', $user->name, ' and has ', $user->notifications, ' unread notification(s)'; + +=head1 DESCRIPTION + +WWW::BackpackTF is an interface to the backpack.tf Team Fortress 2/Dota 2 trading service. + +The only call implemented so far is I. + +=head2 METHODS + +=over + +=item B(I<[$api_key]>) + +Create a new WWW::BackpackTF object. Takes a single optional parameter, the API key. + +=item B(I<@users>) + +Get profile information for a list of users. Takes any number of 64-bit Steam IDs as arguments and returns a list of WWW::BackpackTF::User objects. This method does not require an API key. + +=back + +=head2 EXPORTS + +None by default. + +=over + +=item B + +Constant (440) representing Team Fortress 2. + +=item B + +Constant (570) representing Dota 2. + +=back + +=head1 SEE ALSO + +L, L + +=head1 AUTHOR + +Marius Gavrilescu, Emarius@ieval.roE + +=head1 COPYRIGHT AND LICENSE + +Copyright (C) 2014 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, +at your option, any later version of Perl 5 you may have available. + + +=cut diff --git a/lib/WWW/BackpackTF/User.pm b/lib/WWW/BackpackTF/User.pm new file mode 100644 index 0000000..6120ba4 --- /dev/null +++ b/lib/WWW/BackpackTF/User.pm @@ -0,0 +1,147 @@ +package WWW::BackpackTF::User; + +use 5.014000; +use strict; +use warnings; +our $VERSION = '0.000_001'; + +sub new{ + my ($class, $content) = @_; + bless $content, $class +} + +sub steamid { shift->{steamid} } +sub name { shift->{name} } +sub reputation { shift->{backpack_tf_reputation} } +sub group { shift->{backpack_tf_group} } +sub positive { shift->{backpack_tf_trust}->{for} // 0 } +sub negative { shift->{backpack_tf_trust}->{against} // 0 } +sub scammer { shift->{steamrep_scammer} } +sub banned_backpack { shift->{backpack_tf_banned} } +sub banned_economy { shift->{ban_economy} } +sub banned_community { shift->{ban_community} } +sub banned_vac { shift->{ban_vac} } +sub notifications { shift->{notifications} } +sub value { shift->{backpack_value}->{shift // WWW::BackpackTF::TF2} } +sub update { shift->{backpack_update}->{shift // WWW::BackpackTF::TF2} } + +sub banned { + my ($self) = @_; + $self->banned_backpack || $self->banned_community || $self->banned_economy || $self->banned_vac +} + +1; +__END__ + +=encoding utf-8 + +=head1 NAME + +WWW::BackpackTF::User - Class representing user profile information + +=head1 SYNOPSIS + + my $user = $bp->get_users($user_id); + say 'Steam ID: ', $user->steamid; + say 'Name: ', $user->name; + say 'Reputation: ', $user->reputation; + say 'Part of backpack.tf Steam Group: ', ($user->group ? 'YES' : 'NO'); + say 'Positive trust ratings: ', $user->positive; + say 'Negative trust ratings: ', $user->negative; + say 'Scammer (according to steamrep.com): ', ($user->scammer ? 'YES' : 'NO'); + say 'Banned from backpack.tf: ', ($user->banned_backpack ? 'YES' : 'NO'); + say 'Economy banned: ', ($user->banned_economy ? 'YES' : 'NO'); + say 'Community banned: ', ($user->banned_community ? 'YES' : 'NO'); + say 'VAC banned: ', ($user->banned_vac ? 'YES' : 'NO'); + say 'Banned on any of the previous: ', ($user->banned ? 'YES' : 'NO'); + say 'Unread notifications: ', $user->notifications; + say 'Value of TF2 backpack: ', $user->value(WWW::BackpackTF::TF2); + say 'Value of Dota 2 backpack: ', $user->value(WWW::BackpackTF::DOTA2); + say 'Last TF2 backpack update: ', $user->update(WWW::BackpackTF::TF2); + say 'Last Dota 2 backpack update: ', $user->update(WWW::BackpackTF::DOTA2); + + +=head1 DESCRIPTION + +WWW::BackpackTF::User is a class representing user profile information. + +=head2 METHODS + +=over + +=item B + +Returns this user's Steam ID. + +=item B + +Returns this user's persona name. + +=item B + +Returns this user's backpack.tf reputation. + +=item B + +Returns true if this user is part of the backpack.tf Steam group. + +=item B + +Returns the number of positive trust ratings this user has. + +=item B + +Returns the number of negative trust ratings this user has. + +=item B + +Returns true if this user is a scammer according to L + +=item B + +Returns true if this user is banned from backpack.tf. + +=item B + +Returns true if this user is economy banned. + +=item B + +Returns true if this user is community banned. + +=item B + +Returns true if this user is banned by Valve Anti-Cheat. + +=item B + +Returns true if any of the B methods returns true. + +=item B + +Returns the number of unread notifications this user has. + +=item B([I]) + +Returns the total value of this user's backpack for the specified game, in the lowest currency. I defaults to C. + +=item B([I]) + +Returns the UNIX timestamp of this user's last backpack update for the specified game. I defaults to C + +=back + +=head1 AUTHOR + +Marius Gavrilescu, Emarius@ieval.roE + +=head1 COPYRIGHT AND LICENSE + +Copyright (C) 2014 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, +at your option, any later version of Perl 5 you may have available. + + +=cut diff --git a/t/WWW-BackpackTF.t b/t/WWW-BackpackTF.t new file mode 100644 index 0000000..43e4bf1 --- /dev/null +++ b/t/WWW-BackpackTF.t @@ -0,0 +1,6 @@ +#!/usr/bin/perl -w +use strict; +use warnings; + +use Test::More tests => 1; +BEGIN { use_ok('WWW::BackpackTF') }; -- 2.39.2