| 1 | |||||||||||||
| 2 | # $Id: Advanced.pm,v 2.62 2009/05/02 16:42:37 Martin Exp $ | ||||||||||||
| 3 | |||||||||||||
| 4 | =head1 NAME | ||||||||||||
| 5 | |||||||||||||
| 6 | WWW::Search::Yahoo::News::Advanced - search Yahoo!News using the | ||||||||||||
| 7 | "advanced" interface | ||||||||||||
| 8 | |||||||||||||
| 9 | =head1 SYNOPSIS | ||||||||||||
| 10 | |||||||||||||
| 11 | use WWW::Search; | ||||||||||||
| 12 | my $oSearch = new WWW::Search('Yahoo::News::Advanced'); | ||||||||||||
| 13 | my $sQuery = WWW::Search::escape_query("George Lucas"); | ||||||||||||
| 14 | $oSearch->date_from('7 days ago'); | ||||||||||||
| 15 | $oSearch->date_to ('now'); | ||||||||||||
| 16 | $oSearch->native_query($sQuery); | ||||||||||||
| 17 | while (my $oResult = $oSearch->next_result()) | ||||||||||||
| 18 | print $oResult->url, "\n"; | ||||||||||||
| 19 | |||||||||||||
| 20 | =head1 DESCRIPTION | ||||||||||||
| 21 | |||||||||||||
| 22 | This class is a Yahoo! News specialization of L<WWW::Search>. It | ||||||||||||
| 23 | handles making and interpreting searches on Yahoo! News | ||||||||||||
| 24 | F<http://news.yahoo.com> using the Advanced search interface. | ||||||||||||
| 25 | |||||||||||||
| 26 | This class exports no public interface; all interaction should | ||||||||||||
| 27 | be done through L<WWW::Search> objects. | ||||||||||||
| 28 | |||||||||||||
| 29 | =head1 NOTES | ||||||||||||
| 30 | |||||||||||||
| 31 | This backend supports narrowing the search by date-range. | ||||||||||||
| 32 | Use date_from() and date_to() to set the endpoints of the desired date range. | ||||||||||||
| 33 | You can use any date format supported by the Date::Manip module. | ||||||||||||
| 34 | |||||||||||||
| 35 | NOTE that Yahoo only seems to keep the last 60 days worth of news in its searchable index. | ||||||||||||
| 36 | |||||||||||||
| 37 | At one time, News.yahoo.com would die if the unescaped query is longer | ||||||||||||
| 38 | than 485 characters or so. This backend does NOT check for that. | ||||||||||||
| 39 | |||||||||||||
| 40 | =head1 SEE ALSO | ||||||||||||
| 41 | |||||||||||||
| 42 | To make new back-ends, see L<WWW::Search>. | ||||||||||||
| 43 | |||||||||||||
| 44 | =head1 BUGS | ||||||||||||
| 45 | |||||||||||||
| 46 | To report a new bug, please use | ||||||||||||
| 47 | https://rt.cpan.org/Ticket/Create.html?Queue=WWW-Search-Yahoo | ||||||||||||
| 48 | |||||||||||||
| 49 | =head1 AUTHOR | ||||||||||||
| 50 | |||||||||||||
| 51 | C<WWW::Search::Yahoo::News::Advanced> is maintained by Martin Thurn | ||||||||||||
| 52 | (mthurn@cpan.org). | ||||||||||||
| 53 | |||||||||||||
| 54 | =head1 LEGALESE | ||||||||||||
| 55 | |||||||||||||
| 56 | Copyright (C) 1998-2009 Martin 'Kingpin' Thurn | ||||||||||||
| 57 | |||||||||||||
| 58 | THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED | ||||||||||||
| 59 | WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF | ||||||||||||
| 60 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | ||||||||||||
| 61 | |||||||||||||
| 62 | =cut | ||||||||||||
| 63 | |||||||||||||
| 64 | ##################################################################### | ||||||||||||
| 65 | |||||||||||||
| 66 | package WWW::Search::Yahoo::News::Advanced; | ||||||||||||
| 67 | |||||||||||||
| 68 | 2 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 69 | 2 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 70 | |||||||||||||
| 71 | 2 | ▶ | use Data::Dumper; # for debugging only | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 72 | 2 | ▶ | use Date::Manip; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 73 | 2 | ▶ | use WWW::Search qw( strip_tags ); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 74 | 2 | ▶ | use WWW::Search::Result; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 75 | 2 | ▶ | use WWW::Search::Yahoo; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 76 | |||||||||||||
| 77 | 2 | ▶ | use base 'WWW::Search::Yahoo'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 78 | our | ||||||||||||
| 79 | 2 | $VERSION = do { my @r = (q$Revision: 2.62 $ =~ /\d+/g); sprintf "%d."."%03d" x $#r, @r }; | |||||||||||
| 80 | 2 | our $MAINTAINER = 'Martin Thurn <mthurn@cpan.org>'; | |||||||||||
| 81 | |||||||||||||
| 82 | sub _native_setup_search | ||||||||||||
| 83 | 3 | ▶ | { | ||||||||||
|
Subroutine
_native_setup_search: called CC=6 SCAR=24.5
| |||||||||||||
| 84 | 3 | my ($self, $sQuery, $rh) = @_; | |||||||||||
| 85 | # print STDERR " + in Y::N::A::_native_setup_search, rh is ", Dumper($rh); | ||||||||||||
| 86 | 3 | ▶ | my $sDateFrom = $self->date_from || ''; | ||||||||||
|
Condition50%1/2
$self->date_from || ''
MC/DC0%0/1
$self->date_from || ''
$self->date_from
Truth table50%1/2
A
A= $self->date_from
| |||||||||||||
| 87 | 3 | ▶ | my $sDateTo = $self->date_to || ''; | ||||||||||
|
Condition50%1/2
$self->date_to || ''
MC/DC0%0/1
$self->date_to || ''
$self->date_to
Truth table50%1/2
A
A= $self->date_to
| |||||||||||||
| 88 | 3 | my $iUseDate = 0; | |||||||||||
| 89 | 3 | my ($iMonthFrom, $iDayFrom, $iMonthTo, $iDayTo); | |||||||||||
| 90 | 3 | ▶ | if ($sDateFrom ne '') | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 91 | { | ||||||||||||
| 92 | # User specified the beginning date. | ||||||||||||
| 93 | 0 | my $dateFrom = &ParseDate($sDateFrom); | |||||||||||
| 94 | 0 | $iMonthFrom = &UnixDate($dateFrom, '%m'); | |||||||||||
| 95 | 0 | $iDayFrom = &UnixDate($dateFrom, '%d'); | |||||||||||
| 96 | 0 | $iUseDate = 1; | |||||||||||
| 97 | } | ||||||||||||
| 98 | else | ||||||||||||
| 99 | { | ||||||||||||
| 100 | # User did not specify the beginning date. Set it to the distant | ||||||||||||
| 101 | # past. Yahoo.com barfs if it gets a date earlier than 1999, | ||||||||||||
| 102 | # though. | ||||||||||||
| 103 | 3 | $sDateFrom = &UnixDate(&ParseDate('1999-01-01'), '%m/%d/%y'); | |||||||||||
| 104 | } | ||||||||||||
| 105 | 3 | ▶ | if ($sDateTo ne '') | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 106 | { | ||||||||||||
| 107 | # User specified the ending date. | ||||||||||||
| 108 | 0 | my $date = &ParseDate($sDateTo); | |||||||||||
| 109 | 0 | $iMonthTo = &UnixDate($date, '%m'); | |||||||||||
| 110 | 0 | $iDayTo = &UnixDate($date, '%d'); | |||||||||||
| 111 | 0 | $iUseDate = 1; | |||||||||||
| 112 | } | ||||||||||||
| 113 | else | ||||||||||||
| 114 | { | ||||||||||||
| 115 | # User did not specify the ending date. Set it to the future: | ||||||||||||
| 116 | 3 | $sDateTo = &UnixDate(&ParseDate('tomorrow'), '%m/%d/%y'); | |||||||||||
| 117 | } | ||||||||||||
| 118 | # With default search options: | ||||||||||||
| 119 | # http://news.search.yahoo.com/search/news?p=Aomori&c=&ei=UTF-8&fl=0&n=100&x=wrt | ||||||||||||
| 120 | |||||||||||||
| 121 | 3 | $self->{'_options'} = { | |||||||||||
| 122 | 'ei' => 'UTF-8', | ||||||||||||
| 123 | 'fl' => 0, | ||||||||||||
| 124 | 'n' => 100, # 10 for testing, 100 for release | ||||||||||||
| 125 | 'p' => $sQuery, | ||||||||||||
| 126 | }; | ||||||||||||
| 127 | 3 | ▶ | if ($iUseDate) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 128 | { | ||||||||||||
| 129 | # This is the url when user chose date range: | ||||||||||||
| 130 | # http://news.search.yahoo.com/search/news?ei=UTF-8&fr=&va=Aomori&va_vt=any&vp=&vp_vt=any&vo=&vo_vt=any&ve=&ve_vt=any&&pub=1&smonth=3&sday=22&emonth=3&eday=30&source=&location=&fl=0&n=100 | ||||||||||||
| 131 | 0 | $self->{'_options'}->{'pub'} = 1; | |||||||||||
| 132 | 0 | delete $self->{'_options'}->{'fl'}; | |||||||||||
| 133 | 0 | delete $self->{'_options'}->{'p'}; | |||||||||||
| 134 | 0 | $self->{'_options'}->{'va'} = $sQuery; | |||||||||||
| 135 | 0 | $self->{'_options'}->{'va_vt'} = 'any'; | |||||||||||
| 136 | 0 | $self->{'_options'}->{'smonth'} = $iMonthFrom; | |||||||||||
| 137 | 0 | $self->{'_options'}->{'sday'} = $iDayFrom; | |||||||||||
| 138 | 0 | $self->{'_options'}->{'emonth'} = $iMonthTo; | |||||||||||
| 139 | 0 | $self->{'_options'}->{'eday'} = $iDayTo; | |||||||||||
| 140 | } # if | ||||||||||||
| 141 | 3 | $rh->{'search_base_url'} = 'http://news.search.yahoo.com'; | |||||||||||
| 142 | 3 | $rh->{'search_base_path'} = '/search/news/'; | |||||||||||
| 143 | # print STDERR " + Y::N::A::_native_setup_search() is calling SUPER::_native_setup_search()...\n"; | ||||||||||||
| 144 | 3 | return $self->SUPER::_native_setup_search($sQuery, $rh); | |||||||||||
| 145 | } # _native_setup_search | ||||||||||||
| 146 | |||||||||||||
| 147 | |||||||||||||
| 148 | sub _parse_tree | ||||||||||||
| 149 | 0 | ▶ | { | ||||||||||
|
Subroutine
_parse_tree: not called CC=18 SCAR=58.3
| |||||||||||||
| 150 | 0 | my $self = shift; | |||||||||||
| 151 | 0 | my $tree = shift; | |||||||||||
| 152 | 0 | my $hits_found = 0; | |||||||||||
| 153 | 0 | my @aoFONTcount = $tree->look_down('_tag', 'div', | |||||||||||
| 154 | 'id' => 'yschtools', | ||||||||||||
| 155 | ); | ||||||||||||
| 156 | FONTcount_TAG: | ||||||||||||
| 157 | 0 | foreach my $oFONT (@aoFONTcount) | |||||||||||
| 158 | { | ||||||||||||
| 159 | 0 | my $s = $oFONT->as_text; | |||||||||||
| 160 | 0 | ▶ | print STDERR " + FONTcount == ", $oFONT->as_HTML if 2 <= $self->{_debug}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 161 | # print STDERR " + TEXT == ", $s, "\n" if 2 <= $self->{_debug}; | ||||||||||||
| 162 | 0 | ▶ | if ($s =~ m!Results\s+\d+\s*-\s*\d+\s+of\s+(?:about\s+)?([0-9,]+)!) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 163 | { | ||||||||||||
| 164 | 0 | my $iCount = $1; | |||||||||||
| 165 | 0 | $iCount =~ s!,!!g; | |||||||||||
| 166 | # print STDERR " + found number $iCount\n" if 2 <= $self->{_debug}; | ||||||||||||
| 167 | 0 | $self->approximate_result_count($iCount); | |||||||||||
| 168 | 0 | last FONTcount_TAG; | |||||||||||
| 169 | } # if | ||||||||||||
| 170 | } # foreach FONT_TAG | ||||||||||||
| 171 | |||||||||||||
| 172 | 0 | my @aoA = $tree->look_down('_tag' => 'a', | |||||||||||
| 173 | 'class' => 'yschttl', | ||||||||||||
| 174 | ); | ||||||||||||
| 175 | A_TAG: | ||||||||||||
| 176 | 0 | foreach my $oA (@aoA) | |||||||||||
| 177 | { | ||||||||||||
| 178 | 0 | ▶ | printf STDERR "\n + A == %s", $oA->as_HTML if 2 <= $self->{_debug}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 179 | 0 | my $sMouseOver = $oA->attr('onmouseover'); | |||||||||||
| 180 | 0 | ▶ | next A_TAG unless ($sMouseOver =~ m!window\.status='(.+)'!); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 181 | 0 | my $sURL = $1; | |||||||||||
| 182 | 0 | ▶ | next A_TAG unless defined($sURL); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 183 | 0 | ▶ | next A_TAG unless ($sURL ne ''); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 184 | 0 | ▶ | print STDERR " + URL == $sURL\n" if 2 <= $self->{_debug}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 185 | 0 | my $sTitle = $oA->as_text; | |||||||||||
| 186 | 0 | ▶ | print STDERR " + TITLE == $sTitle\n" if 2 <= $self->{_debug}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 187 | # In order to make it easier to parse, make sure everything is an object! | ||||||||||||
| 188 | 0 | my $oLI = $oA->look_up(_tag => 'li'); | |||||||||||
| 189 | 0 | ▶ | next A_TAG unless ref($oLI); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 190 | 0 | $oA->detach; | |||||||||||
| 191 | 0 | $oA->delete; | |||||||||||
| 192 | 0 | my $oEM = $oLI->look_down('_tag' => 'em', | |||||||||||
| 193 | class => 'yschurl'); | ||||||||||||
| 194 | 0 | ▶ | next A_TAG unless ref($oEM); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 195 | 0 | my $sEM = $oEM->as_text; | |||||||||||
| 196 | 0 | my ($sSource, $sDate) = split(/[\s\240]-[\s\240]/, $sEM); | |||||||||||
| 197 | 0 | my $oDIV = $oLI->look_down(_tag => 'div', | |||||||||||
| 198 | class => 'yschabstr'); | ||||||||||||
| 199 | 0 | ▶ | next A_TAG unless ref($oDIV); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 200 | 0 | my $sDesc = &strip_tags($oDIV->as_text); | |||||||||||
| 201 | 0 | ▶ | print STDERR " + raw DESC == $sDesc\n" if 2 <= $self->{_debug}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 202 | 0 | $sDesc =~ s!Save to My Web\Z!!; | |||||||||||
| 203 | 0 | my $hit = new WWW::Search::Result; | |||||||||||
| 204 | 0 | $hit->add_url($sURL); | |||||||||||
| 205 | 0 | $hit->title($sTitle); | |||||||||||
| 206 | 0 | $hit->description($sDesc); | |||||||||||
| 207 | 0 | $hit->change_date($sDate); | |||||||||||
| 208 | 0 | push(@{$self->{cache}}, $hit); | |||||||||||
| 209 | 0 | $self->{'_num_hits'}++; | |||||||||||
| 210 | 0 | $hits_found++; | |||||||||||
| 211 | } # foreach oFONT | ||||||||||||
| 212 | |||||||||||||
| 213 | # The "next" link is a plain old <A>: | ||||||||||||
| 214 | 0 | @aoA = $tree->look_down('_tag', 'a'); | |||||||||||
| 215 | A_TAG: | ||||||||||||
| 216 | 0 | foreach my $oA (@aoA) | |||||||||||
| 217 | { | ||||||||||||
| 218 | 0 | ▶ | printf STDERR " + A == %s\n", $oA->as_HTML if 2 <= $self->{_debug}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 219 | # <a href="http://search.news.yahoo.com/search/news?p=Japan&b=21"><b>Next 20 ></b></a> | ||||||||||||
| 220 | 0 | ▶ | if ($oA->as_text eq 'Next') | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 221 | { | ||||||||||||
| 222 | 0 | $self->{_next_url} = $HTTP::URI_CLASS->new_abs($oA->attr('href'), $self->{'_prev_url'}); | |||||||||||
| 223 | 0 | last A_TAG; | |||||||||||
| 224 | } # if | ||||||||||||
| 225 | } # foreach $oA | ||||||||||||
| 226 | 0 | $tree->delete; | |||||||||||
| 227 | 0 | return $hits_found; | |||||||||||
| 228 | } # _parse_tree | ||||||||||||
| 229 | |||||||||||||
| 230 | |||||||||||||
| 231 | 2 | 1; | |||||||||||
| 232 | |||||||||||||
| 233 | __END__ | ||||||||||||