Amethyst/Brain/Infobot/Module/Google.pm untested

filter: Statementstmt 0.0% 0 / 126 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 16 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 212 CC 19 SCAR 42.7
CC
19
Cov
0%
parse_response
182.0
handler_response
30.0
init
12.0
CRAP 71.7
1 package Amethyst::Brain::Infobot::Module::Google;
2  
3 use strict;
4 use vars qw(@ISA);
5 use Data::Dumper;
6 use POE;
7 use POE::Component::Client::UserAgent;
8 use HTTP::Request::Common;
9 use HTTP::Response;
10 use WWW::Search;
11 use WWW::SearchResult;
12 use Amethyst::Message;
13 use Amethyst::Brain::Infobot;
14 use Amethyst::Brain::Infobot::Module;
15  
16 @ISA = qw(Amethyst::Brain::Infobot::Module);
17  
18 sub new {
19 my $class = shift;
20 my $self = $class->SUPER::new(
21 Name => 'Google',
22 Regex => qr/^(?:google|search)\s+for\s+(.*)$/i,
23 Usage => '(google|search) for .*',
24 Description => "Search Google",
25 @_
26 );
27  
28 return bless $self, $class;
29 }
30  
31 sub init {
32 my $self = shift;
33  
34 eval {
35 spawn POE::Component::Client::UserAgent(
36 agent => 'Lynx/2.8rel.1 libwww-FM/2.14',
37 );
38 };
39 if ($@) {
40 die $@ unless $@ =~ /^alias is in use by another session/;
41 }
42 }
43  
44 sub action {
45 my ($self, $message, $query) = @_;
46  
47 my %states = map { $_ => "handler_$_" } qw(
48 _start response
49 );
50  
51 print STDERR "Creating child session for google\n";
52  
53 POE::Session->create(
54 package_states => [ ref($self) => \%states ],
55 args => [ $self, $message, $query ],
56 );
57  
58 return 1;
59 }
60  
61  
62 sub parse_response {
63 my ($self, $response) = @_;
64  
65 $self->{_debug} = 10;
66  
67 print STDERR "Self is $self\n";
68  
69 # parse the output
70 my ($HEADER, $HITS, $TRAILER, $POST_NEXT) = (1..10);
71  
72 my @hits = ();
73 my $hit = undef;
74  
75 my $approx_count = -1;
76  
77 my $state = $HEADER;
78  
79 my @lines = split(/\n/, $response->content);
80  
81 foreach (@lines) {
82 next unless /\S/; # short circuit for blank lines
83  
84 # print STDERR substr($_, 0, 70) . "\n" if $self->{_debug};
85 print STDERR $_ . "\n" if $self->{_debug};
86  
87 if ($state == $HEADER && m/about <b>([\d,]+)<\/b>/) {
88 $approx_count = $1;
89 print STDERR "Found Total: $approx_count\n" ;
90 $state = $HITS;
91 }
92 elsif ($state == $HITS &&
93 m|<p><a href=[^\s]*(http[^&>]*)[^>]*>(.*?)</a>|i) {
94 my ($url, $title) = ($1, $2);
95 $hit = new WWW::SearchResult();
96 push(@hits, $hit);
97  
98 print STDERR "**Found HIT Line**\n" if ($self->{_debug});
99 $url =~ s/(>.*)//g;
100 $hit->add_url(WWW::Search::strip_tags($url));
101 $title = "No Title" if ($title =~ /^\s+/);
102  
103 $hit->title(WWW::Search::strip_tags($title));
104 $state = $HITS;
105 }
106 elsif ($state == $HITS && m|Description:</font></span>\s*(.*)<br>|i) {
107 print STDERR "**Parsing Description Line**\n" if ($self->{_debug});
108 if ($hit) {
109 my $desc = $1;
110 $desc =~ s/<.*?>//g;
111 $desc =~ s/Category.*//;
112 $hit->description($desc);
113 $state = $HITS;
114 }
115 else {
116 print STDERR "ERROR: No hit when parsing description\n";
117 }
118 }
119 elsif ($state == $HITS && m@<div class=nav>@i) {
120 print STDERR "**Found Last Line**\n" if ($self->{_debug});
121 # end of hits
122 $state = $TRAILER;
123 }
124 else {
125 print STDERR "**No match**\n" if ($self->{_debug});
126 }
127 }
128  
129 return @hits;
130 }
131  
132 sub handler_response {
133 my ($kernel, $heap, $session, $pbargs) =
134 @_[KERNEL, HEAP, SESSION, ARG1];
135 my ($request, $response, $entry) = @$pbargs;
136  
137 unless ($response->is_success) {
138 my $reply = $heap->{Module}->reply_to($heap->{Message},
139 "HTTP Request failed");
140 $reply->send;
141 print STDERR $response->error_as_HTML;
142 return;
143 }
144  
145 if (0) {
146 local *LOGFILE;
147 open(LOGFILE, ">google.log") or die "Can't open file: $!";
148 print LOGFILE $response->content;
149 print LOGFILE "\n\n\n";
150 close(LOGFILE);
151 }
152  
153 # What we want here is a WWW::Search with a _separate_ parser
154 # for pages which we have alrady retrieved.
155  
156 my @hits = parse_response($heap->{Module}, $response);
157  
158 my $module = $heap->{Module};
159  
160 # print STDERR Dumper(\@hits);
161  
162 if (@hits) {
163 @hits = @hits[0..3] if @hits > 4;
164 foreach my $hit (@hits) {
165 my $url = $hit->url;
166 my $title = $hit->title;
167 my $description = $hit->description || '';
168 my $reply = $module->reply_to($heap->{Message},
169 $url . ": " . $title);
170 $reply->send;
171 }
172 }
173 else {
174 my $reply = $module->reply_to($heap->{Message}, 'No results');
175 $reply->send;
176 }
177 }
178  
179 sub handler__start {
180 my ($kernel, $heap, $session, $module, $message, $query) =
181 @_[KERNEL, HEAP, SESSION, ARG0, ARG1, ARG2];
182  
183 $heap->{Module} = $module;
184 $heap->{Message} = $message;
185 $heap->{Query} = $query;
186  
187 my $uri = new URI('http://www.google.com:80/search');
188 $uri->query_form(q => $query);
189 $uri = $uri->canonical;
190 print STDERR "Searching for " . $uri->as_string . "\n";
191 my $request = new HTTP::Request(GET => $uri);
192 my $postback = $session->postback('response');
193  
194 $kernel->post('useragent', 'request',
195 request => $request,
196 response => $postback,
197 );
198 }
199  
200 1;