| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package WWW::Google::AutoSuggest; | 
| 2 | 2 |  |  | 2 |  | 52256 | use WWW::Google::AutoSuggest::Obj -base; | 
|  | 2 |  |  |  |  | 7 |  | 
|  | 2 |  |  |  |  | 21 |  | 
| 3 | 2 |  |  | 2 |  | 2613 | use LWP::UserAgent; | 
|  | 2 |  |  |  |  | 130146 |  | 
|  | 2 |  |  |  |  | 95 |  | 
| 4 | 2 |  |  | 2 |  | 26 | use URI; | 
|  | 2 |  |  |  |  | 8 |  | 
|  | 2 |  |  |  |  | 110 |  | 
| 5 | 2 |  |  | 2 |  | 2825 | use JSON; | 
|  | 2 |  |  |  |  | 31357 |  | 
|  | 2 |  |  |  |  | 17 |  | 
| 6 | 2 |  |  | 2 |  | 2737 | use Encode; | 
|  | 2 |  |  |  |  | 27364 |  | 
|  | 2 |  |  |  |  | 1198 |  | 
| 7 |  |  |  |  |  |  |  | 
| 8 |  |  |  |  |  |  | our $VERSION = '0.04'; | 
| 9 |  |  |  |  |  |  |  | 
| 10 |  |  |  |  |  |  | =encoding utf-8 | 
| 11 |  |  |  |  |  |  |  | 
| 12 |  |  |  |  |  |  | =head1 NAME | 
| 13 |  |  |  |  |  |  |  | 
| 14 |  |  |  |  |  |  | WWW::Google::AutoSuggest - Query the Google services to retrieve the query suggestions | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 17 |  |  |  |  |  |  |  | 
| 18 |  |  |  |  |  |  | use WWW::Google::AutoSuggest; | 
| 19 |  |  |  |  |  |  | my $AutoSuggest=WWW::Google::AutoSuggest->new(); | 
| 20 |  |  |  |  |  |  | my @Suggestions = $AutoSuggest->search("perl"); | 
| 21 |  |  |  |  |  |  | ###### or | 
| 22 |  |  |  |  |  |  | use WWW::Google::AutoSuggest; | 
| 23 |  |  |  |  |  |  | my $AutoSuggest=WWW::Google::AutoSuggest->new(domain=> "it" ,json=>1); #uses www.google.it instead of .com | 
| 24 |  |  |  |  |  |  | my $result = $AutoSuggest->search("perl"); | 
| 25 |  |  |  |  |  |  | # $result now is a decoded JSON arrayref | 
| 26 |  |  |  |  |  |  | ###### or with the html tags | 
| 27 |  |  |  |  |  |  | use WWW::Google::AutoSuggest; | 
| 28 |  |  |  |  |  |  | my $AutoSuggest=WWW::Google::AutoSuggest->new(strip_html=>0); | 
| 29 |  |  |  |  |  |  | my @Suggestions = $AutoSuggest->search("perl"); | 
| 30 |  |  |  |  |  |  |  | 
| 31 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 32 |  |  |  |  |  |  |  | 
| 33 |  |  |  |  |  |  | WWW::Google::AutoSuggest allows you to use Google Suggest in a quick and easy way and returning it as decoded JSON for further inspection | 
| 34 |  |  |  |  |  |  |  | 
| 35 |  |  |  |  |  |  | =head1 ARGUMENTS | 
| 36 |  |  |  |  |  |  |  | 
| 37 |  |  |  |  |  |  | =over 4 | 
| 38 |  |  |  |  |  |  |  | 
| 39 |  |  |  |  |  |  | =item json | 
| 40 |  |  |  |  |  |  |  | 
| 41 |  |  |  |  |  |  | my $AutoSuggest=WWW::Google::AutoSuggest->new(json=>1); | 
| 42 |  |  |  |  |  |  |  | 
| 43 |  |  |  |  |  |  | or | 
| 44 |  |  |  |  |  |  |  | 
| 45 |  |  |  |  |  |  | $AutoSuggest->json(1); | 
| 46 |  |  |  |  |  |  |  | 
| 47 |  |  |  |  |  |  | Explicitally enable the return of the decoded L object when calling C | 
| 48 |  |  |  |  |  |  |  | 
| 49 |  |  |  |  |  |  | =item strip_html | 
| 50 |  |  |  |  |  |  |  | 
| 51 |  |  |  |  |  |  | my $AutoSuggest=WWW::Google::AutoSuggest->new(strip_html=>0); | 
| 52 |  |  |  |  |  |  |  | 
| 53 |  |  |  |  |  |  | or | 
| 54 |  |  |  |  |  |  |  | 
| 55 |  |  |  |  |  |  | $AutoSuggest->strip_html(0); | 
| 56 |  |  |  |  |  |  |  | 
| 57 |  |  |  |  |  |  | Explicitally disable the stripping of the HTML contained in the google responses | 
| 58 |  |  |  |  |  |  |  | 
| 59 |  |  |  |  |  |  | =item raw | 
| 60 |  |  |  |  |  |  |  | 
| 61 |  |  |  |  |  |  |  | 
| 62 |  |  |  |  |  |  | my $AutoSuggest=WWW::Google::AutoSuggest->new(raw=>1); | 
| 63 |  |  |  |  |  |  |  | 
| 64 |  |  |  |  |  |  | or | 
| 65 |  |  |  |  |  |  |  | 
| 66 |  |  |  |  |  |  | $AutoSuggest->raw(1); | 
| 67 |  |  |  |  |  |  |  | 
| 68 |  |  |  |  |  |  | Explicitally enable the return of the response content when calling C | 
| 69 |  |  |  |  |  |  |  | 
| 70 |  |  |  |  |  |  | =item domain | 
| 71 |  |  |  |  |  |  |  | 
| 72 |  |  |  |  |  |  | my $AutoSuggest=WWW::Google::AutoSuggest->new(domain=>"it"); | 
| 73 |  |  |  |  |  |  |  | 
| 74 |  |  |  |  |  |  | or | 
| 75 |  |  |  |  |  |  |  | 
| 76 |  |  |  |  |  |  | $AutoSuggest->domain("it"); | 
| 77 |  |  |  |  |  |  |  | 
| 78 |  |  |  |  |  |  | Explicitally use the Google domain name in the request | 
| 79 |  |  |  |  |  |  |  | 
| 80 |  |  |  |  |  |  |  | 
| 81 |  |  |  |  |  |  | =back | 
| 82 |  |  |  |  |  |  |  | 
| 83 |  |  |  |  |  |  |  | 
| 84 |  |  |  |  |  |  | =head1 METHODS | 
| 85 |  |  |  |  |  |  |  | 
| 86 |  |  |  |  |  |  | =over 4 | 
| 87 |  |  |  |  |  |  |  | 
| 88 |  |  |  |  |  |  | =item new | 
| 89 |  |  |  |  |  |  |  | 
| 90 |  |  |  |  |  |  | my $AutoSuggest=WWW::Google::AutoSuggest->new(); | 
| 91 |  |  |  |  |  |  |  | 
| 92 |  |  |  |  |  |  | Creates a new WWW::Google::AutoSuggest object | 
| 93 |  |  |  |  |  |  |  | 
| 94 |  |  |  |  |  |  | =item search | 
| 95 |  |  |  |  |  |  |  | 
| 96 |  |  |  |  |  |  | my @Suggestions = $AutoSuggest->search($query); | 
| 97 |  |  |  |  |  |  |  | 
| 98 |  |  |  |  |  |  | Sends your C<$query> to Google web server and fetches and parse suggestions for the given query. | 
| 99 |  |  |  |  |  |  | Default returns an array of that form | 
| 100 |  |  |  |  |  |  |  | 
| 101 |  |  |  |  |  |  | @Suggestions = ( 'foo bar' , 'baar foo',..); | 
| 102 |  |  |  |  |  |  |  | 
| 103 |  |  |  |  |  |  | Setting | 
| 104 |  |  |  |  |  |  | $AutoSuggest->json(1); | 
| 105 |  |  |  |  |  |  |  | 
| 106 |  |  |  |  |  |  | will return the L object | 
| 107 |  |  |  |  |  |  |  | 
| 108 |  |  |  |  |  |  | =back | 
| 109 |  |  |  |  |  |  |  | 
| 110 |  |  |  |  |  |  | =head1 AUTHOR | 
| 111 |  |  |  |  |  |  |  | 
| 112 |  |  |  |  |  |  | mudler Emudler@dark-lab.netE | 
| 113 |  |  |  |  |  |  |  | 
| 114 |  |  |  |  |  |  | =head1 COPYRIGHT | 
| 115 |  |  |  |  |  |  |  | 
| 116 |  |  |  |  |  |  | Copyright 2014 mudler | 
| 117 |  |  |  |  |  |  |  | 
| 118 |  |  |  |  |  |  | =head1 LICENSE | 
| 119 |  |  |  |  |  |  |  | 
| 120 |  |  |  |  |  |  | This library is free software; you can redistribute it and/or modify | 
| 121 |  |  |  |  |  |  | it under the same terms as Perl itself. | 
| 122 |  |  |  |  |  |  |  | 
| 123 |  |  |  |  |  |  | =head1 SEE ALSO | 
| 124 |  |  |  |  |  |  |  | 
| 125 |  |  |  |  |  |  | L | 
| 126 |  |  |  |  |  |  |  | 
| 127 |  |  |  |  |  |  |  | 
| 128 |  |  |  |  |  |  | =cut | 
| 129 |  |  |  |  |  |  |  | 
| 130 |  |  |  |  |  |  | has 'domain'     => sub {"com"}; | 
| 131 |  |  |  |  |  |  | has 'UA'         => sub {"Mozilla/5.0"};    #eheh | 
| 132 |  |  |  |  |  |  | has 'base_url'   => sub {"/s"}; | 
| 133 |  |  |  |  |  |  | has 'strip_html' => sub {1};                #typically you want enable that | 
| 134 |  |  |  |  |  |  | has 'raw'        => sub {0}; | 
| 135 |  |  |  |  |  |  | has 'json'       => sub {0}; | 
| 136 |  |  |  |  |  |  | has 'url'        => sub {"https://www.google." . $_[0]->domain . $_[0]->base_url}; | 
| 137 |  |  |  |  |  |  |  | 
| 138 |  |  |  |  |  |  | sub search { | 
| 139 | 0 |  |  | 0 | 1 |  | my $self = shift; | 
| 140 | 0 |  |  |  |  |  | my $term = shift; | 
| 141 | 0 |  |  |  |  |  | my $ua   = LWP::UserAgent->new; | 
| 142 | 0 |  |  |  |  |  | $ua->agent( $self->UA ); | 
| 143 | 0 |  |  |  |  |  | my $url = URI->new( $self->url );   # makes an object representing the URL | 
| 144 | 0 |  |  |  |  |  | $url->query_form(                   # And here the form data pairs: | 
| 145 |  |  |  |  |  |  | 'q'     => $term, | 
| 146 |  |  |  |  |  |  | 'gs_ri' => 'psy-ab', | 
| 147 |  |  |  |  |  |  | ); | 
| 148 | 0 |  |  |  |  |  | my $res = $ua->get($url); | 
| 149 | 0 | 0 |  |  |  |  | if ( $res->is_success ) { | 
| 150 | 0 | 0 |  |  |  |  | return $res->content if ( $self->raw == 1 ); | 
| 151 | 0 |  |  |  |  |  | my $Response = decode_json( $res->content ); | 
| 152 | 0 | 0 |  |  |  |  | return $Response if ( $self->json == 1 ); | 
| 153 | 0 |  |  |  |  |  | return map { | 
| 154 | 0 |  |  |  |  |  | $_ = encode( 'utf8', $_->[0] ); | 
| 155 | 0 | 0 |  |  |  |  | s|<.+?>||g if $self->strip_html == 1; | 
| 156 | 0 |  |  |  |  |  | $_; | 
| 157 |  |  |  |  |  |  | ##Strips basic HTML tags, i don't think it's needed to load another module | 
| 158 | 0 |  |  |  |  |  | } @{ $Response->[1] }; | 
| 159 |  |  |  |  |  |  | } | 
| 160 |  |  |  |  |  |  | else { | 
| 161 | 0 |  |  |  |  |  | die( $res->status_line ); | 
| 162 |  |  |  |  |  |  | } | 
| 163 |  |  |  |  |  |  | } | 
| 164 |  |  |  |  |  |  |  | 
| 165 |  |  |  |  |  |  | 1; | 
| 166 |  |  |  |  |  |  |  | 
| 167 |  |  |  |  |  |  | __END__ |