root/lang/perl/plagger/lib/Plagger/Plugin/Publish/MixiComment.pm

Revision 18472, 3.7 kB (checked in by hashy1126, 3 months ago)

AUTHORを本名に変更(@LL Future会場)

Line 
1package Plagger::Plugin::Publish::MixiComment;
2use strict;
3use base qw( Plagger::Plugin );
4
5use Encode;
6use WWW::Mechanize;
7
8sub register {
9    my($self, $context) = @_;
10
11    $context->register_hook(
12        $self,
13        'publish.entry' => \&update,
14        'plugin.init'   => \&initialize,
15    );
16}
17
18sub initialize {
19    my($self, $context, $args) = @_;
20    my $response;
21
22    $self->{sleeping_time} = $self->conf->{interval} || 3;
23
24    my $email = $self->conf->{email} || 'xxxx@example.com',
25    my $password = $self->conf->{password} || 'xxxx';
26
27    my $mech = WWW::Mechanize->new( autocheck => 1 );
28    my $response = $mech->get( 'http://mixi.jp/home.pl' );
29    # login
30    $context->log(info => "mixi login start");
31    $mech->form_number(1);
32    $mech->set_fields('email' => $self->conf->{email}, 'password' => $self->conf->{password});
33    Time::HiRes::sleep($self->{sleeping_time});
34    $response = $mech->submit;
35    $context->log(info => "mixi login success");
36
37    while ($response->header('refresh')) {
38        my $refresh = $response->header('refresh');
39        $context->log(info => "mixi refresh processing...");
40        if ($refresh =~ /url=([^\s;]+)/) {
41            Time::HiRes::sleep($self->{sleeping_time});
42            $response = $mech->get(URI->new($1, $mech->uri));
43            $refresh = $mech->uri;
44        } else {
45            Time::HiRes::sleep($self->{sleeping_time});
46            $response = $mech->reload;
47        }
48    }
49   
50    $self->{mech} = $mech;
51}
52
53sub print_content {
54    my ($context, $mech, $response) = @_;
55   
56    foreach my $form ($mech->forms) {
57        foreach my $input ($form->inputs) {
58            $context->log(info => $input->name . " " . $input->type . "=>" . $input->value);
59        }
60    }
61}
62
63sub update {
64    my($self, $context, $args) = @_;
65    my $response;
66    my $mech = $self->{mech};
67
68    $context->log(info => "mixi get_content: " . $args->{entry}->permalink);
69
70    Time::HiRes::sleep($self->{sleeping_time});
71    $response = $mech->get($args->{entry}->permalink);
72
73    my $msg = $args->{entry}->body;
74    Encode::from_to( $msg, "utf8", "eucjp" );
75
76    eval {
77        print_content($context, $mech, $response);
78        $context->log(info => "mixi post comment");
79        $mech->form_name('comment_form');
80        $mech->set_fields('comment_body' => $msg);
81        Time::HiRes::sleep($self->{sleeping_time});
82        $response = $mech->submit;
83        print_content($context, $mech, $response);
84        $context->log(info => "mixi submit comment");
85        $mech->form_number(2);
86        Time::HiRes::sleep($self->{sleeping_time});
87        $response = $mech->submit;
88    };
89}
90
911;
92__END__
93
94=encoding utf-8
95
96=head1 NAME
97
98Plagger::Plugin::Publish::MixiComment - Post firend's diary comment with feeds
99
100=head1 NAME (ja)
101
102Plagger::Plugin::Publish::MixiComment - ミクシィにコメントをポストするプラグイン
103
104=head1 DISCLAIMER
105
106=head1 DISCLAIMER (ja)
107
108このモジュールを使用したことによる、利用者のいかなる損害・不利益等に関して作者は一切の責任を負わないものとします。
109
110=head1 SYNOPSIS
111
112  - module: Publish::MixiComment
113    config:
114      username: mixi-id
115      password: mixi-password
116
117=head1 DESCRIPTION
118
119=head1 DESCRIPTION (ja)
120
121permalinkの日記に対してbodyの内容をコメントとしてポストします。
122bodyの内容をFilterプラグインで内容を変更してください。
123
124=head1 CONFIG
125
126=over 4
127
128=item username
129
130mixi username. Required.
131
132=item password
133
134mixi password. Required.
135
136=item interval
137
138=back
139
140=head1 AUTHOR
141
142Yasuyuki Hashimoto
143
144=head1 SEE ALSO
145
146L<Plagger>, L<WWW::Mechanize>
147
148=cut
Note: See TracBrowser for help on using the browser.