📄 result.pm
字号:
package Yahoo::Search::Result;use strict;our $VERSION = "20070320.002"; # just to make CPAN happy=head1 NAMEYahoo::Search::Result -- class representing a single result (single webpage, image, video file, etc) from a Yahoo! search-engine query.(This package is included in, and automatically loaded by, the Yahoo::Search package.)=head1 Package UseYou never need to C<use> this package directly -- it is loadedautomatically by Yahoo::Search.=head1 Object CreationC<Result> objects are created automatically when a C<Response> object iscreated (when a C<Request> object's C<Fetch> method is called, eitherdirectly, or indirectly via a shortcut such asC<Yahoo::Search-E<gt>Query()>.=head1 Methods OverviewThis table shows the methods available on a per-search-space basis: Terms Related | Spell | | Local | | | News | | | | Video | | | | | Image | | | | | | Doc | | | | | | | | | | | | | | | Next [X] [X] [X] [X] [X] [X] [X] [X] Prev [X] [X] [X] [X] [X] [X] [X] [X] Response [X] [X] [X] [X] [X] [X] [X] [X] Request [X] [X] [X] [X] [X] [X] [X] [X] SearchSpace [X] [X] [X] [X] [X] [X] [X] [X] * I [X] [X] [X] [X] [X] [X] [X] . * i [X] [X] [X] [X] [X] [X] [X] . as_html [X] [X] [X] [X] [X] [X] [X] . as_string [X] [X] [X] [X] [X] [X] [X] . Data [X] [X] [X] [X] [X] [X] [X] . * Url [X] [X] [X] [X] [X] . . . * ClickUrl [X] [X] [X] [X] [X] . . . * Title [X] [X] [X] [X] [X] . . . TitleAsHtml [X] [X] [X] [X] [X] . . . Link [X] [X] [X] [X] [X] . . . * Summary [X] [X] [X] [X] . . . . SummaryAsHtml [X] [X] [X] [X] . . . . * CacheUrl [X] . . . . . . . * CacheSize [X] . . . . . . . * ModTimestamp [X] . . [X] . . . . * Width . [X] [X] . . . . . * Height . [X] [X] . . . . . * ThumbUrl . [X] [X] [X] . . . . * ThumbWidth . [X] [X] [X] . . . . * ThumbHeight . [X] [X] [X] . . . . ThumbImg . [X] [X] [X] . . . . ThumbLink . [X] [X] [X] . . . . * HostUrl . [X] [X] . . . . . * Copyright . [X] [X] . . . . . * Publisher . [X] [X] . . . . . * Restrictions . [X] [X] . . . . . * Type [X] [X] [X] . . . . . * Bytes . [X] [X] . . . . . * Channels . . [X] . . . . . * Seconds . . [X] . . . . . * Duration . . [X] . . . . . * Streaming . . [X] . . . . . * SourceName . . . [X] . . . . SourceNameAsHtml . . . [X] . . . . * SourceUrl . . . [X] . . . . * Language . . . [X] . . . . * PublishTime . . . [X] . . . . * PublishWhen . . . [X] . . . . * Address . . . . [X] . . . * City . . . . [X] . . . * State . . . . [X] . . . * Phone . . . . [X] . . . * Miles . . . . [X] . . . * Kilometers . . . . [X] . . . * Rating . . . . [X] . . . * MapUrl . . . . [X] . . . * BusinessUrl . . . . [X] . . . * BusinessClickUrl . . . . [X] . . . * AllMapUrl . . . . [X] . . . * Term . . . . . [X] [X] [X] TermAsHtml . . . . . [X] [X] [X] | | | | | | | | Doc | | | | | | | Image | | | | | | Video | | | | | News | | | | Local | | | Spell | | Related | TermsThose items marked with a '*' are also available via the C<Data> method=cut 'my @DOW = qw[x Sun Mon Tue Wed Thu Fri Sat];my @MON = qw[x Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec];## helper function -- returns the given text cooked for htmlsub _cook_for_html($){ my $text = shift; #die join(',', caller) if not defined $text; $text =~ s/&/&/g; $text =~ s/</</g; $text =~ s/>/>/g; return $text;}#### helper function -- given a key in a result object, a result object (the## "self" from a method), and an indication of whether we want text or## html, return the appropriate text or html.##sub _text_or_html($@){ my $Key = shift; my $Result = shift; my $AsHtml = shift; #optional my $Text = $Result->{$Key}; if (not defined $Text) { return (); } elsif ($AsHtml) { return _cook_for_html($Text); } else { return $Text; }}#### helper function -- if passed one arg, it's a url, and simply return it.#### If passed multiple args, the 2nd is an attribute (e.g. "href", "src"),## which causes the return of a string like## href="$url"## where we're sure the quoting of the url is safe.##sub _url($@){ my $Url = shift; my $Attrib = shift; if (not $Url) { return (); } elsif (not $Attrib) { return $Url; } elsif (not $Url =~ m/\x22/) { return qq/$Attrib="$Url"/; } elsif (not $Url =~ m/\x27/) { return qq/$Attrib='$Url'/; } else { $Url =~ s/\x22/%22/g; # double qoute $Url =~ s/\x27/%27/g; # single quote return qq/$Attrib="$Url"/; }}#### Want to be able to dump a hash of most data, so note which items are## available and interesting on a per-search-space basis.##my @CommonItems = qw[Url ClickUrl Summary Title i I];my %ItemsBySpace =( Video => [@CommonItems, qw"Type Bytes HostUrl Copyright Publisher Restrictions Channels Seconds Duration Streaming Width Height ThumbUrl ThumbWidth ThumbHeight"], Image => [@CommonItems, qw"Type Bytes HostUrl Copyright Publisher Restrictions Width Height ThumbUrl ThumbWidth ThumbHeight"], Doc => [@CommonItems, qw"Type CacheUrl CacheSize ModTimestamp"], Local => [@CommonItems, qw"Address City State Phone Miles Kilometers Rating MapUrl AllMapUrl"], News => [@CommonItems, qw"SourceName SourceUrl Language ModTimestamp PublishTime ThumbUrl ThumbWidth ThumbHeight"], Spell => [@CommonItems, "Term"], Related => [@CommonItems, "Term"], Terms => [@CommonItems, "Term"],);=head1 METHODS=over 4=cut##############################################################################=item $Result->Next([I<boolean>])Returns the next C<Result> object from among the list of result objectsthat are part of one C<Response> object.Returns nothing when called on the last result in a response, unlessauto-continuation is turned on, in which case the next set is automaticallyfetched and the first C<Result> from that set's C<Response> is returned.An optional defined boolean argument turns auto-continuation on (true) oroff (false). If the argument is not defined, or not provided, the value forthe original request's C<AutoContinue> option (default off) is used.Note that using auto-continuation can be dangerous. See the docs forC<NextResult> in Yahoo::Search::Response.=cutsub Next{ my $Result = shift; # self my $AutoContinue = shift; if ($Result->{_ResponseOrdinal} < $#{ $Result->{_Response}->{Result} }) { return $Result->{_Response}->{Result}->[$Result->{_ResponseOrdinal} + 1]; } else { if (not defined $AutoContinue) { $AutoContinue = $Result->{_Response}->{_Request}->{AutoContinue}; } if ($AutoContinue and my $NextResponse = $Result->{_Response}->NextSet) { return $NextResponse->NextResult(); } else { return () } }}##############################################################################=item $Result->PrevThe opposite of C<Next>. No auto-continuation feature.=cut## does not auto-fetch when fetching result[-1]sub Prev{ my $Result = shift; # self if ($Result->{_ResponseOrdinal} == 0) { return (); } else { return $Result->{_Response}->{Result}->[$Result->{_ResponseOrdinal} - 1]; }}##############################################################################=item $Result->ResponseReturns the C<Response> object of which this C<Result> object is a part.=cutsub Response{ my $Result = shift; # self return $Result->{_Response};}##############################################################################=item $Result->RequestReturns the original C<Request> object from which this C<Result> object'sC<Response> was derived.=cut 'sub Request{ my $Result = shift; # self return $Result->{_Response}->{_Request};}##############################################################################=item $Result->SearchSpaceReturns a string which indicates the search space of the original querythat this result was part of. (That is, it returns C<Doc>, C<Image>,C<News>, C<Local>, or C<Video>.)It's the same as $Result->Request->SearchSpace;=cutsub SearchSpace{ my $Result = shift; # self return $Result->{_Response}->{_Request}->{Space};}##############################################################################=item $Result->i[ I<separator> ]=item $Result->I[ I<separator> ]The first (lower-case letter "i") returns the zero-based ordinal of theresult from among those in the current C<Response>.The latter (upper-case letter "I") returns the zero-based ordinal of theresult from among all search results that might be returned by Yahoo! forthe given query.For example, after my @Results = Yahoo::Search->Results(Image => "Briteny", AppId => "my app id", Start => 45, Count => 15);the C<$Results[0]> result object has an C<I> of 45 (the 45th result of all"Briteny" image results) and an C<i> of 0 (the 0th result among thosereturned this time.)In either case, if an optional argument is given and is true, it is used asa separator every three digits. In the US, one would use $Result->I(',')to return "1,234" where $Result->I()would return "1234".=cutsub i{ my $Result = shift; # self my $Comma = shift; # optional return Yahoo::Search::Response::_commaize($Result->{_ResponseOrdinal}, $Comma);}sub I{ my $Result = shift; # self my $Comma = shift; # optional return Yahoo::Search::Response::_commaize($Result->{_ResponseOrdinal} + $Result->{_Response}->{firstResultPosition} - 1, $Comma);}##############################################################################=item $Result->as_htmlReturns a string of HTML that represents the result, as appropriate to theresult's query search space.There are many ways one may wish to display query results -- this methodreturns one display that the author finds useful. It may come in useful forquick prototyping of web applications, e.g. sub ShowRelated { print join "<hr>", map { $_->as_html } Yahoo::Search->Results(@_); }(Also see C<Yahoo::Search-E<gt>HtmlResults>)The HTML returned by C<as_html> contains class references, thereby allowingthe look-and-feel to be easily adjusted. Here's a style sheet that makesImage search results look palatable. <style> .yResult { display: block; border: #CCF 3px solid ; padding:10px } .yLink { } .yTitle { display:none } .yImg { border: solid 1px } .yUrl { display:none } .yMeta { font-size: 80% } .ySrcUrl { } .ySum { font-family: arial; font-size: 90% } </style>B<Bugs>: English-centric=cut 'sub as_html{ my $Result = shift; # self my $SearchSpace = $Result->SearchSpace; my $summary = $Result->Summary(1); if ($SearchSpace eq 'Doc') { my $link = $Result->Link; my $url = $Result->Url; my $html = "$link<span class=yUrl><br>$url</span>"; if ($summary) { $html .= "<span class=ySum><br>$summary</span>"; } return "<span class=yResult>$html</span>"; } if ($SearchSpace eq 'Video') { my $HREF = $Result->ClickUrl('HREF'); my $title = $Result->Title(1); my $html; if (my $img = $Result->ThumbImg) { $html = "<a class=yLink $HREF>$img<span class=yTitle> $title</span></a>"; } else { $html = "<a class=yLink $HREF><span class=yTitle>$title</span></a>"; } $html .= "<span class=yUrl><br>" . $Result->Url . "</span>"; my @extra; if (my $duration = $Result->Duration) { push @extra, "Duration: $duration"; } if (my $width = $Result->Width and my $height = $Result->Height) { push @extra, "Video resolution $width x $height"; } if (my $size = $Result->Bytes) { push @extra, "File size: $size"; } if (my $chan = $Result->Channels) { push @extra, "$chan-channel audio"; } if (my $HREF = $Result->HostUrl('href')) { push @extra, "<a class=ySrcUrl $HREF>Source page</a>"; } if (@extra) { $html .= "<span class=yMeta><br>" . join(" | ", @extra) . "</span>"; } if ($summary) { $html .= "<span class=ySum><br>$summary</span>"; } return "<span class=yResult>$html</span>"; } if ($SearchSpace eq 'Image') { my $HREF = $Result->ClickUrl('href'); my $title = $Result->Title(1); my $html; if (my $img = $Result->ThumbImg) { $html = "<a class=yLink $HREF>$img<span class=yTitle> $title</span></a>"; } else { $html = "<a class=yLink $HREF><span class=yTitle>$title</span></a>"; } $html .= "<span class=yUrl><br>" . $Result->Url . "</span>"; my @extra; if (my $size = $Result->Bytes) { push @extra, "File size: $size"; } if (my $width = $Result->Width and my $height = $Result->Height) { push @extra, "Image size: $width x $height";
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -