blib/lib/WWW/Search/YahooNews.pm untested

filter: Statementstmt 0.0% 0 / 92 Branchbran 0.0% 0 / 46 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 156 CC 24 SCAR 55.5
CC
24
Cov
0%
native_retrieve_some
380.0
native_setup_search
42.0
BEGIN
2.0
CRAP 257.9
1 # YahooNews.pm
2 # by Jim Smyser
3 # Copyright (c) 1999 by Jim Smyser
4 # $Id: YahooNews.pm,v 1.0 2000/07/9 16:05:28 jims Exp $
5  
6 package WWW::Search::YahooNews;
7  
8 =head1 NAME
9  
10 WWW::Search::YahooNews - backend for searching Yahoo News
11  
12 =head1 SYNOPSIS
13  
14 use WWW::Search;
15 $query = "Bob Hope";
16 $search = new WWW::Search('YahooNews');
17 $search->native_query(WWW::Search::escape_query($query));
18 $search->maximum_to_retrieve(100);
19 while (my $result = $search->next_result()) {
20  
21 $url = $result->url;
22 $title = $result->title;
23 $desc = $result->description;
24  
25 print "<a href=$url>$title</a><br>$desc<p>\n";
26 }
27  
28 =head1 DESCRIPTION
29  
30 This class is a Yahoo specialization of WWW::Search. It handles making
31 and interpreting Yahoo News Searches. Yahoo allows searching a wide
32 variety of news sources like SEC and PRWire to name a few.
33 F<http://www.search.news.yahoo.com>.
34  
35 =head1 HOW DOES IT WORK?
36  
37 C<native_setup_search> is called (from C<WWW::Search::setup_search>)
38 before we do anything. It initializes our private variables (which
39 all begin with underscore) and sets up a URL to the first results
40 page in C<{_next_url}>.
41  
42 C<native_retrieve_some> is called (from C<WWW::Search::retrieve_some>)
43 whenever more hits are needed. It calls C<WWW::Search::http_request>
44 to fetch the page specified by C<{_next_url}>.
45 It then parses this page, appending any search hits it finds to
46 C<{cache}>. If it finds a ``next'' button in the text,
47 it sets C<{_next_url}> to point to the page for the next
48 set of results, otherwise it sets it to undef to indicate we''re done.
49  
50 =head1 AUTHOR
51  
52 This Backend is will now be maintained and supported by Jim Smyser.
53 Flames to: <jsmyser@bigfoot.com>
54  
55 =head1 LEGALESE
56  
57 THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
58 WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
59 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
60  
61 =head1 CHANGES
62  
63 1.0
64 First release.
65  
66 =cut
67 #'
68  
69 #####################################################################
70  
71 require Exporter;
72 @EXPORT = qw();
73 @EXPORT_OK = qw();
74 @ISA = qw(WWW::Search Exporter);
75 $VERSION = '1.00';
76  
77 $MAINTAINER = 'Jim Smyser <jsmyser@bigfoot.com>';
78  
79 use Carp ();
80 use WWW::Search(qw( generic_option strip_tags ));
81 require WWW::SearchResult;
82  
83  
84 # private
85 sub native_setup_search
86 {
87 my ($self, $native_query, $native_options_ref) = @_;
88 $self->{_debug} = $native_options_ref->{'search_debug'};
89 $self->{_debug} = 2 if ($native_options_ref->{'search_parse_debug'});
90 $self->{_debug} = 0 if (!defined($self->{_debug}));
91 $self->{agent_e_mail} = 'jsmyser@bigfoot.com';
92 $self->timeout(120);
93 $self->user_agent('user');
94 $self->{_next_to_retrieve} = 1;
95 $self->{'_num_hits'} = 0;
96 if (!defined($self->{_options}))
97 {
98 $self->{_options} = {
99 'search_url' => 'http://search.news.yahoo.com/search/news?p=' . $native_query. '&z=date&n=20&o=a',
100 };
101 }
102 my $options_ref = $self->{_options};
103 if (defined($native_options_ref))
104 {
105 # Copy in new options.
106 foreach (keys %$native_options_ref)
107 {
108 $options_ref->{$_} = $native_options_ref->{$_};
109 } # foreach
110 } # if
111 # Process the options.
112 my($options) = '';
113 foreach (sort keys %$options_ref)
114 {
115 next if (generic_option($_));
116 $options .= $_ . '=' . $options_ref->{$_} . '&';
117 }
118 chop $options;
119 # Finally figure out the url.
120 $self->{_next_url} = $self->{_options}{'search_url'} .'?'. $options;
121 } # native_setup_search
122  
123 # private
124 sub native_retrieve_some
125 {
126 my ($self) = @_;
127 print STDERR "**Getting Some**\n" if $self->{_debug};
128
129 # Fast exit if already done:
130 return undef if (!defined($self->{_next_url}));
131 $self->user_agent_delay if 1 < $self->{'_next_to_retrieve'};
132
133 # Get some:
134 print STDERR "**Requesting-> (",$self->{_next_url},")\n" if $self->{_debug};
135 my($response) = $self->http_request('GET', $self->{_next_url});
136 $self->{response} = $response;
137 if (!$response->is_success)
138 {
139 return undef;
140 }
141 $self->{'_next_url'} = undef;
142 print STDERR "**Found Some**\n" if $self->{_debug};
143  
144 # parse the output
145 my ($HEADER, $HITS) = qw(HE HH);
146 my ($hits_found) = 0;
147 my ($state) = ($HITS);
148 my ($hit) = ();
149 my $NEXT = '';
150 my $URL = '';
151 foreach ($self->split_lines($response->content()))
152 {
153 s/\r$//; # delete DOS carriage-return
154 next if m/^\r?$/; # short circuit for blank lines
155 print STDERR "\n * $state ===$_===" if 2 <= $self->{'_debug'};
156  
157
158 if ($state eq $HITS && m/^<p><a href/i)
159 {
160 print STDERR "**Found Hit's Line**" ;
161 my @mLine = split /<p>/;
162 foreach my $iHit (@mLine) {
163  
164 $URL = $1 if $iHit =~ m@<A HREF="(.*?)">.*?</a>@i;
165 $URL =~ s/\/h\//\/htx\//g;
166 next if ($URL =~ m@^/@);
167 $Title = $1 if $iHit =~ m@<A HREF=".*?">(.+)</a>@i;
168 $Title =~ s/<\/a>//g;
169 $source = $1 if $iHit =~ m@<small>(.+)</small><br>@i;
170 $desc = $1 if $iHit =~ m@<br>(.*</i></small>)@i;
171 $desc = $desc . " $source";
172 if ($URL ne '')
173 {
174 if (ref($hit))
175 {
176 push(@{$self->{cache}}, $hit);
177 };
178 $hit = new WWW::SearchResult;
179 $hit->title(strip_tags($Title));
180 $hit->add_url($URL);
181 $hit->description($desc);
182 $self->{'_num_hits'}++;
183 $hits_found++;
184 }
185 }
186 $state = $HITS;
187 }
188 if ($state eq $HITS && m@^<a href="(/search/news.*?)">@i)
189 {
190 $NEXT = $1;
191 $self->{'_next_url'} = 'http://search.news.yahoo.com' . $NEXT;
192 print STDERR "**Going to Next Page**";
193 $state = $HITS;
194 }
195 }
196 if (defined($hit))
197 {
198 push(@{$self->{cache}}, $hit);
199 }
200  
201 return $hits_found;
202 } # native_retrieve_some
203  
204 1;