File: Google.pm

package info (click to toggle)
libnet-google-perl 0.62-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 104 kB
  • ctags: 69
  • sloc: perl: 557; makefile: 35
file content (517 lines) | stat: -rw-r--r-- 7,773 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
{

=head1 NAME

Net::Google - simple OOP-ish interface to the Google SOAP API

=head1 SYNOPSIS

 use Net::Google;
 use constant LOCAL_GOOGLE_KEY => "********************************";

 my $google = Net::Google->new(key=>LOCAL_GOOGLE_KEY);
 my $search = $google->search();

 # Search interface

 $search->query(qw(aaron straup cope));
 $search->lr(qw(en fr));
 $search->starts_at(5);
 $search->max_results(15);

 map { print $_->title()."\n"; } @{$search->results()};

 # or...

 foreach my $r (@{$search->response()}) {
   print "Search time :".$r->searchTime()."\n";

   # returns an array ref of Result objects
   # the same as the $search->results() method
   map { print $_->URL()."\n"; } @{$r->resultElements()};
 }

 # Spelling interface

 print $google->spelling(phrase=>"muntreal qwebec")->suggest(),"\n";

 # Cache interface

 my $cache = $google->cache(url=>"http://search.cpan.org/recent");
 print $cache->get();

=head1 DESCRIPTION

Provides a simple OOP-ish interface to the Google SOAP API

=head1 ENCODING

According to the Google API docs :

 "In order to support searching documents in multiple languages 
 and character encodings the Google Web APIs perform all requests 
 and responses in the UTF-8 encoding. The parameters <ie> and 
 <oe> are required in client requests but their values are ignored.
 Clients should encode all request data in UTF-8 and should expect
 results to be in UTF-8."

(This package takes care of setting both parameters in requests.)

=cut

use strict;

package Net::Google;
use base qw (Net::Google::tool);

use Carp;

$Net::Google::VERSION = '0.62';

=head1 PACKAGE METHODS

=cut

=head2 __PACKAGE__->new(\%args)

Valid arguments are :

=over 4

=item *

B<key>

I<string>. A Google API key.

=item *

B<http_proxy>

I<url>. A URL for proxy-ing HTTP requests.

=item *

B<debug>

Valid options are:

=over 4

=item *

I<boolean>

If true prints debugging information returned by SOAP::Lite
to STDERR

=item *

I<coderef>.

Your own subroutine for munging the debugging information
returned by SOAP::Lite.

=back

=back

Note that prior to version 0.60, arguments were not passed
by reference. Versions >= 0.60 are backwards compatible.

Returns an object. Woot!

=cut

sub new {
  my $pkg = shift;

  my $self = {};
  bless $self,$pkg;

  if (! $self->init(@_)) {
    return undef;
  }

  return $self;
}

sub init {
  my $self = shift;
  my $args = (ref($_[0]) eq "HASH") ? shift : {@_};

  $self->{'_debug'}      = $args->{'debug'};
  $self->{'_key'}        = $args->{'key'};
  $self->{'_http_proxy'} = $args->{'http_proxy'};

  # Do *not* call parent
  # class' init() method.

  return 1;
}

=head1 OBJECT METHODS

=cut

=head2 $obj->key($string)

Get/set the Google API key for this object.

=cut

# Defined in Net::Google::tool

=head2 $obj->http_proxy($url)

Get/set the HTTP proxy for this object.

Returns a string.

=cut

# Note we subclass the method normally inherited
# from ::tool.pm. Since this is just a wrapper
# module, we don't have a SOAP thingy to actually
# set the proxy for so we'll just cache it for
# later.

sub http_proxy {
  my $self = shift;
  my $uri  = shift;

  if ($uri) {
    $self->{'_http_proxy'} = $uri;
  }

  return $self->{'_http_proxy'};
}

=head2 $obj->search(\%args)

Valid arguments are :

=over 4

=item *

B<key>

I<string>. A Google API key. 

If none is provided then the key passed to the parent 
I<Net::Google> object will be used.

=item *

B<starts_at>

I<int>. First result number to display. 

Default is 0.

=item *

B<max_results>

I<int>. Number of results to return. 

Default is 10.

=item *

B<lr>

I<string> or I<array reference>. Language restrictions.

=item *

B<safe>

I<boolean>.

=item *

B<filter>

I<boolean>.

=item *

B<http_proxy>

I<url>. A URL for proxy-ing HTTP requests.


=item *

B<debug>

Valid options are:

=over 4

=item *

I<boolean>

If true prints debugging information returned by SOAP::Lite
to STDERR

=item *

I<coderef>

Your own subroutine for munging the debugging information
returned by SOAP::Lite.

=back

=back

Note that prior to version 0.60, arguments were not passed
by reference. Versions >= 0.60 are backwards compatible.

Returns a I<Net::Google::Search> object. Woot!

Returns undef if there was an error.

=cut

sub search {
  my $self = shift;
  require Net::Google::Search;
  return Net::Google::Search->new($self->_parse_args(@_));
}

=head2 $obj->spelling(\%args)

Valid arguments are:

=over 4

=item *

B<key>

I<string>. A Google API key. 

If none is provided then the key passed to the parent 
I<Net::Google> object will be used.

=item *

B<phrase>

I<string> or I<array reference>.

=item *

B<http_proxy>

I<url>. A URL for proxy-ing HTTP requests.

=item *

B<debug>

=over 4

=item *

B<boolean>

Prints debugging information returned by SOAP::Lite to STDERR

=item *

B<coderef>

Your own subroutine for munging the debugging information
returned by SOAP::Lite.

=back

If no option is defined then the debug argument passed to the parent
I<Net::Google> object will be used.

=back

Note that prior to version 0.60, arguments were not passed
by reference. Versions >= 0.60 are backwards compatible.

Returns a I<Net::Google::Spelling> object. Woot!

Returns undef if there was an error.

=cut

sub spelling {
  my $self = shift;
  require Net::Google::Spelling;
  return Net::Google::Spelling->new($self->_parse_args(@_));
}

# Small things are good because you can
# fit them in your hand *and* your mouth.

sub speling { return shift->spelling(@_); }

=head2 $obj->cache(\%args)

Valid arguments are :

=over 4

=item *

B<key>

String. Google API key. 

If none is provided then the key passed to the parent I<Net::Google> 
object will be used.

=item *

B<url>

I<string>

=item *

B<http_proxy>

I<url>. A URL for proxy-ing HTTP requests.

=item *

B<debug>

Valid options are:

=over 4

=item *

I<boolean>

If true, prints debugging information returned by SOAP::Lite
to STDERR

=item *

I<coderef>

Your own subroutine for munging the debugging information
returned by SOAP::Lite.

=back

If no option is defined then the debug argument passed to the parent
I<Net::Google> object will be used.

=back

Note that prior to version 0.60, arguments were not passed
by reference. Versions >= 0.60 are backwards compatible.

Returns a I<Net::Google::Cache> object. Woot!

Returns undef if there was an error.

=cut

sub cache {
  my $self = shift;
  require Net::Google::Cache;
  return Net::Google::Cache->new($self->_parse_args(@_));
}

#

sub _parse_args {
  my $self = shift;
  my $args = (ref($_[0]) eq "HASH") ? shift : {@_};

  foreach my $el ("key","debug","http_proxy") {
    next if (defined($args->{$el}));
    $args->{$el} = $self->{"_$el"};
  }

  return $args;
}

=head1 VERSION

0.62

=head1 DATE

$Date: 2004/06/02 14:25:29 $

=head1 AUTHOR

Aaron Straup Cope

=head1 CONTRIBUTORS

Marc Hedlund <marc@precipice.org>

=head1 SEE ALSO

http://www.google.com/apis

L<Net::Google::Search>

L<Net::Google::Spelling>

L<Net::Google::Cache>

L<Net::Google::Response>

L<Net::Google::Service>

http://aaronland.info/weblog/archive/4231

=head1 TO DO

=over 4

=item *

Tickle the tests so that they will pass on systems without
Test::More.

=item *

Add tests for filters.

=item *

Add some sort of functionality for managing multiple keys. 
Sort of like what is describe here :

http://aaronland.net/weblog/archive/4204

This will probably happen around the time Hell freezes over
so if you think you can do it faster, go nuts.

=back

=head1 BUGS

Please report all bugs via http://rt.cpan.org

=head1 LICENSE

Copyright (c) 2002-2004, Aaron Straup Cope. All Rights Reserved.

This is free software, you may use it and distribute it under the
same terms as Perl itself.

=cut

return 1;

}