Add CSGO constant
[www-backpacktf.git] / lib / WWW / BackpackTF.pm
CommitLineData
6e8c48c3
MG
1package WWW::BackpackTF;
2
3use 5.014000;
4use strict;
5use warnings;
6use parent qw/Exporter/;
d6d9ee4c 7our $VERSION = '0.000_003';
6e8c48c3
MG
8our @EXPORT_OK = qw/TF2 DOTA2/;
9
011cd8b5
MG
10use constant +{
11 TF2 => 440,
12 DOTA2 => 570,
921096c3 13 CSGO => 730,
011cd8b5
MG
14 QUALITIES => [qw/Normal Genuine rarity2 Vintage rarity3 Unusual Unique Community Valve Self-Made Customized Strange Completed Haunted Collector's/],
15};
16
17BEGIN {
18 my @qualities = @{QUALITIES()};
19 for (0 .. $#qualities) {
20 my $name = uc $qualities[$_];
21 $name =~ y/A-Z0-9//cd;
22 constant->import($name, $_)
23 }
24}
6e8c48c3 25
1e12c1b3 26use JSON::MaybeXS qw/decode_json/;
8ab94679 27use HTTP::Tiny;
011cd8b5
MG
28use PerlX::Maybe;
29use WWW::BackpackTF::Currency;
30use WWW::BackpackTF::Item;
6e8c48c3
MG
31use WWW::BackpackTF::User;
32
8ab94679
MG
33my $ht = HTTP::Tiny->new(agent => "WWW-BackpackTF/$VERSION");
34
011cd8b5
MG
35sub request {
36 my ($self, $url, %params) = @_;
af9ae5ee 37 $params{key} = $self->{key} if $self->{key};
011cd8b5
MG
38 $url = $self->{base} . $url;
39 $url .= "&$_=$params{$_}" for keys %params;
8ab94679
MG
40 my $htr = $ht->get($url);
41 die $htr->{reason} unless $htr->{success};
42 my $response = decode_json($htr->{content})->{response};
011cd8b5
MG
43 die $response->{message} unless $response->{success};
44 $response
45}
46
6e8c48c3 47sub new{
011cd8b5
MG
48 my ($class, %args) = @_;
49 $args{base} //= 'http://backpack.tf/api/';
50 bless \%args, $class
51}
52
53sub get_prices {
54 my ($self, $appid, $raw) = @_;
55 my $response = $self->request('IGetPrices/v4/?compress=1', maybe appid => $appid, maybe raw => $raw);
72f329fe 56 map { WWW::BackpackTF::Item->new($_, $response->{items}{$_}) } keys %{$response->{items}}
6e8c48c3
MG
57}
58
011cd8b5 59sub get_users {
6e8c48c3 60 my ($self, @users) = @_;
011cd8b5 61 my $response = $self->request('IGetUsers/v3/?compress=1', steamids => join ',', @users);
72f329fe 62 @users = map { WWW::BackpackTF::User->new($_) } values %{$response->{players}};
6e8c48c3
MG
63 wantarray ? @users : $users[0]
64}
65
011cd8b5
MG
66sub get_currencies {
67 my ($self, $appid) = @_;
68 my $response = $self->request('IGetCurrencies/v1/?compress=1', maybe appid => $appid);
72f329fe 69 map { WWW::BackpackTF::Currency->new($_, $response->{currencies}{$_}) } keys %{$response->{currencies}};
011cd8b5
MG
70}
71
6e8c48c3
MG
721;
73__END__
74
75=head1 NAME
76
77WWW::BackpackTF - interface to the backpack.tf trading service
78
79=head1 SYNOPSIS
80
81 use WWW::BackpackTF;
82 my $api_key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
83 my $user_id = <STDIN>;
84 my $bp = WWW::BackpackTF->new($api_key);
85 my $user = $bp->get_users($user_id);
86 print 'This user is named ', $user->name, ' and has ', $user->notifications, ' unread notification(s)';
011cd8b5
MG
87 my @all_items_in_dota2 = $bp->get_prices(WWW::BackpackTF::DOTA2);
88 my @currencies = $bp->get_currencies;
89 print 'The first currency is ', $currencies[0]->name;
6e8c48c3
MG
90
91=head1 DESCRIPTION
92
921096c3 93WWW::BackpackTF is an interface to the backpack.tf Team Fortress 2/Dota 2/Counter-Strike: Global Offensive trading service.
6e8c48c3 94
6e8c48c3
MG
95=head2 METHODS
96
97=over
98
1115e95d 99=item B<new>([key => I<$api_key>], [base => I<$base_url>])
6e8c48c3 100
1115e95d
MG
101Create a new WWW::BackpackTF object. Takes a hash of parameters. Possible parameters:
102
103=over
104
105=item B<key>
106
107The API key. Defaults to nothing. Most methods require an API key.
108
109=item B<base>
110
111The base URL. Defaults to http://backpack.tf/api/.
112
113=back
6e8c48c3 114
011cd8b5
MG
115=item B<get_prices>([I<$appid>, [I<$raw>]])
116
117Get price information for all items. Takes two optional parameters. The first parameter is the appid and defaults to WWW::BackpackTF::TF2. The second (if true) adds a value_raw property to prices and defaults to false. Returns a list of L<WWW::BackpackTF::Item> objects.
118
6e8c48c3
MG
119=item B<get_users>(I<@users>)
120
011cd8b5
MG
121Get profile information for a list of users. Takes any number of 64-bit Steam IDs as arguments and returns a list of L<WWW::BackpackTF::User> objects. This method does not require an API key. Dies with an error message if the operation is unsuccessful.
122
123=item B<get_currencies>([I<$appid>])
124
125Get currency information. Takes one optional parameter, the appid, which defaults to WWW::BackpackTF::TF2. Returns a list of L<WWW::BackpackTF::Currency> objects.
6e8c48c3
MG
126
127=back
128
129=head2 EXPORTS
130
131None by default.
132
133=over
134
135=item B<TF2>
136
137Constant (440) representing Team Fortress 2.
138
139=item B<DOTA2>
140
141Constant (570) representing Dota 2.
142
921096c3
MG
143=item B<CSGO>
144
145Constant (730) representing Counter-Strike: Global Offensive
146
011cd8b5
MG
147=item B<NORMAL>
148
149The Normal item quality (0).
150
151=item B<GENUINE>
152
153The Genuine item quality (1).
154
155=item B<RARITY2>
156
157The unused rarity2 item quality (2).
158
159=item B<VINTAGE>
160
161The Vintage item quality (3).
162
163=item B<RARITY3>
164
165The unused rarity3 item quality (4).
166
167=item B<UNUSUAL>
168
169The Unusual item quality (5).
170
171=item B<UNIQUE>
172
173The Unique item quality (6).
174
175=item B<COMMUNITY>
176
177The Community item quality (7).
178
179=item B<VALVE>
180
181The Valve item quality (8).
182
183=item B<SELFMADE>
184
185The Self-Made item quality (9).
186
187=item B<CUSTOMIZED>
188
189The unused Customized item quality (10).
190
191=item B<STRANGE>
192
193The Strange item quality (11).
194
195=item B<COMPLETED>
196
197The Completed item quality (12).
198
199=item B<HAUNTED>
200
201The Haunted item quality (13).
202
203=item B<COLLECTORS>
204
205The Collector's item quality (14).
206
6e8c48c3
MG
207=back
208
209=head1 SEE ALSO
210
211L<http://backpack.tf/>, L<http://backpack.tf/api>
212
213=head1 AUTHOR
214
215Marius Gavrilescu, E<lt>marius@ieval.roE<gt>
216
217=head1 COPYRIGHT AND LICENSE
218
219Copyright (C) 2014 by Marius Gavrilescu
220
221This library is free software; you can redistribute it and/or modify
222it under the same terms as Perl itself, either Perl version 5.18.2 or,
223at your option, any later version of Perl 5 you may have available.
224
225
226=cut
This page took 0.023833 seconds and 4 git commands to generate.