/[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 8 - (show annotations)
Sat Feb 17 21:24:48 2007 UTC (17 years, 2 months ago) by dpavlin
File size: 2504 byte(s)
with no results report failure which will keep entered data in form. nice :-)
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
15 use Data::Dump qw/dump/;
16
17 use Jifty::Param::Schema;
18 use Jifty::Action schema {
19
20 param q =>
21 type is 'text',
22 label is 'Search for',
23 hint is 'enter few words to search for';
24
25 param feed =>
26 label is 'From feed',
27 render as 'select',
28 available are defer {
29 my $feeds = Grep::Model::FeedCollection->new;
30 $feeds->order_by({ column => 'title', order => 'ASC' });
31 $feeds->unlimit;
32 warn "feeds ", $feeds->build_select_query;
33 [{
34 display_from => 'title',
35 value_from => 'id',
36 collection => $feeds,
37 }];
38 };
39
40 };
41
42 =head2 take_action
43
44 =cut
45
46 sub take_action {
47 my $self = shift;
48
49 # Custom action code
50
51 my $feed = Grep::Model::Feed->new();
52 my $feed_id = $self->argument_value('feed');
53
54 if (! $feed_id) {
55 $self->result->message("Need feed ID");
56 return 0;
57 }
58
59 $feed->load_by_cols( id => $feed_id );
60
61 if (! $feed->id) {
62 $self->result->message("Can't fetch feed $feed_id");
63 return 0;
64 }
65
66 my $uri = $feed->uri;
67 if ($uri =~ m/%s/) {
68 $uri = sprintf( $uri, $self->argument_value('q') );
69 Jifty->log->info("Searching ", $feed->title, " at $uri");
70 } else {
71 Jifty->log->info("Fetching ", $feed->title, " at $uri");
72 }
73
74 my $xml_feed = XML::Feed->parse( URI->new($uri) )
75 or die XML::Feed->errstr;
76
77 warn "fetching ", $xml_feed->title, "\n";
78
79 my @results;
80
81 for my $entry ($xml_feed->entries) {
82 my $i = Grep::Model::Item->new();
83
84 $i->load_or_create(
85 in_feed => $feed,
86 title => $entry->title,
87 link => $entry->link,
88 content => $entry->content->body,
89 summary => $entry->summary->body,
90 category => $entry->category,
91 author => $entry->author,
92 issued => $entry->issued ? $entry->issued->strftime("%Y-%m-%d %H:%M:%S %z") : undef,
93 modified => $entry->modified ? $entry->modified->strftime("%Y-%m-%d %H:%M:%S %z") : undef,
94 );
95
96 die "can't create item from entry ", dump( $entry ) unless ( $i->id );
97
98 push @results, $i->id;
99
100 Jifty->log->debug("item ", $i->id, " = ",dump( $entry ) );
101 }
102
103 if ( @results ) {
104
105 $self->result->message( $self->argument_value('q') . ' => ' .
106 $xml_feed->entries . ' items: ' . join(",", @results)
107 );
108
109 $self->result->content( results => \@results );
110 return 1;
111
112 } else {
113
114 $self->result->message( 'No results found' );
115
116 # with default sticky_on_failure, this will keep form data
117 $self->result->failure( 1 );
118
119 return 0;
120 }
121 }
122
123 1;
124

  ViewVC Help
Powered by ViewVC 1.1.26