line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# |
2
|
|
|
|
|
|
|
# This file is part of WebService-Google-Suggest |
3
|
|
|
|
|
|
|
# |
4
|
|
|
|
|
|
|
# This software is copyright (c) 2011 by Tasuhiko Miyagawa. |
5
|
|
|
|
|
|
|
# |
6
|
|
|
|
|
|
|
# This is free software; you can redistribute it and/or modify it under |
7
|
|
|
|
|
|
|
# the same terms as the Perl 5 programming language system itself. |
8
|
|
|
|
|
|
|
# |
9
|
|
|
|
|
|
|
package WebService::Google::Suggest; |
10
|
|
|
|
|
|
|
BEGIN { |
11
|
2
|
|
|
2
|
|
53564
|
$WebService::Google::Suggest::VERSION = '0.05'; |
12
|
|
|
|
|
|
|
} |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
# ABSTRACT: Google Suggest as an API |
15
|
|
|
|
|
|
|
|
16
|
2
|
|
|
2
|
|
21
|
use strict; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
75
|
|
17
|
2
|
|
|
2
|
|
11
|
use warnings; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
66
|
|
18
|
|
|
|
|
|
|
|
19
|
2
|
|
|
2
|
|
11
|
use Carp; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
208
|
|
20
|
2
|
|
|
2
|
|
5031
|
use LWP::UserAgent; |
|
2
|
|
|
|
|
126955
|
|
|
2
|
|
|
|
|
74
|
|
21
|
2
|
|
|
2
|
|
25
|
use URI::Escape; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
935
|
|
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
our $CompleteURL = "http://www.google.com/complete/search?hl=en&js=true&qu="; |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
sub new { |
26
|
1
|
|
|
1
|
1
|
15
|
my $class = shift; |
27
|
1
|
|
|
|
|
10
|
my $ua = LWP::UserAgent->new(); |
28
|
1
|
|
|
|
|
8062
|
$ua->agent("Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"); |
29
|
1
|
|
|
|
|
98
|
bless { ua => $ua }, $class; |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
2
|
|
|
2
|
1
|
24
|
sub ua { $_[0]->{ua} } |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
sub complete { |
35
|
1
|
|
|
1
|
1
|
4
|
my ( $self, $query ) = @_; |
36
|
1
|
|
|
|
|
8
|
my $url = $CompleteURL . uri_escape($query); |
37
|
|
|
|
|
|
|
|
38
|
1
|
|
|
|
|
37
|
my $response = $self->ua->get($url); |
39
|
1
|
50
|
|
|
|
277030
|
$response->is_success |
40
|
|
|
|
|
|
|
or croak "Google doesn't respond well: ", $response->code; |
41
|
|
|
|
|
|
|
|
42
|
1
|
|
|
|
|
23
|
my $content = $response->content(); |
43
|
1
|
50
|
|
|
|
201
|
$content =~ /^window\.google\.ac\.\w+\(\["([^"]+)",\[(.*)\]\)$/ |
44
|
|
|
|
|
|
|
or croak "Google returns unrecognized format: $content"; |
45
|
|
|
|
|
|
|
|
46
|
0
|
|
|
|
|
|
my ( $user_query, $array ) = ( $1, $2 ); |
47
|
0
|
|
|
|
|
|
my @results; |
48
|
0
|
|
|
|
|
|
while ( $array =~ /\[([^\]]+)\]/g ) { |
49
|
0
|
|
|
|
|
|
my $row = $1; |
50
|
0
|
|
|
|
|
|
my ( $query, $count, $rank ) = $row =~ /\"([^"]+)\",\"([\d]+)?\",\"([\d]+)?\"/; |
51
|
0
|
|
|
|
|
|
$count += 0; # numify |
52
|
0
|
|
|
|
|
|
$rank += 0; |
53
|
0
|
|
|
|
|
|
push @results, { query => $query, results => $count, rank => $rank }; |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
0
|
|
|
|
|
|
return @results; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
1; |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
=pod |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
=head1 NAME |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
WebService::Google::Suggest - Google Suggest as an API |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
=head1 VERSION |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
version 0.05 |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=head1 SYNOPSIS |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
use WebService::Google::Suggest; |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
my $suggest = WebService::Google::Suggest->new(); |
77
|
|
|
|
|
|
|
my @suggestions = $suggest->complete("goog"); |
78
|
|
|
|
|
|
|
for my $suggestion (@suggestions) { |
79
|
|
|
|
|
|
|
print "[" . $suggestion->{rank} . "] " |
80
|
|
|
|
|
|
|
. $suggestion->{query} . ":" |
81
|
|
|
|
|
|
|
. $suggestion->{results} results . "\n"; |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
=head1 DESCRIPTION |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
WebService::Google::Suggest allows you to use Google Suggest as a Web Service API to retrieve completions to your search query or partial query. This module is based on Adam Stiles' hack (http://www.adamstiles.com/adam/2004/12/hacking_google_.html). |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
=head1 METHODS |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
=over 4 |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
=item new |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
$suggest = WebService::Google::Suggest->new(); |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
Creates new WebService::Google::Suggest object. |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=item complete |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
@suggestions = $suggest->complete($query); |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
Sends your C<$query> to Google web server and fetches suggestions for |
103
|
|
|
|
|
|
|
the query. Suggestions are in a list of hashrefs, for example with |
104
|
|
|
|
|
|
|
query "Google": |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
@suggestions = ( |
107
|
|
|
|
|
|
|
{ query => "google", results => 0, rank => 0 }, |
108
|
|
|
|
|
|
|
{ query => "google toolbar", results => 0, rank => 1 }, |
109
|
|
|
|
|
|
|
... |
110
|
|
|
|
|
|
|
); |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
Note that C value does NOT contain commas and "results" text. |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
=item ua |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
$ua = $suggest->ua; |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
Returns underlying LWP::UserAgent object. It allows you to change |
119
|
|
|
|
|
|
|
User-Agent (Windows IE by default), timeout seconds and various |
120
|
|
|
|
|
|
|
properties. |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=back |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=head1 SEE ALSO |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
http://www.adamstiles.com/adam/2004/12/hacking_google_.html |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
http://www.google.com/webhp?complete=1&hl=en |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
http://labs.google.com/suggest/faq.html |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
=head1 AUTHOR |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
Tatsuhiko Miyagawa , franck cuny |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
This software is copyright (c) 2011 by Tasuhiko Miyagawa. |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
141
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
=cut |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
__END__ |