-
Notifications
You must be signed in to change notification settings - Fork 2
/
XPathFeed.pm
389 lines (327 loc) · 9.01 KB
/
XPathFeed.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
package XPathFeed;
use strict;
use warnings;
use base qw/Class::Accessor::Fast Class::Data::Inheritable/;
use XPathFeed::UserAgent;
use Encode qw(decode_utf8);
use HTML::ResolveLink;
use HTML::Selector::XPath;
use HTML::Tagset;
use HTML::TreeBuilder 5 -weak;
use HTML::TreeBuilder::XPath;
use HTTP::Request;
use Scalar::Util qw(blessed);
use URI;
use URI::Escape qw(uri_escape);
use XML::RSS;
our ($UserAgent, $Cache);
our $EXPIRE = 10 * 60; # 10分
our $DEFAULT_XPATH_ITEM = {
title => '//a',
link => '//a/@href',
image => '//img/@src',
description => '//*',
};
__PACKAGE__->mk_classdata(
params => [
qw{
url
search_word
xpath_list
xpath_item_title
xpath_item_link
xpath_item_image
xpath_item_description
},
],
);
__PACKAGE__->mk_accessors(
@{__PACKAGE__->params},
qw{
error
create
},
);
sub new {
my $class = shift;
my %args = @_==1 ? %{$_[0]} : @_;
return $class->SUPER::new({%args});
}
sub new_from_query {
my ($class, $q) = @_;
$class->new(
map {
$_ => $q->param($_) || '',
} @{$class->params},
);
}
sub ua {
$UserAgent ||= XPathFeed::UserAgent->new;
}
sub cache {
my ($self, $new_value) = @_;
return $Cache = $new_value if $new_value;
$Cache ||= do {
require Cache::FileCache;
Cache::FileCache->new(
{
namespace => 'xpathfeed',
cache_root => '/tmp/filecache',
}
);
};
}
sub resolver {
my $self = shift;
$self->{resolver} ||= HTML::ResolveLink->new(
base => $self->url,
);
}
sub uri {
my $self = shift;
my $url = $self->url;
$url =~ /http/ or $url = "http://$url";
$self->{uri} ||= URI->new($url)->canonical;
}
sub http_result {
my $self = shift;
$self->{http_result} ||= do {
my $url = $self->uri;
my $cache = $self->cache;
my $result = $cache->get($url);
if (!$result) {
# キャッシュがない場合
my $res = $self->_get($url);
if ($res->is_success) {
$result = $self->_res2result($res);
$cache->set($url, $result);
}
} elsif (my $now = time() - $result->{cached} > $EXPIRE) {
# キャッシュがexpireしている場合
my $res = $self->_get($url, $now);
if ($res->code == 304) {
$result->{cached} = $now; # 時間だけ上書き
$cache->set($url, $result);
} elsif ($res->is_success) {
$result = $self->_res2result($res);
$cache->set($url, $result);
}
}
$result || {};
}
}
sub _get {
my $self = shift;
my $url = shift;
my $time = shift; # If-Modified-Since
my $req = HTTP::Request->new('GET', $url);
$req->if_modified_since($time) if $time;
return $self->ua->request($req);
}
sub _res2result {
my ($self, $res) = @_;
return {
content => $res->content,
resolved_content => $self->_resolve($res),
decoded_content => $res->decoded_content,
cached => time(),
};
}
sub _resolve {
my $self = shift;
my $res = shift or return;
my $content = $self->resolver->resolve($res->decoded_content);
Encode::encode($res->content_charset, $content);
}
sub _add_inspector {
my $content = shift;
$content =~ s{<base [^>]+>}{}ig;
my $script = '<script type="text/javascript" charset="utf-8" src="/js/inspector.js"></script>';
$content =~ s{(</body>)}{$script$1} or $content .= $script;
$content;
}
sub frame_content {
my $self = shift;
$self->{frame_content} ||= _add_inspector($self->resolved_content);
}
BEGIN {
no strict 'refs';
for my $method (qw/content resolved_content decoded_content/) {
*{__PACKAGE__.'::'.$method} = sub {
my $self = shift;
$self->{$method} = shift || $self->http_result->{$method} || '';
}
}
}
sub tree {
my $self = shift;
return $self->{tree} if exists $self->{tree};
$self->{tree} = do {
my $t = HTML::TreeBuilder::XPath->new;
$t->parse($self->decoded_content);
$t->eof;
$t;
} || undef;
}
sub list {
my $self = shift;
return $self->{list} if defined $self->{list};
my $class = ref $self;
my $list = eval {
local $SIG{__WARN__} = sub { };
my @nodes = $self->tree->findnodes(xpath($self->xpath_list));
[map { {node => $_} } @nodes];
} || [];
for my $item (@$list) {
$item or next;
my $node = $item->{node} or next;
my $tree = $node->clone or next; # この要素以下のtreeにする
for my $key (sort keys %$DEFAULT_XPATH_ITEM) {
my $method = 'xpath_item_' . $key;
my $xpath = $self->$method() || $DEFAULT_XPATH_ITEM->{$key} or next;
$item->{$key} = eval {
local $SIG{__WARN__} = sub { };
my @nodes = $tree->findnodes(xpath($xpath));
extract($nodes[0], $key, $self->uri);
};
$item->{html} = $node->as_XML;
}
push @{$self->{list}}, $item;
$tree->delete;
}
$self->{list};
}
sub list_size {
my $self = shift;
return scalar @{$self->list || []};
}
sub title {
my $self = shift;
$self->{title} // eval {
local $SIG{__WARN__} = sub { };
my ($node) = $self->tree->findnodes(xpath('title'));
my $title = $node ? $node->as_text || '' : '';
$title =~ s{\s+}{ }g;
$title =~ s{(?:^\s|\s$)}{}g;
$title;
} || $self->url || '';
}
sub search {
my $self = shift;
return $self->{search_result} if defined $self->{search_result};
my $word = $self->search_word or return;
$word =~ s{'}{\\'}g;
my $xpath = xpath(sprintf(q{//text()[contains(.,'%s')]/..}, $word));
$self->{search_result} = do {
local $SIG{__WARN__} = sub { };
my @nodes = $self->tree->findnodes($xpath);
[map { {node => $_} } @nodes];
} || [];
return $self->{search_result};
}
sub feed {
my $self = shift;
my $list = $self->list;
$self->{feed} ||= do {
my $rss = XML::RSS->new (version => '2.0');
$rss->channel(
title => $self->title,
link => $self->page_uri,
description => $self->url,
);
for my $item (@$list) {
$rss->add_item(
title => $item->{title},
link => $item->{link},
description => ( $item->{image} ? qq{<img src="$item->{image}"><br>} : '' )
. $item->{description},
enclosure => $item->{image} ? {
url => $item->{image},
type => "image"
} : undef,
);
}
$rss->as_string;
};
}
sub page_uri { shift->add_query_params(URI->new('/')) }
sub feed_uri { shift->add_query_params(URI->new('/feed')) }
sub add_query_params {
my $self = shift;
my $uri = shift or return;
for my $key (qw/url xpath_list xpath_item_title xpath_item_link xpath_item_image xpath_item_description/) {
$self->$key() or next;
$uri->query_form(
$uri->query_form,
$key => $self->$key(),
);
}
return $uri;
}
# utility method
sub xpath {
# xpath || css selector を xpath に変換する
# copy from Web::Scraper
my $exp = shift;
my $xpath = $exp =~ m!^(?:/|id\()! ? $exp : HTML::Selector::XPath::selector_to_xpath($exp);
decode_utf8($xpath);
}
sub extract {
my ($node, $key, $uri) = @_;
if (blessed($node) && $node->isa('HTML::TreeBuilder::XPath::Attribute')) {
if (is_link_element($node->getParentNode, $node->getName)) {
URI->new_abs($node->getValue, $uri);
} else {
$node->getValue;
}
} elsif (blessed($node) && $node->can('as_text')) {
$node->as_text;
}
}
sub is_link_element {
my($node, $attr) = @_;
my $link_elements = $HTML::Tagset::linkElements{$node->tag} || [];
for my $elem (@$link_elements) {
return 1 if $attr eq $elem;
}
return;
}
1;
__END__
=head1 NAME
XPathFeed - Generate RSS Feed from XPath
=head1 SYNOPSIS
use XPathFeed;
my $x = XPathFeed->new;
$x->url($url);
$x->xpath_list($xpath);
print $x->feed;
=head1 DESCRIPTION
XPathFeed is a feed generator from xpath.
use XPathFeed;
my $x = XPathFeed->new;
$x->url($url);
$x->xpath_list($xpath);
print $x->feed;
=over
=item new
=item new_from_query
=item url
=item xpath_list
=item xpath_item_title
=item xpath_item_link
=item xpath_item_image
=item xpath_item_description
=item list
=item feed
=back
=head1 AUTHOR
Yasuhiro Onishi E<lt>[email protected]<gt>
=head1 SEE ALSO
=over
=item L<Web::Scraper>
=back
=head1 LICENSE
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=cut