| 1 |
package Plagger::Plugin::CustomFeed::KotonohaInbox; |
|---|
| 2 |
use strict; |
|---|
| 3 |
use base qw( Plagger::Plugin ); |
|---|
| 4 |
|
|---|
| 5 |
use Encode; |
|---|
| 6 |
use WWW::Mechanize; |
|---|
| 7 |
use Memoize; |
|---|
| 8 |
|
|---|
| 9 |
memoize('get_icon'); |
|---|
| 10 |
|
|---|
| 11 |
sub plugin_id { |
|---|
| 12 |
my $self = shift; |
|---|
| 13 |
$self->class_id . '-' . $self->conf->{email}; |
|---|
| 14 |
} |
|---|
| 15 |
|
|---|
| 16 |
sub register { |
|---|
| 17 |
my($self, $context) = @_; |
|---|
| 18 |
$context->register_hook( |
|---|
| 19 |
$self, |
|---|
| 20 |
'subscription.load' => \&load, |
|---|
| 21 |
); |
|---|
| 22 |
} |
|---|
| 23 |
|
|---|
| 24 |
sub load { |
|---|
| 25 |
my($self, $context) = @_; |
|---|
| 26 |
|
|---|
| 27 |
my $feed = Plagger::Feed->new; |
|---|
| 28 |
$feed->aggregator(sub { $self->aggregate(@_) }); |
|---|
| 29 |
$context->subscription->add($feed); |
|---|
| 30 |
} |
|---|
| 31 |
|
|---|
| 32 |
sub aggregate { |
|---|
| 33 |
my($self, $context, $args) = @_; |
|---|
| 34 |
|
|---|
| 35 |
my $start = 'http://kotonoha.cc/'; |
|---|
| 36 |
|
|---|
| 37 |
my $mech = WWW::Mechanize->new(cookie_jar => $self->cache->cookie_jar); |
|---|
| 38 |
$mech->agent_alias( 'Windows IE 6' ); |
|---|
| 39 |
$mech->get($start); |
|---|
| 40 |
|
|---|
| 41 |
if ($mech->content =~ /loginform/) { |
|---|
| 42 |
my $success; |
|---|
| 43 |
eval { $success = $self->login($mech) }; |
|---|
| 44 |
|
|---|
| 45 |
if ($@) { |
|---|
| 46 |
$context->log(error => "Login failed. Clear cookie and redo."); |
|---|
| 47 |
$mech->cookie_jar->clear; |
|---|
| 48 |
$mech->get($start); |
|---|
| 49 |
sleep 3; |
|---|
| 50 |
eval { $success = $self->login($mech) }; |
|---|
| 51 |
} |
|---|
| 52 |
|
|---|
| 53 |
return unless $success; |
|---|
| 54 |
} |
|---|
| 55 |
|
|---|
| 56 |
$context->log(info => "Login to kotonoha succeeded."); |
|---|
| 57 |
|
|---|
| 58 |
|
|---|
| 59 |
$mech->get('http://kotonoha.cc/inbox'); |
|---|
| 60 |
|
|---|
| 61 |
my $content = decode('utf-8', $mech->content); |
|---|
| 62 |
my $re = '<a href="/user/([^>]+)">.+<a href="/no/(\d+)">([^>]+)</a>(.+)\s*</li>'; |
|---|
| 63 |
|
|---|
| 64 |
my $feed = Plagger::Feed->new; |
|---|
| 65 |
$feed->title('kotonoha inbox'); |
|---|
| 66 |
$feed->link('http://kotonoha.cc'); |
|---|
| 67 |
$feed->image({ |
|---|
| 68 |
url => 'http://kotonoha.cc/images/logo.png', |
|---|
| 69 |
link => 'http://kotonoha.cc/', |
|---|
| 70 |
}); |
|---|
| 71 |
|
|---|
| 72 |
while ($content =~ /$re/g) { |
|---|
| 73 |
my $entry = Plagger::Entry->new; |
|---|
| 74 |
|
|---|
| 75 |
$entry->title($3); |
|---|
| 76 |
$entry->body($4); |
|---|
| 77 |
$entry->author($1); |
|---|
| 78 |
$entry->link("http://kotonoha.cc/no/$2#$1"); |
|---|
| 79 |
|
|---|
| 80 |
my $icon = $self->get_icon($mech, $1); |
|---|
| 81 |
$entry->icon({ |
|---|
| 82 |
title => $1, |
|---|
| 83 |
url => $icon->{url}, |
|---|
| 84 |
link => $icon->{link}, |
|---|
| 85 |
}); |
|---|
| 86 |
|
|---|
| 87 |
$feed->add_entry($entry); |
|---|
| 88 |
} |
|---|
| 89 |
$context->update->add($feed); |
|---|
| 90 |
} |
|---|
| 91 |
|
|---|
| 92 |
sub login { |
|---|
| 93 |
my($self, $mech, $retry) = @_; |
|---|
| 94 |
|
|---|
| 95 |
$mech->submit_form( |
|---|
| 96 |
fields => { |
|---|
| 97 |
mail => $self->conf->{email}, |
|---|
| 98 |
password => $self->conf->{password}, |
|---|
| 99 |
set_cookie => 'on', |
|---|
| 100 |
}, |
|---|
| 101 |
); |
|---|
| 102 |
|
|---|
| 103 |
while ($mech->content =~ m!ãã°ã€ã³ã§ããŸããã§ãã!) { |
|---|
| 104 |
Plagger->context->log(error => "Login to kotonoha failed."); |
|---|
| 105 |
return; |
|---|
| 106 |
} |
|---|
| 107 |
|
|---|
| 108 |
return 1; |
|---|
| 109 |
} |
|---|
| 110 |
|
|---|
| 111 |
sub get_icon { |
|---|
| 112 |
my ($self, $mech, $user) = @_; |
|---|
| 113 |
|
|---|
| 114 |
Plagger->context->log('info', "Getting an icon of $user"); |
|---|
| 115 |
$mech->get("http://kotonoha.cc/user/$user"); |
|---|
| 116 |
my $content = decode('utf-8', $mech->content); |
|---|
| 117 |
my $re = '<img src="(/upload/profile/.+.jpg)"'; |
|---|
| 118 |
|
|---|
| 119 |
my $icon; |
|---|
| 120 |
while ($content =~ /$re/g) { |
|---|
| 121 |
$icon->{url} = "http://kotonoha.cc$1"; |
|---|
| 122 |
$icon->{title} = $user; |
|---|
| 123 |
$icon->{link} = "http://kotonoha.cc/user/$user" |
|---|
| 124 |
} |
|---|
| 125 |
|
|---|
| 126 |
return $icon; |
|---|
| 127 |
} |
|---|
| 128 |
|
|---|
| 129 |
1; |
|---|
| 130 |
__END__ |
|---|
| 131 |
|
|---|
| 132 |
=head1 NAME |
|---|
| 133 |
|
|---|
| 134 |
Plagger::Plugin::CustomFeed::KotonohaInbox - Kotonoha Inbox custom feed |
|---|
| 135 |
|
|---|
| 136 |
=head1 SYNOPSIS |
|---|
| 137 |
|
|---|
| 138 |
- module: CustomFeed::KotonohaInbox |
|---|
| 139 |
config: |
|---|
| 140 |
email: email@example.com |
|---|
| 141 |
password: password |
|---|
| 142 |
|
|---|
| 143 |
=head1 DESCRIPTION |
|---|
| 144 |
|
|---|
| 145 |
This plugin fetches your inbox data from kotonoha.cc. |
|---|
| 146 |
|
|---|
| 147 |
=head1 CONFIG |
|---|
| 148 |
|
|---|
| 149 |
=over 4 |
|---|
| 150 |
|
|---|
| 151 |
=item email, password |
|---|
| 152 |
|
|---|
| 153 |
Credential you need to login to kotonoha.cc. |
|---|
| 154 |
|
|---|
| 155 |
=back |
|---|
| 156 |
|
|---|
| 157 |
=head1 AUTHOR |
|---|
| 158 |
|
|---|
| 159 |
Gosuke Miyashita |
|---|
| 160 |
|
|---|
| 161 |
=head1 SEE ALSO |
|---|
| 162 |
|
|---|
| 163 |
L<Plagger> |
|---|
| 164 |
|
|---|
| 165 |
=cut |
|---|
| 166 |
|
|---|