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