| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Kwiki::FetchRSS; |
|
2
|
1
|
|
|
1
|
|
34409
|
use Kwiki::Plugin '-Base'; |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
use Kwiki::Installer '-base'; |
|
4
|
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
our $VERSION = '0.08'; |
|
6
|
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
const class_id => 'fetchrss'; |
|
8
|
|
|
|
|
|
|
const class_title => 'Fetch RSS'; |
|
9
|
|
|
|
|
|
|
const config_file => 'fetchrss.yaml'; |
|
10
|
|
|
|
|
|
|
const css_file => 'fetchrss.css'; |
|
11
|
|
|
|
|
|
|
field 'cache'; |
|
12
|
|
|
|
|
|
|
field 'error'; |
|
13
|
|
|
|
|
|
|
field 'expire'; |
|
14
|
|
|
|
|
|
|
field 'timeout' => -init => '$self->hub->config->fetchrss_ua_timeout'; |
|
15
|
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
sub register { |
|
17
|
|
|
|
|
|
|
my $registry = shift; |
|
18
|
|
|
|
|
|
|
$registry->add( wafl => fetchrss => 'Kwiki::FetchRSS::Wafl' ); |
|
19
|
|
|
|
|
|
|
} |
|
20
|
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
sub cache_dir { |
|
22
|
|
|
|
|
|
|
$self->plugin_directory; |
|
23
|
|
|
|
|
|
|
} |
|
24
|
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
sub get_content { |
|
26
|
|
|
|
|
|
|
my $url = shift; |
|
27
|
|
|
|
|
|
|
my $content; |
|
28
|
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
require LWP::UserAgent; |
|
30
|
|
|
|
|
|
|
my $ua = LWP::UserAgent->new(); |
|
31
|
|
|
|
|
|
|
$ua->timeout($self->timeout); |
|
32
|
|
|
|
|
|
|
if (defined($self->hub->config->fetchrss_proxy) and |
|
33
|
|
|
|
|
|
|
$self->hub->config->fetchrss_proxy ne '' ) { |
|
34
|
|
|
|
|
|
|
$ua->proxy([ 'http' ], $self->hub->config->fetchrss_proxy); |
|
35
|
|
|
|
|
|
|
} |
|
36
|
|
|
|
|
|
|
my $response = $ua->get($url); |
|
37
|
|
|
|
|
|
|
if ($response->is_success()) { |
|
38
|
|
|
|
|
|
|
$content = $response->content(); |
|
39
|
|
|
|
|
|
|
if (length($content)) { |
|
40
|
|
|
|
|
|
|
$self->cache->set( $url, $content, $self->expire ); |
|
41
|
|
|
|
|
|
|
} else { |
|
42
|
|
|
|
|
|
|
$self->error('zero length response'); |
|
43
|
|
|
|
|
|
|
} |
|
44
|
|
|
|
|
|
|
} else { |
|
45
|
|
|
|
|
|
|
$self->error($response->status_line); |
|
46
|
|
|
|
|
|
|
} |
|
47
|
|
|
|
|
|
|
return $content; |
|
48
|
|
|
|
|
|
|
} |
|
49
|
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
sub setup_cache { |
|
51
|
|
|
|
|
|
|
require Cache::FileCache; |
|
52
|
|
|
|
|
|
|
$self->cache(Cache::FileCache->new( { |
|
53
|
|
|
|
|
|
|
namespace => $self->class_id, |
|
54
|
|
|
|
|
|
|
cache_root => $self->cache_dir, |
|
55
|
|
|
|
|
|
|
cache_depth => 1, |
|
56
|
|
|
|
|
|
|
cache_umask => 002, |
|
57
|
|
|
|
|
|
|
} )); |
|
58
|
|
|
|
|
|
|
} |
|
59
|
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
sub get_cached_result { |
|
61
|
|
|
|
|
|
|
my $name = shift; |
|
62
|
|
|
|
|
|
|
return($self->cache->get($name)); |
|
63
|
|
|
|
|
|
|
} |
|
64
|
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
sub get_feed { my ($url, $expire) = @_; |
|
66
|
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
require XML::Feed; |
|
68
|
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
$self->expire($expire |
|
70
|
|
|
|
|
|
|
? $expire |
|
71
|
|
|
|
|
|
|
: $self->hub->config->fetchrss_default_expire() |
|
72
|
|
|
|
|
|
|
); |
|
73
|
|
|
|
|
|
|
$self->setup_cache; |
|
74
|
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
my $content = $self->get_cached_result($url); |
|
76
|
|
|
|
|
|
|
if ( !defined($content) or !length($content) ) { |
|
77
|
|
|
|
|
|
|
$content = $self->get_content($url); |
|
78
|
|
|
|
|
|
|
} |
|
79
|
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
if (defined($content) and length($content)) { |
|
81
|
|
|
|
|
|
|
my $feed; |
|
82
|
|
|
|
|
|
|
# XXX needs to be an eval here, sometimes the parse |
|
83
|
|
|
|
|
|
|
# make poop on bad input |
|
84
|
|
|
|
|
|
|
eval { |
|
85
|
|
|
|
|
|
|
$feed = XML::Feed->parse(\$content) or die XML::Feed->errstr; |
|
86
|
|
|
|
|
|
|
}; |
|
87
|
|
|
|
|
|
|
return $feed unless $@; |
|
88
|
|
|
|
|
|
|
$self->error("xml parser error: $@"); |
|
89
|
|
|
|
|
|
|
} |
|
90
|
|
|
|
|
|
|
return {error => $self->error}; |
|
91
|
|
|
|
|
|
|
} |
|
92
|
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
package Kwiki::FetchRSS::Wafl; |
|
94
|
|
|
|
|
|
|
use Spoon::Formatter; |
|
95
|
|
|
|
|
|
|
use base 'Spoon::Formatter::WaflPhrase'; |
|
96
|
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
sub to_html { |
|
98
|
|
|
|
|
|
|
my ($url, $full, $expire) = split(/,?\s+/, $self->arguments); |
|
99
|
|
|
|
|
|
|
return $self->wafl_error unless $url; |
|
100
|
|
|
|
|
|
|
my $feed = $self->hub->fetchrss->get_feed($url, $expire); |
|
101
|
|
|
|
|
|
|
$self->hub->template->process('fetchrss.html', full => $full, |
|
102
|
|
|
|
|
|
|
method => $self->method, fetchrss_url => $url, |
|
103
|
|
|
|
|
|
|
feed => $feed); |
|
104
|
|
|
|
|
|
|
} |
|
105
|
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
1; |
|
108
|
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
package Kwiki::FetchRSS; |
|
110
|
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
1; |
|
112
|
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
__DATA__ |