/[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 25 - (show annotations)
Sun Feb 18 20:45:59 2007 UTC (17 years, 3 months ago) by dpavlin
File size: 3019 byte(s)
implement item_fragment selection in UI, better error message,
warn on empty ATOM feeds instead of die
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
47 };
48
49 =head2 take_action
50
51 =cut
52
53 sub take_action {
54 my $self = shift;
55
56 # Custom action code
57
58 my $feed = Grep::Model::Feed->new();
59 my $feed_id = $self->argument_value('feed');
60
61 if (! $feed_id) {
62 $self->result->message("Need feed ID");
63 return 0;
64 }
65
66 $feed->load_by_cols( id => $feed_id );
67
68 if (! $feed->id) {
69 $self->result->message("Can't fetch feed $feed_id");
70 return 0;
71 }
72
73 my $uri = $feed->uri;
74 if ($uri =~ m/%s/) {
75 $uri = $feed->search_uri( $self->argument_value('q') );
76 Jifty->log->info("Searching ", $feed->title, " at $uri");
77 } else {
78 Jifty->log->info("Fetching ", $feed->title, " at $uri");
79 }
80
81 my $ua = LWP::UserAgent->new;
82 $ua->default_header( 'Cookie' => $feed->cookie );
83 my $r = $ua->get( $uri );
84 die $feed->title . " returned " . $r->status_line . " for $uri\n" unless ( $r->is_success );
85
86 my $content = $r->content;
87
88 die "No content returned from $uri" unless length( $content ) > 1;
89
90 my $xml_feed = XML::Feed->parse( \$content )
91 or die $feed->title, " returned ", XML::Feed->errstr, " for $uri\n";
92
93 warn "getting entries from ", $xml_feed->title, "\n";
94
95 my @items;
96
97 for my $entry ($xml_feed->entries) {
98 my $i = Grep::Model::Item->new();
99
100 $i->load_or_create(
101 in_feed => $feed,
102 title => $entry->title,
103 link => $entry->link,
104 content => $entry->content->body,
105 summary => $entry->summary->body,
106 category => $entry->category,
107 author => $entry->author,
108 issued => $entry->issued ? $entry->issued->strftime("%Y-%m-%d %H:%M:%S %z") : undef,
109 modified => $entry->modified ? $entry->modified->strftime("%Y-%m-%d %H:%M:%S %z") : undef,
110 );
111
112 if ( $i->id ) {
113 push @items, $i;
114
115 # Grep::Event::Result->new( $i )->publish;
116
117 Jifty->log->debug("published ", $i->id ) ; # dump( $entry, $i ) );
118 } else {
119 warn "can't create item from entry ", dump( $entry ) unless ( $i->id );
120 }
121
122 }
123
124 if ( @items ) {
125
126 $self->result->message( $self->argument_value('q') . ' => ' .
127 $xml_feed->entries . ' items: ' . join(",", map { $_->id } @items)
128 );
129
130 $self->result->content( items => \@items );
131 return 1;
132
133 } else {
134
135 $self->result->error( 'No results found' );
136 return 0;
137
138 }
139 }
140
141 1;
142

  ViewVC Help
Powered by ViewVC 1.1.26