root/lang/perl/WWW-CommentGetter/trunk/WWW-CommentGetter/lib/WWW/CommentGetter/Plugin.pm @ 1934

Revision 1934, 0.9 kB (checked in by tokuhirom, 6 years ago)

CommentGetter: trim the space.

Line 
1package WWW::CommentGetter::Plugin;
2use strict;
3use warnings;
4use utf8;
5use Web::Scraper;
6
7sub import {
8    my $pkg = caller(0);
9    my $class = shift;
10
11    for my $name ( qw/trim new scrape_by_siteinfo/) {
12        no strict 'refs'; ## no critic.
13        *{"$pkg\::$name"} = *{"$class\::$name"};
14    }
15}
16
17sub new { bless {}, shift }
18
19sub trim {
20    local $_ = shift;
21    s/^\s*//;
22    s/\s*$//;
23    return $_;
24}
25
26sub scrape_by_siteinfo {
27    my ($url, $siteinfo) = @_;
28
29    return unless $url =~ $siteinfo->{url};
30
31    my $ret = scraper {
32        process $siteinfo->{context}, 'comments[]' => scraper {
33            my $x = shift;
34            while (my ($key, $xpath) = each %{$siteinfo->{attributes}}) {
35                process $xpath, $key, sub {
36                    my $element = shift;
37                    trim $element->as_text;
38                };
39            }
40        };
41    }->scrape(URI->new($url));
42
43    return [ @{ $ret->{comments} } ];
44}
45
461;
Note: See TracBrowser for help on using the browser.