1 package WWW::OpenSearch;
6 use base qw( Class::Accessor::Fast );
9 use WWW::OpenSearch::Response;
10 use WWW::OpenSearch::Description;
11 use Encode qw( _utf8_off );
13 __PACKAGE__->mk_accessors( qw( description_url agent description ) );
15 our $VERSION = '0.08';
19 WWW::OpenSearch - Search A9 OpenSearch compatible engines
25 my $url = "http://bulkfeeds.net/opensearch.xml";
26 my $engine = WWW::OpenSearch->new($url);
28 my $name = $engine->description->ShortName;
29 my $tags = $engine->description->Tags;
31 # Perform search for "iPod"
32 my $response = $engine->search("iPod");
33 for my $item (@{$response->feed->items}) {
34 print $item->{description};
37 # Retrieve the next page of results
38 my $next_page = $response->next_page;
39 for my $item (@{$next_page->feed->items}) {
40 print $item->{description};
45 WWW::OpenSearch is a module to search A9's OpenSearch compatible search engines. See http://opensearch.a9.com/ for details.
49 =head2 new( $url [, $useragent] )
51 Constructs a new instance of WWW::OpenSearch using the given
52 URL as the location of the engine's OpenSearch Description
53 document (retrievable via the description_url accessor). Pass any
54 LWP::UserAgent compatible object if you wish to override the default
59 =head2 fetch_description( [ $url ] )
61 Fetches the OpenSearch Descsription found either at the given URL
62 or at the URL specified by the description_url accessor. Fetched
63 description may be accessed via the description accessor.
65 =head2 search( $query [, \%params] )
67 Searches the engine for the given query using the given
68 search parameters. Valid search parameters include:
82 See http://opensearch.a9.com/spec/1.1/response/#elements for details.
84 =head2 do_search( $url [, $method] )
86 Performs a request for the given URL and returns a
87 WWW::OpenSearch::Response object. Method defaults to 'GET'.
91 =head2 description_url( [$description_url] )
93 =head2 agent( [$agent] )
95 =head2 description( [$description] )
101 =item * Tatsuhiko Miyagawa E<lt>miyagawa@bulknews.netE<gt>
103 =item * Brian Cassidy E<lt>bricas@cpan.orgE<gt>
107 =head1 COPYRIGHT AND LICENSE
109 Copyright 2006 by Tatsuhiko Miyagawa and Brian Cassidy
111 This library is free software; you can redistribute it and/or modify
112 it under the same terms as Perl itself.
117 my( $class, $url, $agent ) = @_;
119 croak( "No OpenSearch Description url provided" ) unless $url;
121 my $self = $class->SUPER::new;
124 require LWP::UserAgent;
125 $agent = LWP::UserAgent->new( agent => join( '/', ref $self, $VERSION ) );
128 $self->description_url( $url );
129 $self->agent( $agent );
131 $self->fetch_description;
136 sub fetch_description {
137 my( $self, $url ) = @_;
138 $url ||= $self->description_url;
139 $self->description_url( $url );
140 my $response = $self->agent->get( $url );
142 unless( $response->is_success ) {
143 croak "Error while fetching $url: " . $response->status_line;
146 $self->description( WWW::OpenSearch::Description->new( $response->content ) );
150 my( $self, $query, $params ) = @_;
153 $params->{ searchTerms } = $query;
154 _utf8_off( $params->{ searchTerms } );
156 my $url = $self->description->get_best_url;
157 return $self->do_search( $url->prepare_query( $params ), $url->method );
161 my( $self, $url, $method ) = @_;
163 $method = lc( $method ) || 'get';
166 if( $method eq 'post' ) {
167 $response = $self->agent->post( @$url );
170 $response = $self->agent->$method( $url );
173 return WWW::OpenSearch::Response->new( $self, $response );