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

  ViewVC Help
Powered by ViewVC 1.1.26