E2::Search - A module for searching for nodes on L<http://everything2.com>


E2-Interface documentation Contained in the E2-Interface distribution.

Index


Code Index:

NAME

Top

E2::Search - A module for searching for nodes on http://everything2.com

SYNOPSIS

Top

	use E2::Search;

	my $search = new E2::Search;

	# Fetch 10 results for a keyword search on "William Shatner"

	my @results = $search->search( "William Shatner", 'e2node', 10 );

	foreach my $r ( @results ) {
		print $r->{title};
	}

DESCRIPTION

Top

This module provides an interface to everything2.com's search interface. It inherits E2::Ticker.

CONSTRUCTOR

Top

new [ USERNAME ]

new creates an E2::Search object.

METHODS

Top

$search->search KEYWORDS [, NODETYPE ] [, MAX_RESULTS ]

search performs a title search and returns a list of hashrefs to the titles found (with "title" and "node_id" as keys to each hash). NODETYPE is the type of node intended ("e2node" is default; other possibilities include "user", "group", "room", "document", "superdoc", and possible others). MAX_RESULTS (if set) is the maximum number of results to return.

SEE ALSO

Top

E2::Interface, E2::Ticker, E2::UserSearch, http://everything2.com, http://everything2.com/?node=clientdev

AUTHOR

Top

Jose M. Weeks <jose@joseweeks.com> (Simpleton on E2)

COPYRIGHT

Top


E2-Interface documentation Contained in the E2-Interface distribution.

# E2::Search
# Jose M. Weeks <jose@joseweeks.com>
# 05 June 2003
#
# See bottom for pod documentation.

package E2::Search;

use 5.006;
use strict;
use warnings;
use Carp;

use E2::Ticker;

our $VERSION = "0.32";
our @ISA = qw(E2::Ticker);
our $DEBUG; *DEBUG = *E2::Interface::DEBUG;

sub new { 
	my $arg   = shift;
	my $class = ref( $arg ) || $arg;
	my $self  = $class->SUPER::new();

	bless ($self, $class);

	return $self;
}

sub search {
	my $self = shift or croak "Usage: search E2SEARCH, KEYWORDS [, NODETYPE ] [, MAX_RESULTS ]";
	my $keywords = shift or croak "Usage: search E2SEARCH, KEYWORDS [, NODETYPE ] [, MAX_RESULTS ]";
	my $nodetype = shift || 'e2node';
	my $max_results = shift;

	my @results;

	warn "E2::Search::search\n"	if $DEBUG > 1;

	my %opt = (
		keywords => $keywords,
		nodetype => $nodetype
	);
	
	my $handlers = {
		'searchinfo/keywords' => sub {
			(my $a, my $b) = @_;
			$self->{keywords} = $b->text;
		},
		'searchinfo/search_nodetype' => sub {
			(my $a, my $b) = @_;
			$self->{searchtype} = $b->text;
		},
		'searchresults/e2link' => sub {
			(my $a, my $b) = @_;
			if( !$self->{max_results} || 
			    !$self->{results} || 
			    $self->{max_results} > @results ) {

				push @results, {
					title => $b->text, 
					node_id =>$b->{att}->{node_id}
				};
			}
		}
	};


	$self->{keywords} = undef;
	$self->{searchtype} = undef;
	
	return $self->parse( 'search', $handlers, \@results, %opt );
}

1;
__END__