/[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

Annotation of /lib/Grep/Action/Fetch.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 21 - (hide annotations)
Sun Feb 18 15:07:03 2007 UTC (17 years, 3 months ago) by dpavlin
File size: 2814 byte(s)
Bookmarklet is now designed to work on html results page (to capture cookies so that
Grep will later be able to fetch feeds with user credentials creating single sign-on
scenario :-), and it will automatically (using new requirement Feed::Find) find feed
on that page.

For that to work, new action AddFeed was added.
1 dpavlin 3 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 dpavlin 21 use LWP::UserAgent;
15 dpavlin 3
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 dpavlin 7 render as 'select',
29 dpavlin 3 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     };
42    
43     =head2 take_action
44    
45     =cut
46    
47     sub take_action {
48     my $self = shift;
49    
50     # Custom action code
51    
52     my $feed = Grep::Model::Feed->new();
53     my $feed_id = $self->argument_value('feed');
54    
55     if (! $feed_id) {
56     $self->result->message("Need feed ID");
57     return 0;
58     }
59    
60     $feed->load_by_cols( id => $feed_id );
61    
62     if (! $feed->id) {
63     $self->result->message("Can't fetch feed $feed_id");
64     return 0;
65     }
66    
67 dpavlin 7 my $uri = $feed->uri;
68     if ($uri =~ m/%s/) {
69 dpavlin 19 $uri = $feed->search_uri( $self->argument_value('q') );
70 dpavlin 7 Jifty->log->info("Searching ", $feed->title, " at $uri");
71     } else {
72     Jifty->log->info("Fetching ", $feed->title, " at $uri");
73     }
74 dpavlin 3
75 dpavlin 21 my $ua = LWP::UserAgent->new;
76     $ua->default_header( 'Cookie' => $feed->cookie );
77     my $r = $ua->get( $uri );
78     die "Can't fetch $uri: " . $r->status_line unless ( $r->is_success );
79    
80     my $content = $r->content;
81    
82     die "No content returned from $uri" unless length( $content ) > 1;
83    
84     my $xml_feed = XML::Feed->parse( \$content )
85 dpavlin 19 or die $feed->title, " returned ", XML::Feed->errstr, " for $uri\n";
86 dpavlin 3
87 dpavlin 21 warn "getting entries from ", $xml_feed->title, "\n";
88 dpavlin 7
89 dpavlin 9 my @items;
90 dpavlin 7
91 dpavlin 3 for my $entry ($xml_feed->entries) {
92     my $i = Grep::Model::Item->new();
93    
94     $i->load_or_create(
95     in_feed => $feed,
96     title => $entry->title,
97     link => $entry->link,
98 dpavlin 5 content => $entry->content->body,
99     summary => $entry->summary->body,
100 dpavlin 3 category => $entry->category,
101     author => $entry->author,
102     issued => $entry->issued ? $entry->issued->strftime("%Y-%m-%d %H:%M:%S %z") : undef,
103     modified => $entry->modified ? $entry->modified->strftime("%Y-%m-%d %H:%M:%S %z") : undef,
104     );
105 dpavlin 5
106 dpavlin 7 die "can't create item from entry ", dump( $entry ) unless ( $i->id );
107    
108 dpavlin 9 push @items, $i;
109 dpavlin 7
110 dpavlin 9 # Grep::Event::Result->new( $i )->publish;
111    
112     Jifty->log->debug("published ", $i->id ) ; # dump( $entry, $i ) );
113 dpavlin 3 }
114    
115 dpavlin 9 if ( @items ) {
116 dpavlin 7
117     $self->result->message( $self->argument_value('q') . ' => ' .
118 dpavlin 9 $xml_feed->entries . ' items: ' . join(",", map { $_->id } @items)
119 dpavlin 7 );
120    
121 dpavlin 9 $self->result->content( items => \@items );
122 dpavlin 7 return 1;
123    
124     } else {
125    
126 dpavlin 21 $self->result->error( 'No results found' );
127     return 0;
128 dpavlin 8
129 dpavlin 7 }
130 dpavlin 3 }
131    
132     1;
133    

  ViewVC Help
Powered by ViewVC 1.1.26