blib/lib/WWW/Search/PRWire.pm

filter: Statementstmt 66.6% 4 / 6 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 2 / 2 Podpod n/a totaltotal 75.0% 6 / 8 CC 1 SCAR 1.4
CC
1
Cov
67%
BEGIN
1.3
BEGIN
1.0
CRAP 1.1
« PRWIRE/test.pl ↑ Summary
1 # PRWire.pm
2 # by Jim Smyser
3 # Copyright (C) 2000 by Jim Smyser
4 # $Id: PRWire.pm,v 1.00 2000/04/07 02:33:19 jims Exp $
5
6
7 package WWW::Search::PRWire;
8
9 =head1 NAME
10
11 WWW::Search::PRWire - class for viewing latest Press Releases
12
13 =head1 SYNOPSIS
14
15 use WWW::Search;
16 my $search = new WWW::Search('PRWire');
17 $search->native_query(WWW::Search::escape_query('NULL'));
18 while (my $result = $search->next_result())
19 {
20 print $result->url, "\n";
21 }
22
23 =head1 DESCRIPTION
24
25 Class for WWW::Search for fetching and parsing latest PRWire news
26 headlines. F<http://www.prnewswire.com>. This code should
27 serve as an example of using WWW::Search to parse useful data
28 from pages other than its usual searching methods. Yes, WWW::Search
29 is useful for retrieving data when there is "next" pages to get and
30 yet, no search interface to interact with.
31 See USAGE.
32
33 This class exports no public interface; all interaction should
34 be done through WWW::Search objects.
35
36 =head1 USAGE
37
38 PRWire.pm does not deal with options $native_query (Query) or any
39 others except $maximum_to_retrieve. In a sense, this is not a
40 "search" backend. It simply parses all the latest headlines and
41 retrieves more as defined by $maximum_to_retrieve.
42
43 If you use with WebSearch or AutoSearch you will need to to send a
44 bogus query to prevent complaining of NO query. Search for NULL or
45 something. On a web page you could just have a button with a caption
46 "View Latest Press Releases" and optionally perhaps a option for how
47 many to return.
48
49 $result->title returns just the date and time of the article, so you
50 will also want to print $result->description after $result->title
51 so users will have descriptive text identifying the article.
52
53 =head1 AUTHOR
54
55 C<WWW::Search::PRWire> is written and maintained by Jim Smyser
56 <jsmyser@bigfoot.com>.
57
58 =head1 COPYRIGHT
59
60 (c) PR Newswire Redistribution, retransmission, republication or
61 commercial exploitation of the contents of PR Newswire are expressly
62 prohibited without the written consent of PR Newswire.
63
64 WWW::Search Copyright (c) 1996-1998 University of Southern California.
65 All rights reserved.
66
67 THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
68 WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
69 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
70
71 =cut
72 #'
73
74 #####################################################################
75 require Exporter;
76 @EXPORT = qw();
77 @EXPORT_OK = qw();
78 @ISA = qw(WWW::Search Exporter);
79 $VERSION = '1.0';
80
81 1 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
82 0 ▶use WWW::Search(qw(generic_option strip_tags));
Subroutine BEGIN: called CC=1 SCAR=2.6
83
84 require WWW::SearchResult;
85
86 sub native_setup_search {
87
88 my($self, $native_query, $native_options_ref) = @_;
89 $self->{_debug} = $native_options_ref->{'search_debug'};
90 $self->{_debug} = 2 if ($native_options_ref->{'search_parse_debug'});
91 $self->{_debug} = 0 if (!defined($self->{_debug}));
92 $self->{agent_e_mail} = 'jsmyser@bigfoot.com';
93 $self->user_agent('user');
94 $self->{_next_to_retrieve} = 1;
95 $self->{'_num_hits'} = 0;
96 if (!defined($self->{_options})) {
97 $self->{_options} = {
98 'search_url' => 'http://www.prnewswire.com/tnw/tnw.shtml',
99 };
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 }
110 }
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 $self->{_next_url} = $self->{_options}{'search_url'};
120 }
121 # private
122 sub native_retrieve_some {
123
124 my ($self) = @_;
125 print STDERR "**PRWire Get Latest**\n" if $self->{_debug};
126
127 # Fast exit if already done:
128 return undef if (!defined($self->{_next_url}));
129
130 # If this is not the first page of results, sleep so as to not
131 # overload the server:
132 $self->user_agent_delay if 1 < $self->{'_next_to_retrieve'};
133
134 # Get some if were not already scoring somewhere else:
135 print STDERR "*Sending request (",$self->{_next_url},")\n" if $self->{_debug};
136 my($response) = $self->http_request('GET', $self->{_next_url});
137
138 $self->{response} = $response;
139 if (!$response->is_success)
140 {
141 return undef;
142 }
143 $self->{'_next_url'} = undef;
144 print STDERR "**Response\n" if $self->{_debug};
145 # parse the output
146 my ($HEADER, $HITS, $TITLE, $DESC) = qw(HE HI TI DE);
147 my $hits_found = 0;
148 my $state = $HEADER;
149 my $hit = ();
150 foreach ($self->split_lines($response->content()))
151 {
152 next if m@^$@; # short circuit for blank lines
153 print STDERR " $state ===$_=== " if 2 <= $self->{'_debug'};
154
155 if (m|<TITLE>.*?</TITLE>|i)
156 {
157 $state = $HITS;
158 }
159 elsif ($state eq $HITS && m@^<A HREF="(.*)">@i)
160 {
161 print "**Found Hit URL**\n" if 2 <= $self->{_debug};
162 my ($url) = ($1);
163 if (defined($hit))
164 {
165 push(@{$self->{cache}}, $hit);
166 };
167 $hit = new WWW::SearchResult;
168 $hits_found++;
169 $url = "http://www.prnewswire.com" . $url;
170 $hit->add_url($url);
171 $state = $TITLE;
172 }
173 elsif ($state eq $TITLE && m|^(.+)</A>|i)
174 {
175 my $sTitle = $1;
176 $hit->title($sTitle);
177 $state = $DESC;
178 }
179 elsif ($state eq $DESC && m|^<DD>(.+)|i)
180 {
181 $hit->description($1);
182 $state = $HITS;
183 }
184 elsif ($state eq $HITS && m|Click\s<A HREF="(.*?)">.*?<IMG SRC.*?>|i)
185 {
186 $sURL = $1;
187 $self->{'_next_url'} = 'http://www.prnewswire.com' . $sURL;
188 print STDERR " **Next Tag is: ", $self->{'_next_url'}, "\n" if 2 <= $self->{_debug};
189 }
190 else
191 {
192 print STDERR "**Nothing matched.**\n" if 2 <= $self->{_debug};
193 }
194 }
195 if (defined($hit))
196 {
197 push(@{$self->{cache}}, $hit);
198 }
199 return $hits_found;
200 }
201 1;
202
203
« PRWIRE/test.pl ↑ Summary