1 |
dpavlin |
21 |
use strict; |
2 |
|
|
use warnings; |
3 |
|
|
|
4 |
|
|
=head1 NAME |
5 |
|
|
|
6 |
|
|
Grep::Action::AddFeed |
7 |
|
|
|
8 |
|
|
=cut |
9 |
|
|
|
10 |
|
|
package Grep::Action::AddFeed; |
11 |
|
|
use base qw/Grep::Action::CreateFeed/; |
12 |
|
|
|
13 |
|
|
use Feed::Find; |
14 |
|
|
use LWP::UserAgent; |
15 |
|
|
use Data::Dump qw/dump/; |
16 |
|
|
|
17 |
|
|
=head2 canonicalize_uri |
18 |
|
|
|
19 |
|
|
Replace C<grep>' with C<%s> in URI arguments |
20 |
|
|
|
21 |
|
|
=cut |
22 |
|
|
|
23 |
|
|
sub canonicalize_uri { |
24 |
|
|
my $self = shift; |
25 |
|
|
my $value = shift; |
26 |
|
|
warn "uri: $value"; |
27 |
dpavlin |
26 |
if ($value =~ s/\bgrep\b/%s/) { |
28 |
dpavlin |
21 |
$self->canonicalization_note( uri => 'Replaced grep with %s' ); |
29 |
|
|
} |
30 |
|
|
return $value; |
31 |
|
|
} |
32 |
|
|
|
33 |
dpavlin |
26 |
=head2 canonicalize_cookie |
34 |
|
|
|
35 |
|
|
Remove C<Cookie:> header from beginning and replace EOL with space. |
36 |
|
|
|
37 |
|
|
=cut |
38 |
|
|
|
39 |
|
|
# disabled for now |
40 |
|
|
sub xx_canonicalize_cookie { |
41 |
|
|
my $self = shift; |
42 |
|
|
my $value = shift; |
43 |
|
|
|
44 |
|
|
warn "cookie: $value"; |
45 |
|
|
|
46 |
|
|
$self->canonicalization_note( uri => 'Removed Cookie: header' ) |
47 |
|
|
if ($value =~ s/^Cookie:\s+//); |
48 |
|
|
|
49 |
|
|
$self->canonicalization_note( uri => 'Converted EOL to space' ) |
50 |
|
|
if ($value =~ s/[\n\r]/ /gs); |
51 |
|
|
|
52 |
|
|
return $value; |
53 |
|
|
} |
54 |
dpavlin |
21 |
=head2 take_action |
55 |
|
|
|
56 |
|
|
=cut |
57 |
|
|
|
58 |
|
|
sub take_action { |
59 |
|
|
my $self = shift; |
60 |
|
|
|
61 |
|
|
my @ARGS = @_; |
62 |
|
|
|
63 |
|
|
# Custom action code |
64 |
|
|
|
65 |
|
|
my $ua = LWP::UserAgent->new; |
66 |
|
|
|
67 |
|
|
my $cookie = $self->argument_value('cookie'); |
68 |
|
|
if ($cookie =~ s/{x!(26|3b)}/chr(hex($1))/gei) { |
69 |
|
|
$self->argument_value('cookie', $cookie); |
70 |
|
|
} |
71 |
|
|
|
72 |
|
|
Jifty->log->debug("using cookie: $cookie"); |
73 |
|
|
$ua->default_header( 'Cookie' => $cookie ); |
74 |
|
|
|
75 |
|
|
my $search_moniker = 'grep'; |
76 |
|
|
|
77 |
|
|
my $uri = $self->argument_value('uri'); |
78 |
|
|
$uri =~ s/{x!(26|3b)}/chr(hex($1))/gei; |
79 |
|
|
|
80 |
|
|
Jifty->log->debug("trying to find feed on $uri"); |
81 |
|
|
|
82 |
dpavlin |
69 |
my $r = $ua->get( sprintf( $uri, $search_moniker ) ); |
83 |
dpavlin |
21 |
|
84 |
dpavlin |
69 |
return $self->result->error( $r->status_line . " from $uri" ) unless ( $r->is_success ); |
85 |
|
|
|
86 |
|
|
if ($r->header('Content-type') =~ /xml/) { |
87 |
|
|
$self->result->message( "Assuming $uri is feed and using it" ); |
88 |
|
|
return $self->SUPER::take_action( @ARGS ); |
89 |
|
|
} |
90 |
|
|
|
91 |
|
|
my @feeds = Feed::Find->find_in_html( $r->content ); |
92 |
|
|
|
93 |
dpavlin |
21 |
if (@feeds) { |
94 |
|
|
|
95 |
|
|
Jifty->log->info("found possible feeds: ", dump( @feeds )); |
96 |
|
|
|
97 |
|
|
@feeds = map { |
98 |
|
|
my $t = $_; |
99 |
dpavlin |
26 |
$t =~ s/\b$search_moniker\b/%s/; |
100 |
dpavlin |
21 |
$t |
101 |
dpavlin |
26 |
} grep(/\b$search_moniker\b/,@feeds); |
102 |
dpavlin |
21 |
|
103 |
|
|
|
104 |
dpavlin |
58 |
my $feed_uri = shift @feeds; |
105 |
dpavlin |
21 |
|
106 |
dpavlin |
58 |
return $self->result->error("Can't find any feed at $uri") unless ( $feed_uri ); |
107 |
dpavlin |
21 |
|
108 |
dpavlin |
69 |
$self->result->message('Found ' . @feeds . " feeds, using first: $feed_uri" ); |
109 |
dpavlin |
58 |
$self->argument_value('uri', $feed_uri); |
110 |
dpavlin |
21 |
|
111 |
dpavlin |
58 |
Jifty->log->debug("calling parent take_action with new uri $feed_uri"); |
112 |
dpavlin |
21 |
|
113 |
dpavlin |
58 |
return $self->SUPER::take_action( @ARGS ); |
114 |
dpavlin |
21 |
|
115 |
|
|
} else { |
116 |
|
|
|
117 |
|
|
warn "no feeds in ", $r->content; |
118 |
|
|
|
119 |
|
|
$self->result->error('No feeds found on supplied URI'); |
120 |
|
|
return 0; |
121 |
|
|
} |
122 |
|
|
|
123 |
|
|
} |
124 |
|
|
|
125 |
|
|
1; |
126 |
|
|
|