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