| 1 | package Google::Ranker; | ||
| 2 | |||
| 3 | use warnings; | ||
| 4 | use strict; | ||
| 5 | |||
| 6 | =head1 NAME | ||
| 7 | |||
| 8 | Google::Ranker - Find the ranking of a site/result against a search | ||
| 9 | |||
| 10 | =head1 VERSION | ||
| 11 | |||
| 12 | Version 0.02 | ||
| 13 | |||
| 14 | =cut | ||
| 15 | |||
| 16 | our $VERSION = '0.02'; | ||
| 17 | |||
| 18 | |||
| 19 | =head1 SYNOPSIS | ||
| 20 | |||
| 21 | use Google::Ranker; | ||
| 22 | |||
| 23 | my $rank = Google::Ranker->rank("search.cpan.org", { q => "perl network", key => ..., referer => ... }); | ||
| 24 | # (Make sure to get a valid key and referer from http://code.google.com/apis/ajaxsearch/signup.html first) | ||
| 25 | |||
| 26 | # Or pass in a prepared search: | ||
| 27 | |||
| 28 | my $search = Google::Search->Local(...); | ||
| 29 | my $rank = Google::Ranker->rank("example.com", $search); | ||
| 30 | |||
| 31 | # You can also rank against different criteria: | ||
| 32 | |||
| 33 | my $search = Google::Search->Video(q => "tay zonday", ...); | ||
| 34 | my $rank = Google::Ranker->rank(sub { $_[0]->titleNoFormatting =~ m/Chocolate Rain/i }, $search); | ||
| 35 | |||
| 36 | =head1 DESCRIPTION | ||
| 37 | |||
| 38 | Google::Ranker will determine the rank of a result matching some criteria within a search. The search | ||
| 39 | can be done on any of Google's search services, including web, local, news, blogs, images, videos, and books. | ||
| 40 | |||
| 41 | This connects to Google's AJAX Search API (L<http://code.google.com/apis/ajaxsearch/>) and is built upon | ||
| 42 | L<Google::Search> | ||
| 43 | |||
| 44 | =cut | ||
| 45 | |||
| 46 | use Google::Search; | ||
| 47 | use Scalar::Util qw/blessed/; | ||
| 48 | use Carp; | ||
| 49 | |||
| 50 | =head1 METHODS | ||
| 51 | |||
| 52 | =head2 Google::Rank->rank( <match>, <search> ) | ||
| 53 | |||
| 54 | Returns the numeric rank for <match> in <search> | ||
| 55 | |||
| 56 | Returns undef if <match> is not found (very possible, since the Google AJAX API only returns a limited | ||
| 57 | number of results at this time) | ||
| 58 | |||
| 59 | The first result from Google is ranked at 1 | ||
| 60 | |||
| 61 | The parameter <match> can either be a string converted into a regular expression, | ||
| 62 | a regular expression to be matched against the uri of each result, or a code reference | ||
| 63 | passed each result (in turn) as the first parameter. | ||
| 64 | |||
| 65 | The parameter <search> should be a L<Google::Search> or a hash reference to be passed to | ||
| 66 | Google::Search->new(...) | ||
| 67 | |||
| 68 | At minimum you must pass in a C<key>, a C<referer>, and a C<q> (the actual query) | ||
| 69 | |||
| 70 | =cut | ||
| 71 | |||
| 72 | sub rank { | ||
| 73 | my $class = shift; | ||
| 74 | my $matcher = shift; | ||
| 75 | my $search = shift; | ||
| 76 | |||
| 77 | if (defined $search && ref $search eq "") { | ||
| 78 | warn "\n# ", __FILE__, ":", __LINE__, "\n", <<_END_; | ||
| 79 | # Running a search without a valid API key/referer | ||
| 80 | # Pass in a key => ... and referer => ... to disable this warning | ||
| 81 | # You can get both at http://code.google.com/apis/ajaxsearch/signup.html | ||
| 82 | |||
| 83 | _END_ | ||
| 84 | $search = { q => $search }; | ||
| 85 | } | ||
| 86 | if (ref $search eq "HASH") { | ||
| 87 | $search = Google::Search->Web(%$search); | ||
| 88 | } | ||
| 89 | |||
| 90 | croak "Don't have a search to rank with" unless $search; | ||
| 91 | croak "Don't understand search \"$search\"" unless blessed $search && $search->isa("Google::Search"); | ||
| 92 | croak "Don't have a matcher to find ranking position with" unless $matcher; | ||
| 93 | |||
| 94 | $matcher = qr/$matcher/ if ref $matcher eq ""; | ||
| 95 | if (ref $matcher eq "Regexp") { | ||
| 96 | my $re = $matcher; | ||
| 97 | $matcher = sub { | ||
| 98 | return $_[0]->uri->as_string =~ $re; | ||
| 99 | }; | ||
| 100 | } | ||
| 101 | unless (ref $matcher eq "CODE") { | ||
| 102 | croak "Don't understand matcher \"$matcher\""; | ||
| 103 | } | ||
| 104 | |||
| 105 | my $result = $search->first_match($matcher); | ||
| 106 | |||
| 107 | return undef unless defined $result; | ||
| 108 | |||
| 109 | return $result->number + 1; | ||
| 110 | } | ||
| 111 | |||
| 112 | =head1 AUTHOR | ||
| 113 | |||
| 114 | Robert Krimen, C<< <rkrimen at cpan.org> >> | ||
| 115 | |||
| 116 | =head1 SEE ALSO | ||
| 117 | |||
| 118 | L<Google::Search> | ||
| 119 | |||
| 120 | =head1 BUGS | ||
| 121 | |||
| 122 | Please report any bugs or feature requests to C<bug-google-ranker at rt.cpan.org>, or through | ||
| 123 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Google-Ranker>. I will be notified, and then you'll | ||
| 124 | automatically be notified of progress on your bug as I make changes. | ||
| 125 | |||
| 126 | |||
| 127 | |||
| 128 | |||
| 129 | =head1 SUPPORT | ||
| 130 | |||
| 131 | You can find documentation for this module with the perldoc command. | ||
| 132 | |||
| 133 | perldoc Google::Ranker | ||
| 134 | |||
| 135 | |||
| 136 | You can also look for information at: | ||
| 137 | |||
| 138 | =over 4 | ||
| 139 | |||
| 140 | =item * RT: CPAN's request tracker | ||
| 141 | |||
| 142 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Google-Ranker> | ||
| 143 | |||
| 144 | =item * AnnoCPAN: Annotated CPAN documentation | ||
| 145 | |||
| 146 | L<http://annocpan.org/dist/Google-Ranker> | ||
| 147 | |||
| 148 | =item * CPAN Ratings | ||
| 149 | |||
| 150 | L<http://cpanratings.perl.org/d/Google-Ranker> | ||
| 151 | |||
| 152 | =item * Search CPAN | ||
| 153 | |||
| 154 | L<http://search.cpan.org/dist/Google-Ranker> | ||
| 155 | |||
| 156 | =back | ||
| 157 | |||
| 158 | |||
| 159 | =head1 ACKNOWLEDGEMENTS | ||
| 160 | |||
| 161 | |||
| 162 | =head1 COPYRIGHT & LICENSE | ||
| 163 | |||
| 164 | Copyright 2008 Robert Krimen, all rights reserved. | ||
| 165 | |||
| 166 | This program is free software; you can redistribute it and/or modify it | ||
| 167 | under the same terms as Perl itself. | ||
| 168 | |||
| 169 | |||
| 170 | =cut | ||
| 171 | |||
| 172 | 1; # End of Google::Ranker |