| 1 |
package Plagger::Plugin::CustomFeed::Nowa; |
|---|
| 2 |
use strict; |
|---|
| 3 |
use warnings; |
|---|
| 4 |
use utf8; |
|---|
| 5 |
use base qw( Plagger::Plugin ); |
|---|
| 6 |
|
|---|
| 7 |
use Web::Scraper; |
|---|
| 8 |
use HTTP::Request::Common; |
|---|
| 9 |
use HTTP::Cookies; |
|---|
| 10 |
use DateTime::Duration; |
|---|
| 11 |
|
|---|
| 12 |
use Plagger::UserAgent; |
|---|
| 13 |
use Plagger::Util qw( decode_content ); |
|---|
| 14 |
|
|---|
| 15 |
sub register { |
|---|
| 16 |
my($self, $context) = @_; |
|---|
| 17 |
$context->register_hook( |
|---|
| 18 |
$self, |
|---|
| 19 |
'subscription.load' => \&load, |
|---|
| 20 |
); |
|---|
| 21 |
} |
|---|
| 22 |
|
|---|
| 23 |
sub load { |
|---|
| 24 |
my ($self, $context) = @_; |
|---|
| 25 |
|
|---|
| 26 |
my $feed = Plagger::Feed->new; |
|---|
| 27 |
$feed->aggregator(sub { $self->aggregate(@_) }); |
|---|
| 28 |
$context->subscription->add($feed); |
|---|
| 29 |
} |
|---|
| 30 |
|
|---|
| 31 |
sub aggregate { |
|---|
| 32 |
my($self, $context, $args) = @_; |
|---|
| 33 |
|
|---|
| 34 |
my $feed = Plagger::Feed->new; |
|---|
| 35 |
$feed->type('nowa'); |
|---|
| 36 |
$feed->title("[nowa] ナニシテル? & 新着記事"); |
|---|
| 37 |
$feed->id('nowa'); |
|---|
| 38 |
|
|---|
| 39 |
my $cookie_jar = HTTP::Cookies->new( |
|---|
| 40 |
file => '/tmp/cookies.txt', |
|---|
| 41 |
autosave => 1, |
|---|
| 42 |
); |
|---|
| 43 |
|
|---|
| 44 |
my $agent = Plagger::UserAgent->new; |
|---|
| 45 |
$agent->cookie_jar($cookie_jar); |
|---|
| 46 |
|
|---|
| 47 |
my $login_url = 'http://my.nowa.jp/login/'; |
|---|
| 48 |
|
|---|
| 49 |
my $login_req = POST($login_url, { |
|---|
| 50 |
nowa_id => $self->conf->{id}, |
|---|
| 51 |
password => $self->conf->{password}, |
|---|
| 52 |
}); |
|---|
| 53 |
my $login_res = $agent->request($login_req); |
|---|
| 54 |
|
|---|
| 55 |
my $url = 'http://my.nowa.jp/friend/'; |
|---|
| 56 |
my $res = $agent->fetch($url, $self); |
|---|
| 57 |
|
|---|
| 58 |
if ($res->is_error) { |
|---|
| 59 |
$context->log(error => "GET $url failed: " . $res->http_status); |
|---|
| 60 |
return; |
|---|
| 61 |
} |
|---|
| 62 |
|
|---|
| 63 |
if (!$self->conf->{article} && !$self->conf->{nanishiteru}) { |
|---|
| 64 |
$self->conf->{article} = 1; |
|---|
| 65 |
$self->conf->{nanishiteru} = 1; |
|---|
| 66 |
} |
|---|
| 67 |
|
|---|
| 68 |
my $content = decode_content($res); |
|---|
| 69 |
scraper { |
|---|
| 70 |
process 'div[class="friendentrybody"]', 'articles[]' => sub { |
|---|
| 71 |
my $node = shift; |
|---|
| 72 |
my $entry = $self->_create_entry($node); |
|---|
| 73 |
if ($self->conf->{article} && $entry->body) { |
|---|
| 74 |
$feed->add_entry($entry); |
|---|
| 75 |
} |
|---|
| 76 |
elsif ($self->conf->{nanishiteru} && !$entry->body) { |
|---|
| 77 |
$feed->add_entry($entry); |
|---|
| 78 |
} |
|---|
| 79 |
}; |
|---|
| 80 |
}->scrape($content); |
|---|
| 81 |
|
|---|
| 82 |
$context->update->add($feed); |
|---|
| 83 |
} |
|---|
| 84 |
|
|---|
| 85 |
sub _create_entry { |
|---|
| 86 |
my ($self, $node) = @_; |
|---|
| 87 |
|
|---|
| 88 |
my $entry_scraped = scraper { |
|---|
| 89 |
process 'p', body => 'TEXT'; |
|---|
| 90 |
process 'a.blue-cms', author => 'TEXT'; |
|---|
| 91 |
process 'h2', title => 'TEXT'; |
|---|
| 92 |
process 'h2 > a', link => '@href'; |
|---|
| 93 |
process 'span.time', time => 'TEXT'; |
|---|
| 94 |
result 'body', 'author', 'title', 'link', 'time'; |
|---|
| 95 |
}->scrape($node); |
|---|
| 96 |
|
|---|
| 97 |
|
|---|
| 98 |
my $time_str = $entry_scraped->{time}; |
|---|
| 99 |
my $time; |
|---|
| 100 |
my $duration; |
|---|
| 101 |
if (($time) = $time_str =~ m/^\((\d+)時間/xms) { |
|---|
| 102 |
$duration = DateTime::Duration->new(hours => $time); |
|---|
| 103 |
} |
|---|
| 104 |
elsif (($time) = $time_str =~ m/^\((\d+)分/xms) { |
|---|
| 105 |
$duration = DateTime::Duration->new(minutes => $time); |
|---|
| 106 |
} |
|---|
| 107 |
elsif (($time) = $time_str =~ m/^\((\d+)秒/xms) { |
|---|
| 108 |
$duration = DateTime::Duration->new(seconds => $time); |
|---|
| 109 |
} |
|---|
| 110 |
my $date = Plagger::Date->now() - $duration; |
|---|
| 111 |
|
|---|
| 112 |
my $entry = Plagger::Entry->new; |
|---|
| 113 |
$entry->title($entry_scraped->{title}); |
|---|
| 114 |
$entry->link($entry_scraped->{link}); |
|---|
| 115 |
$entry->author($entry_scraped->{author}); |
|---|
| 116 |
$entry->body($entry_scraped->{body}); |
|---|
| 117 |
$entry->date($date); |
|---|
| 118 |
|
|---|
| 119 |
return $entry; |
|---|
| 120 |
} |
|---|
| 121 |
|
|---|
| 122 |
1; |
|---|
| 123 |
|
|---|
| 124 |
__END__ |
|---|
| 125 |
|
|---|