/[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 19 - (show annotations)
Sun Feb 18 12:51:26 2007 UTC (17 years, 2 months ago) by dpavlin
File size: 2603 byte(s)
added cookie and search_uri to feed model
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 = $feed->search_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 $feed->title, " returned ", XML::Feed->errstr, " for $uri\n";
76
77 warn "fetching ", $xml_feed->title, "\n";
78
79 my @items;
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 @items, $i;
99
100 # Grep::Event::Result->new( $i )->publish;
101
102 Jifty->log->debug("published ", $i->id ) ; # dump( $entry, $i ) );
103 }
104
105 if ( @items ) {
106
107 $self->result->message( $self->argument_value('q') . ' => ' .
108 $xml_feed->entries . ' items: ' . join(",", map { $_->id } @items)
109 );
110
111 $self->result->content( items => \@items );
112 return 1;
113
114 } else {
115
116 $self->result->message( 'No results found' );
117
118 # with default sticky_on_failure, this will keep form data
119 $self->result->failure( 1 );
120
121 return 0;
122 }
123 }
124
125 1;
126

  ViewVC Help
Powered by ViewVC 1.1.26