line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Yahoo::Answers; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
=head1 NAME |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
Yahoo::Answers - The great new Yahoo::Answers! |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
=head1 VERSION |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
Version 0.01 |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
=cut |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
our $VERSION = '0.03'; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
=head1 SYNOPSIS |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
Quick summary of what the module does. |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
Perhaps a little code snippet. |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
use Yahoo::Answers; |
22
|
|
|
|
|
|
|
use Data::Dumper; |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
my $ya = Yahoo::Answers->new( |
25
|
|
|
|
|
|
|
query => 'teste', |
26
|
|
|
|
|
|
|
results => 50, |
27
|
|
|
|
|
|
|
sort => 'date_desc', |
28
|
|
|
|
|
|
|
appid => |
29
|
|
|
|
|
|
|
'9J_NabHV34Fuzb1qIdxpKfQdBmV6eaMGeva5NESfQ7IDCupidoKd_cSGK7MI5Xvl.eLeQKd9YkPOU0M4DsX73A--' |
30
|
|
|
|
|
|
|
); |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
$ya->region_by_name('Brazil'); |
33
|
|
|
|
|
|
|
my $struct = $ya->get_search; |
34
|
|
|
|
|
|
|
if ( $ya->has_error ) { |
35
|
|
|
|
|
|
|
die( Dumper $ya->error ); |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
else { |
38
|
|
|
|
|
|
|
print Dumper $struct; |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
=cut |
42
|
|
|
|
|
|
|
|
43
|
1
|
|
|
1
|
|
26837
|
use Moose; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
use Moose::Util::TypeConstraints; |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
use MooseX::Types::Common::String qw/NonEmptySimpleStr SimpleStr/; |
47
|
|
|
|
|
|
|
use MooseX::Types::Common::Numeric qw/PositiveInt SingleDigit/; |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
use WWW::Mechanize; |
50
|
|
|
|
|
|
|
use URI::QueryParam; |
51
|
|
|
|
|
|
|
use URI; |
52
|
|
|
|
|
|
|
use JSON; |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
has 'mechanize' => ( |
55
|
|
|
|
|
|
|
is => 'ro', |
56
|
|
|
|
|
|
|
isa => 'WWW::Mechanize', |
57
|
|
|
|
|
|
|
lazy => 1, |
58
|
|
|
|
|
|
|
default => sub { |
59
|
|
|
|
|
|
|
my $self = shift; |
60
|
|
|
|
|
|
|
WWW::Mechanize->new( |
61
|
|
|
|
|
|
|
onerror => sub { $self->error("@_"), timeout => 120 } ); |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
); |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
has 'url' => ( |
66
|
|
|
|
|
|
|
is => 'rw', |
67
|
|
|
|
|
|
|
isa => 'Object', |
68
|
|
|
|
|
|
|
default => sub { |
69
|
|
|
|
|
|
|
URI->new( |
70
|
|
|
|
|
|
|
'http://answers.yahooapis.com/AnswersService/V1/questionSearch'); |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
); |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
has 'query' => ( |
75
|
|
|
|
|
|
|
is => 'rw', |
76
|
|
|
|
|
|
|
isa => 'Str', |
77
|
|
|
|
|
|
|
required => 0, |
78
|
|
|
|
|
|
|
predicate => 'has_query' |
79
|
|
|
|
|
|
|
); |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
subtype 'Search_in' => as Str => where { /^all$|^question$|^best_answer$/ }; |
82
|
|
|
|
|
|
|
has 'search_in' => ( |
83
|
|
|
|
|
|
|
is => 'rw', |
84
|
|
|
|
|
|
|
isa => 'Search_in', |
85
|
|
|
|
|
|
|
default => 'all', |
86
|
|
|
|
|
|
|
lazy => 1, |
87
|
|
|
|
|
|
|
predicate => 'has_search_in' |
88
|
|
|
|
|
|
|
); |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
has 'category_id' => |
91
|
|
|
|
|
|
|
( is => 'rw', isa => PositiveInt, predicate => 'has_category_id' ); |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
has 'category_name' => |
94
|
|
|
|
|
|
|
( is => 'rw', isa => SimpleStr, predicate => 'has_category_name' ); |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
subtype 'Region' => as Str => where { |
97
|
|
|
|
|
|
|
/^us$|^uk$|^ca$|^au$|^in$|^es$|^br$|^ar$|^mx$|^e1$|^it$|^de$|^fr$|^sg$/x; |
98
|
|
|
|
|
|
|
}; |
99
|
|
|
|
|
|
|
has 'region' => ( |
100
|
|
|
|
|
|
|
isa => 'rw', |
101
|
|
|
|
|
|
|
isa => 'Region', |
102
|
|
|
|
|
|
|
predicate => 'has_region', |
103
|
|
|
|
|
|
|
); |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
=head2 region_by_name |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
With this, you can pass the country name, not the "us,uk" you can |
108
|
|
|
|
|
|
|
literary write the country name. The countrys are available here: |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
United States |
111
|
|
|
|
|
|
|
United kingdom |
112
|
|
|
|
|
|
|
Canada |
113
|
|
|
|
|
|
|
Australia |
114
|
|
|
|
|
|
|
India |
115
|
|
|
|
|
|
|
Spain |
116
|
|
|
|
|
|
|
Brazil |
117
|
|
|
|
|
|
|
Argentina |
118
|
|
|
|
|
|
|
Mexico |
119
|
|
|
|
|
|
|
Italy |
120
|
|
|
|
|
|
|
Germany |
121
|
|
|
|
|
|
|
France |
122
|
|
|
|
|
|
|
Singapore |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
you can also search for only results in espanol with, |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
En espanol |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
=cut |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
sub region_by_name { |
131
|
|
|
|
|
|
|
my ( $self, $region ) = @_; |
132
|
|
|
|
|
|
|
my %country = ( |
133
|
|
|
|
|
|
|
'united states' => 'us', |
134
|
|
|
|
|
|
|
'united kingdom' => 'uk', |
135
|
|
|
|
|
|
|
'canada' => 'ca', |
136
|
|
|
|
|
|
|
'australia' => 'au', |
137
|
|
|
|
|
|
|
'india' => 'in', |
138
|
|
|
|
|
|
|
'spain' => 'es', |
139
|
|
|
|
|
|
|
'brazil' => 'br', |
140
|
|
|
|
|
|
|
'argentina' => 'ar', |
141
|
|
|
|
|
|
|
'mexico' => 'mx', |
142
|
|
|
|
|
|
|
'en espanol' => 'e1', |
143
|
|
|
|
|
|
|
'italy' => 'it', |
144
|
|
|
|
|
|
|
'germany' => 'de', |
145
|
|
|
|
|
|
|
'france' => 'fr', |
146
|
|
|
|
|
|
|
'singapore' => 'sg' |
147
|
|
|
|
|
|
|
); |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
if ( length($region) > 2 ) { |
150
|
|
|
|
|
|
|
$self->{'region'} = $country{ lc($region) } |
151
|
|
|
|
|
|
|
|| die "There is no region with the name: {$region}"; |
152
|
|
|
|
|
|
|
} |
153
|
|
|
|
|
|
|
} |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
subtype 'Date_Range' => as Str => where { /all|\d|\d\-\d|more\d/ }; |
156
|
|
|
|
|
|
|
has 'date_range' => ( |
157
|
|
|
|
|
|
|
is => 'rw', |
158
|
|
|
|
|
|
|
isa => 'Date_Range', |
159
|
|
|
|
|
|
|
default => 'all', |
160
|
|
|
|
|
|
|
lazy => 1, |
161
|
|
|
|
|
|
|
predicate => 'has_date_range' |
162
|
|
|
|
|
|
|
); |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
subtype 'Sort' => as Str => where { /relevance|date_desc|date_asc/ }; |
165
|
|
|
|
|
|
|
has 'sort' => ( |
166
|
|
|
|
|
|
|
is => 'rw', |
167
|
|
|
|
|
|
|
isa => 'Sort', |
168
|
|
|
|
|
|
|
default => 'relevance', |
169
|
|
|
|
|
|
|
lazy => 1, |
170
|
|
|
|
|
|
|
predicate => 'has_sort' |
171
|
|
|
|
|
|
|
); |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
# You can see more information at, |
174
|
|
|
|
|
|
|
# http://developer.yahoo.com/faq/index.html#appid |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
has 'appid' => ( |
177
|
|
|
|
|
|
|
is => 'rw', |
178
|
|
|
|
|
|
|
isa => NonEmptySimpleStr, |
179
|
|
|
|
|
|
|
required => 1, |
180
|
|
|
|
|
|
|
predicate => 'has_appid' |
181
|
|
|
|
|
|
|
); |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
subtype 'Search_Type', |
184
|
|
|
|
|
|
|
as Str => where { /^all$|^resolved$|^open$|^undecided$/ }; |
185
|
|
|
|
|
|
|
has 'search_type' => ( is => 'rw', isa => 'Type_', predicate => 'has_type' ); |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
has 'start' => ( is => 'rw', isa => PositiveInt, predicate => 'has_start' ); |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
subtype Results => as Int => where { $_[0] <= 50 }; |
190
|
|
|
|
|
|
|
has 'results' => ( is => 'rw', isa => 'Results', predicate => 'has_results' ); |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
has 'output' => ( is => 'ro', isa => NonEmptySimpleStr, default => 'json' ); |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
=head2 url_builder |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
Build the URL to do the "get" with all arguments that you pass |
197
|
|
|
|
|
|
|
for the attributes. |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
=cut |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
sub url_builder { |
202
|
|
|
|
|
|
|
my $self = shift; |
203
|
|
|
|
|
|
|
for my $acr ( |
204
|
|
|
|
|
|
|
'query', 'search_in', 'category_id', 'category_name', |
205
|
|
|
|
|
|
|
'region', 'date_range', 'sort', 'appid', |
206
|
|
|
|
|
|
|
'search_type', 'start', 'results', 'output' |
207
|
|
|
|
|
|
|
) |
208
|
|
|
|
|
|
|
{ |
209
|
|
|
|
|
|
|
$self->url->query_param( $acr => $self->{$acr} ) if $self->{$acr}; |
210
|
|
|
|
|
|
|
} |
211
|
|
|
|
|
|
|
} |
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
=head2 get_search |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
Make the search, and decode the JSON, if don't have the attribute |
216
|
|
|
|
|
|
|
"query", it return nothing. |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
=cut |
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
sub get_search { |
221
|
|
|
|
|
|
|
my $self = shift; |
222
|
|
|
|
|
|
|
my $json = JSON->new->allow_nonref; |
223
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
# if haven't "query" to search. |
225
|
|
|
|
|
|
|
return unless $self->has_query; |
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
if ( my $request = $self->request ) { |
228
|
|
|
|
|
|
|
my $content = $json->decode($request); |
229
|
|
|
|
|
|
|
$self->check_error($content); |
230
|
|
|
|
|
|
|
return $content; |
231
|
|
|
|
|
|
|
} |
232
|
|
|
|
|
|
|
} |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
has 'error' => ( |
235
|
|
|
|
|
|
|
is => 'rw', |
236
|
|
|
|
|
|
|
isa => 'Str', |
237
|
|
|
|
|
|
|
predicate => 'has_error', |
238
|
|
|
|
|
|
|
); |
239
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
=head2 check_error |
241
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
If have any error with your search, it sets the attribute error, |
243
|
|
|
|
|
|
|
so you can see the error and check for errors. |
244
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
=cut |
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
sub check_error { |
248
|
|
|
|
|
|
|
my ( $self, $content ) = @_; |
249
|
|
|
|
|
|
|
if ( my $error = $content->{'Error'} ) { |
250
|
|
|
|
|
|
|
$self->error($error); |
251
|
|
|
|
|
|
|
} |
252
|
|
|
|
|
|
|
|
253
|
|
|
|
|
|
|
# - Clear the query |
254
|
|
|
|
|
|
|
$self->query(0); |
255
|
|
|
|
|
|
|
return 1; |
256
|
|
|
|
|
|
|
} |
257
|
|
|
|
|
|
|
|
258
|
|
|
|
|
|
|
=head2 request |
259
|
|
|
|
|
|
|
|
260
|
|
|
|
|
|
|
Do the request, and return the content. |
261
|
|
|
|
|
|
|
|
262
|
|
|
|
|
|
|
=cut |
263
|
|
|
|
|
|
|
|
264
|
|
|
|
|
|
|
before 'request' => sub { shift->url_builder }; |
265
|
|
|
|
|
|
|
|
266
|
|
|
|
|
|
|
sub request { |
267
|
|
|
|
|
|
|
my $self = shift; |
268
|
|
|
|
|
|
|
$self->mechanize->get( $self->url ); |
269
|
|
|
|
|
|
|
$self->mechanize->success |
270
|
|
|
|
|
|
|
? return $self->mechanize->content |
271
|
|
|
|
|
|
|
: return; |
272
|
|
|
|
|
|
|
} |
273
|
|
|
|
|
|
|
|
274
|
|
|
|
|
|
|
=head1 AUTHOR |
275
|
|
|
|
|
|
|
|
276
|
|
|
|
|
|
|
Daniel de O. Mantovani, C<< <daniel.oliveira.mantovani at gmail.com> >> |
277
|
|
|
|
|
|
|
|
278
|
|
|
|
|
|
|
=head1 BUGS |
279
|
|
|
|
|
|
|
|
280
|
|
|
|
|
|
|
Please report any bugs or feature requests to C<bug-yahoo-ansewers at rt.cpan.org>, or through |
281
|
|
|
|
|
|
|
the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Yahoo-Answers>. I will be notified, and then you'll |
282
|
|
|
|
|
|
|
automatically be notified of progress on your bug as I make changes. |
283
|
|
|
|
|
|
|
|
284
|
|
|
|
|
|
|
|
285
|
|
|
|
|
|
|
|
286
|
|
|
|
|
|
|
|
287
|
|
|
|
|
|
|
=head1 SUPPORT |
288
|
|
|
|
|
|
|
|
289
|
|
|
|
|
|
|
You can find documentation for this module with the perldoc command. |
290
|
|
|
|
|
|
|
|
291
|
|
|
|
|
|
|
perldoc Yahoo::Answers |
292
|
|
|
|
|
|
|
|
293
|
|
|
|
|
|
|
|
294
|
|
|
|
|
|
|
You can also look for information at: |
295
|
|
|
|
|
|
|
|
296
|
|
|
|
|
|
|
=over 4 |
297
|
|
|
|
|
|
|
|
298
|
|
|
|
|
|
|
=item * RT: CPAN's request tracker |
299
|
|
|
|
|
|
|
|
300
|
|
|
|
|
|
|
L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Yahoo-Answers> |
301
|
|
|
|
|
|
|
|
302
|
|
|
|
|
|
|
=item * AnnoCPAN: Annotated CPAN documentation |
303
|
|
|
|
|
|
|
|
304
|
|
|
|
|
|
|
L<http://annocpan.org/dist/Yahoo-Answers> |
305
|
|
|
|
|
|
|
|
306
|
|
|
|
|
|
|
=item * CPAN Ratings |
307
|
|
|
|
|
|
|
|
308
|
|
|
|
|
|
|
L<http://cpanratings.perl.org/d/Yahoo-Answers> |
309
|
|
|
|
|
|
|
|
310
|
|
|
|
|
|
|
=item * Search CPAN |
311
|
|
|
|
|
|
|
|
312
|
|
|
|
|
|
|
L<http://search.cpan.org/dist/Yahoo-Answers/> |
313
|
|
|
|
|
|
|
|
314
|
|
|
|
|
|
|
=back |
315
|
|
|
|
|
|
|
|
316
|
|
|
|
|
|
|
|
317
|
|
|
|
|
|
|
=head1 ACKNOWLEDGEMENTS |
318
|
|
|
|
|
|
|
|
319
|
|
|
|
|
|
|
Thiago Rondon |
320
|
|
|
|
|
|
|
|
321
|
|
|
|
|
|
|
=head1 LICENSE AND COPYRIGHT |
322
|
|
|
|
|
|
|
|
323
|
|
|
|
|
|
|
Copyright 2010 Aware (www.aware.com.br) |
324
|
|
|
|
|
|
|
|
325
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it |
326
|
|
|
|
|
|
|
under the terms of either: the GNU General Public License as published |
327
|
|
|
|
|
|
|
by the Free Software Foundation; or the Artistic License. |
328
|
|
|
|
|
|
|
|
329
|
|
|
|
|
|
|
See http://dev.perl.org/licenses/ for more information. |
330
|
|
|
|
|
|
|
|
331
|
|
|
|
|
|
|
|
332
|
|
|
|
|
|
|
=cut |
333
|
|
|
|
|
|
|
|
334
|
|
|
|
|
|
|
1; # End of Yahoo::Answers |