/[Grep]/lib/Grep/Source.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /lib/Grep/Source.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 72 - (show annotations)
Fri Feb 23 09:54:28 2007 UTC (17 years, 2 months ago) by dpavlin
File size: 2562 byte(s)
another great refactoring: added new Source object which implements
searching within feed (which now can be anything as long as it produce fields
which somewhat resamble RSS feed). Source plugins implement just (site or
source format specific) fetching of items. 

Sample implementation of MoinMoin scraper, which fetch full pages from wiki
for results, so it has performance impact on remote wiki, be kind to it.
1 # Dobrica Pavlinusic, <dpavlin@rot13.org> 02/22/07 20:30:00 CET
2
3 use strict;
4 use warnings;
5
6 package Grep::Source;
7
8 use Carp qw/verbose/;
9 use Module::Pluggable search_path => 'Grep::Source', sub_name => 'sources', require => 1;
10 use base qw(Class::Accessor);
11 Grep::Source->mk_accessors( qw(feed uri q new_items collection) );
12
13 use Data::Dump qw/dump/;
14
15 =head1 NAME
16
17 Grep::Source - base class for implementation of different sources for Grep
18
19 =head1 METHODS
20
21 This is mostly documentation because most of methods are implemented by plugins.
22
23 =head2 sources
24
25 my @sources = Grep::Source->sources();
26
27 Returns all available sources.
28
29 =cut
30
31 warn "Found source plugins: ", dump( __PACKAGE__->sources() );
32
33 =head2 new
34
35 my $source = Grep::Source->new({ feed => $feed_record });
36
37 =head2 search
38
39 my $collection = $source->search( 'query string' );
40
41 It will also setup following accessors:
42
43 =over 8
44
45 =item q
46
47 Search query
48
49 =item uri
50
51 URI of feed with embedded search query
52
53 =item new_items
54
55 Number of new items in result collection
56
57 =head2 collection
58
59 Actuall results which is L<Grep::Model::ItemCollection>, so following will
60 work:
61
62 print "and ", $self->collection->count, " total items";
63
64
65 Also setups number of new items
66
67 print $source->new_items, " items new";
68
69 =cut
70
71 sub search {
72 my $self = shift;
73
74 my $q = shift;
75
76 $q ? $self->q( $q ) : $q = $self->q;
77
78 die "no q?" unless ( $self->q );
79 die "no feed?" unless ( $self->feed );
80 die "feed not Grep::Model::Feed" unless ( $self->feed->isa('Grep::Model::Feed') );
81
82 my $message;
83 my $uri = $self->feed->uri;
84 if ($uri =~ m/%s/) {
85 $uri = $self->feed->search_uri( $q );
86 $message = 'Searching';
87 } else {
88 $message = 'Fetching';
89 }
90 $message .= ' ' . $self->feed->title . " at $uri";
91
92 $self->uri( $uri );
93
94 Jifty->log->info( $message );
95
96 $self->collection( Grep::Model::ItemCollection->new() );
97
98 my $class = 'Grep::Source::Feed';
99 $class = 'Grep::Source::MoinMoin';
100 Jifty->log->debug("using $class");
101
102 $class->fetch( $self );
103
104 Grep::Search->finish if $self->new_items;
105
106 return $self->collection;
107 }
108
109 =head2 add_record
110
111 $parent->add_record( id => 42, foo => 'bar', ... );
112
113 =cut
114
115 sub add_record {
116 my $self = shift;
117
118 my $i = Grep::Model::Item->new();
119
120 my ($ok,$msg) = $i->load_or_create( @_ );
121
122 $msg ||= '';
123
124 if ( $ok ) {
125 Jifty->log->debug("item ", $i->id, ": $msg");
126 $self->collection->add_record( $i );
127
128 # is new record?
129 if ( $msg !~ m/^Found/ ) {
130 Grep::Search->add( $i );
131 $self->new_items( $self->new_items + 1 );
132 }
133 } else {
134 warn "can't add entry ", dump( @_ ), "\n";
135 }
136 }
137
138 1;

  ViewVC Help
Powered by ViewVC 1.1.26