| 1 | #!perl | ||
| 2 | |||
| 3 | package WWW::ImageSpool::Source::Google; | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | use WWW::ImageSpool::Source; | ||
| 8 | use base qw(WWW::ImageSpool::Source); | ||
| 9 | use vars qw($LIMIT $SEARCHLIMIT @EXPORT_OK); | ||
| 10 | |||
| 11 | use WWW::Google::Images; | ||
| 12 | |||
| 13 | @EXPORT_OK = qw($LIMIT); | ||
| 14 | $LIMIT = 5; | ||
| 15 | $SEARCHLIMIT = 50; | ||
| 16 | return 1; | ||
| 17 | |||
| 18 | sub new | ||
| 19 | { | ||
| 20 | my $class = shift; | ||
| 21 | my %args = @_; | ||
| 22 | my %google_args; | ||
| 23 | my $self; | ||
| 24 | |||
| 25 | $google_args{server} = $args{server} | ||
| 26 | if($args{server}); | ||
| 27 | |||
| 28 | $google_args{proxy} = $args{proxy} | ||
| 29 | if($args{proxy}); | ||
| 30 | |||
| 31 | $args{limit} ||= $LIMIT; | ||
| 32 | $args{searchlimit} ||= $SEARCHLIMIT; | ||
| 33 | |||
| 34 | if(my $agent = WWW::Google::Images->new(%google_args)) | ||
| 35 | { | ||
| 36 | $self = bless { args => \%args, google_args => \%google_args, agent => $agent, urls => {} }, $class; | ||
| 37 | } | ||
| 38 | else | ||
| 39 | { | ||
| 40 | warn "WWW::ImageSpool::Sources::Google->new(): Failed to initialize WWW::Google::Images object!\n"; | ||
| 41 | return; | ||
| 42 | } | ||
| 43 | |||
| 44 | return $self; | ||
| 45 | } | ||
| 46 | |||
| 47 | sub agent_search | ||
| 48 | { | ||
| 49 | my($self, $search, $searchlimit) = @_; | ||
| 50 | my $count = 0; | ||
| 51 | |||
| 52 | if(!$search) | ||
| 53 | { | ||
| 54 | return; | ||
| 55 | } | ||
| 56 | |||
| 57 | if(!$searchlimit) | ||
| 58 | { | ||
| 59 | $searchlimit = $self->{args}->{searchlimit}; | ||
| 60 | } | ||
| 61 | |||
| 62 | my $result = ($self->{agent}->search($search, limit => $searchlimit)); | ||
| 63 | my @result; | ||
| 64 | my $image; | ||
| 65 | |||
| 66 | while(($count < $searchlimit) && ($image = $result->next())) | ||
| 67 | { | ||
| 68 | my $url = $image->content_url(); | ||
| 69 | push(@result, $image->content_url()); | ||
| 70 | $count++; | ||
| 71 | } | ||
| 72 | if(scalar(@result)) | ||
| 73 | { | ||
| 74 | if($self->{args}->{verbose} > 2) | ||
| 75 | { | ||
| 76 | print scalar(@result), "/$searchlimit results for $search.\n"; | ||
| 77 | } | ||
| 78 | return(@result); | ||
| 79 | } | ||
| 80 | else | ||
| 81 | { | ||
| 82 | return; | ||
| 83 | } | ||
| 84 | } |