]>
Commit | Line | Data |
---|---|---|
6e8c48c3 MG |
1 | package WWW::BackpackTF; |
2 | ||
3 | use 5.014000; | |
4 | use strict; | |
5 | use warnings; | |
6 | use parent qw/Exporter/; | |
bd910ad7 | 7 | our $VERSION = '0.001'; |
6e8c48c3 MG |
8 | our @EXPORT_OK = qw/TF2 DOTA2/; |
9 | ||
116816ee | 10 | use 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 | ||
17 | BEGIN { | |
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 | 26 | use JSON::MaybeXS qw/decode_json/; |
8ab94679 | 27 | use HTTP::Tiny; |
011cd8b5 MG |
28 | use PerlX::Maybe; |
29 | use WWW::BackpackTF::Currency; | |
30 | use WWW::BackpackTF::Item; | |
6e8c48c3 MG |
31 | use WWW::BackpackTF::User; |
32 | ||
8ab94679 MG |
33 | my $ht = HTTP::Tiny->new(agent => "WWW-BackpackTF/$VERSION"); |
34 | ||
011cd8b5 MG |
35 | sub 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 | 40 | my $htr = $ht->get($url); |
116816ee | 41 | die $htr->{reason} unless $htr->{success}; ## no critic (RequireCarping) |
8ab94679 | 42 | my $response = decode_json($htr->{content})->{response}; |
116816ee | 43 | die $response->{message} unless $response->{success}; ## no critic (RequireCarping) |
011cd8b5 MG |
44 | $response |
45 | } | |
46 | ||
6e8c48c3 | 47 | sub new{ |
011cd8b5 MG |
48 | my ($class, %args) = @_; |
49 | $args{base} //= 'http://backpack.tf/api/'; | |
50 | bless \%args, $class | |
51 | } | |
52 | ||
53 | sub 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 | 59 | sub 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 |
66 | sub 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 |
72 | 1; |
73 | __END__ | |
74 | ||
bd910ad7 MG |
75 | =encoding utf-8 |
76 | ||
6e8c48c3 MG |
77 | =head1 NAME |
78 | ||
79 | WWW::BackpackTF - interface to the backpack.tf trading service | |
80 | ||
81 | =head1 SYNOPSIS | |
82 | ||
83 | use WWW::BackpackTF; | |
84 | my $api_key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; | |
85 | my $user_id = <STDIN>; | |
86 | my $bp = WWW::BackpackTF->new($api_key); | |
87 | my $user = $bp->get_users($user_id); | |
88 | print 'This user is named ', $user->name, ' and has ', $user->notifications, ' unread notification(s)'; | |
011cd8b5 MG |
89 | my @all_items_in_dota2 = $bp->get_prices(WWW::BackpackTF::DOTA2); |
90 | my @currencies = $bp->get_currencies; | |
91 | print 'The first currency is ', $currencies[0]->name; | |
6e8c48c3 MG |
92 | |
93 | =head1 DESCRIPTION | |
94 | ||
921096c3 | 95 | WWW::BackpackTF is an interface to the backpack.tf Team Fortress 2/Dota 2/Counter-Strike: Global Offensive trading service. |
6e8c48c3 | 96 | |
6e8c48c3 MG |
97 | =head2 METHODS |
98 | ||
99 | =over | |
100 | ||
1115e95d | 101 | =item B<new>([key => I<$api_key>], [base => I<$base_url>]) |
6e8c48c3 | 102 | |
1115e95d MG |
103 | Create a new WWW::BackpackTF object. Takes a hash of parameters. Possible parameters: |
104 | ||
105 | =over | |
106 | ||
107 | =item B<key> | |
108 | ||
109 | The API key. Defaults to nothing. Most methods require an API key. | |
110 | ||
111 | =item B<base> | |
112 | ||
113 | The base URL. Defaults to http://backpack.tf/api/. | |
114 | ||
115 | =back | |
6e8c48c3 | 116 | |
011cd8b5 MG |
117 | =item B<get_prices>([I<$appid>, [I<$raw>]]) |
118 | ||
119 | 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. | |
120 | ||
6e8c48c3 MG |
121 | =item B<get_users>(I<@users>) |
122 | ||
011cd8b5 MG |
123 | 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. |
124 | ||
125 | =item B<get_currencies>([I<$appid>]) | |
126 | ||
127 | Get 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 |
128 | |
129 | =back | |
130 | ||
131 | =head2 EXPORTS | |
132 | ||
133 | None by default. | |
134 | ||
135 | =over | |
136 | ||
137 | =item B<TF2> | |
138 | ||
139 | Constant (440) representing Team Fortress 2. | |
140 | ||
141 | =item B<DOTA2> | |
142 | ||
143 | Constant (570) representing Dota 2. | |
144 | ||
921096c3 MG |
145 | =item B<CSGO> |
146 | ||
147 | Constant (730) representing Counter-Strike: Global Offensive | |
148 | ||
011cd8b5 MG |
149 | =item B<NORMAL> |
150 | ||
151 | The Normal item quality (0). | |
152 | ||
153 | =item B<GENUINE> | |
154 | ||
155 | The Genuine item quality (1). | |
156 | ||
157 | =item B<RARITY2> | |
158 | ||
159 | The unused rarity2 item quality (2). | |
160 | ||
161 | =item B<VINTAGE> | |
162 | ||
163 | The Vintage item quality (3). | |
164 | ||
165 | =item B<RARITY3> | |
166 | ||
167 | The unused rarity3 item quality (4). | |
168 | ||
169 | =item B<UNUSUAL> | |
170 | ||
171 | The Unusual item quality (5). | |
172 | ||
173 | =item B<UNIQUE> | |
174 | ||
175 | The Unique item quality (6). | |
176 | ||
177 | =item B<COMMUNITY> | |
178 | ||
179 | The Community item quality (7). | |
180 | ||
181 | =item B<VALVE> | |
182 | ||
183 | The Valve item quality (8). | |
184 | ||
185 | =item B<SELFMADE> | |
186 | ||
187 | The Self-Made item quality (9). | |
188 | ||
189 | =item B<CUSTOMIZED> | |
190 | ||
191 | The unused Customized item quality (10). | |
192 | ||
193 | =item B<STRANGE> | |
194 | ||
195 | The Strange item quality (11). | |
196 | ||
197 | =item B<COMPLETED> | |
198 | ||
199 | The Completed item quality (12). | |
200 | ||
201 | =item B<HAUNTED> | |
202 | ||
203 | The Haunted item quality (13). | |
204 | ||
205 | =item B<COLLECTORS> | |
206 | ||
207 | The Collector's item quality (14). | |
208 | ||
6e8c48c3 MG |
209 | =back |
210 | ||
211 | =head1 SEE ALSO | |
212 | ||
213 | L<http://backpack.tf/>, L<http://backpack.tf/api> | |
214 | ||
215 | =head1 AUTHOR | |
216 | ||
217 | Marius Gavrilescu, E<lt>marius@ieval.roE<gt> | |
218 | ||
219 | =head1 COPYRIGHT AND LICENSE | |
220 | ||
bd910ad7 | 221 | Copyright (C) 2014, 2015 by Marius Gavrilescu |
6e8c48c3 MG |
222 | |
223 | This library is free software; you can redistribute it and/or modify | |
224 | it under the same terms as Perl itself, either Perl version 5.18.2 or, | |
225 | at your option, any later version of Perl 5 you may have available. | |
226 | ||
227 | ||
228 | =cut |