/[Grep]/lib/Grep/Action/Fetch.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/Action/Fetch.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 46 - (show annotations)
Tue Feb 20 22:44:59 2007 UTC (17 years, 2 months ago) by dpavlin
File size: 3596 byte(s)
understand messages from load_or_create to count new entries (and produce
nice sentences about results)
1 use strict;
2 use warnings;
3
4 =head1 NAME
5
6 Grep::Action::Fetch
7
8 =cut
9
10 package Grep::Action::Fetch;
11 use base qw/Grep::Action Jifty::Action/;
12
13 use XML::Feed;
14 use LWP::UserAgent;
15
16 use Data::Dump qw/dump/;
17
18 use Jifty::Param::Schema;
19 use Jifty::Action schema {
20
21 param q =>
22 type is 'text',
23 label is 'Search for',
24 hint is 'enter few words to search for';
25
26 param feed =>
27 label is 'From feed',
28 render as 'select',
29 available are defer {
30 my $feeds = Grep::Model::FeedCollection->new;
31 $feeds->order_by({ column => 'title', order => 'ASC' });
32 $feeds->unlimit;
33 warn "feeds ", $feeds->build_select_query;
34 [{
35 display_from => 'title',
36 value_from => 'id',
37 collection => $feeds,
38 }];
39 };
40
41 param item_fragment =>
42 label is 'Show',
43 render as 'select',
44 # valid are qw/result result_short/,
45 # available are qw/result result_short/;
46 available are qw/long short title/;
47
48 };
49
50 =head2 take_action
51
52 Returns C<Grep::Model::ItemCollection> of fatched items from Feed which will
53 also be stored in local cache.
54
55 =cut
56
57 sub take_action {
58 my $self = shift;
59
60 # Custom action code
61
62 my $feed = Grep::Model::Feed->new();
63 my $feed_id = $self->argument_value('feed');
64 my $q = $self->argument_value('q');
65 my $user_id = Jifty->web->current_user->id;
66
67 if (! $feed_id) {
68 $self->result->message("Need feed ID");
69 return 0;
70 }
71
72 $feed->load_by_cols( id => $feed_id );
73
74 if (! $feed->id) {
75 $self->result->message("Can't fetch feed $feed_id");
76 return 0;
77 }
78
79 my $message;
80 my $uri = $feed->uri;
81 if ($uri =~ m/%s/) {
82 $uri = $feed->search_uri( $q );
83 $message = 'Searching';
84 } else {
85 $message = 'Fetching';
86 }
87 $message .= ' ' . $feed->title . " at $uri";
88
89 Jifty->log->info( $message );
90
91 my $items = Grep::Model::ItemCollection->new();
92
93 my $ua = LWP::UserAgent->new;
94 $ua->default_header( 'Cookie' => $feed->cookie );
95 my $r = $ua->get( $uri );
96 return $self->result->error(
97 $feed->title . " returned " . $r->status_line . " for $uri\n"
98 ) unless ( $r->is_success );
99
100 my $content = $r->content;
101
102 return $self->result->error( "No content returned from $uri" ) unless length( $content ) > 1;
103
104
105 my $xml_feed = XML::Feed->parse( \$content )
106 or return $self->result->error( $feed->title, " returned ", XML::Feed->errstr, " for $uri" );
107
108 warn "getting entries from ", $xml_feed->title, "\n";
109
110 my $new;
111
112 for my $entry ($xml_feed->entries) {
113 my $i = Grep::Model::Item->new();
114
115 my ($ok,$msg) = $i->load_or_create(
116 in_feed => $feed,
117 title => $entry->title,
118 link => $entry->link,
119 content => $entry->content->body,
120 summary => $entry->summary->body,
121 category => $entry->category,
122 author => $entry->author,
123 issued => $entry->issued ? $entry->issued->strftime("%Y-%m-%d %H:%M:%S") : undef,
124 modified => $entry->modified ? $entry->modified->strftime("%Y-%m-%d %H:%M:%S") : undef,
125 );
126
127 if ( $ok ) {
128 Jifty->log->debug("item ", $i->id, ": $msg");
129 $items->add_record( $i );
130 # count new objects
131 $new++ if ($msg !~ m/^Found/);
132 } else {
133 warn "can't add entry ", dump( $entry ), "\n";
134 }
135
136 }
137
138 if ( my $count = $items->count ) {
139
140 # construct a proper sentence :-)
141 my $message = $count
142 . ( $new == $count ? ' new' : '' )
143 . ( $new == 0 ? ' old' : '' )
144 . ' results '
145 . ( $new && $new < $count ? "of which $new new " : '' )
146 . "for '$q' in " . $feed->title;
147
148 $self->result->message( $message );
149
150 $self->result->content( items => $items );
151 $self->result->content( count => $items->count );
152
153 } else {
154 return $self->result->error( "No results for '$q' in " . $feed->title );
155 }
156
157 return $items;
158 }
159
160 1;
161

  ViewVC Help
Powered by ViewVC 1.1.26