# Copyright (c) 2012 ISHIKAWA Mutsumi <ishikawa@hanzubon.jp>
# This program is covered by the GNU General Public License 2
#
-package Mubot4FB;
-
+package Mubot4FB::FB;
use strict;
use utf8;
-use base 'Bot::BasicBot';
use Facebook::Graph;
use LWP::UserAgent;
use HTTP::Request::Common;
-use DBI qw/:sql_types/;
-use POSIX 'strftime';
-use Data::Dumper;
+sub new {
+ my $proto = shift;
+ my $class = ref $proto || $proto;
+ my $self = {cfg => shift};
+ bless $self, $class;
-my $mu_re = qr/^([^\s]+)\s+((?:https?|ftps?):\/\/[^\s]+)\s+(.+)$/i;
-my $irc_type = 1;
+ $self->init();
+ return $self;
+}
-sub fb_init {
+sub init {
my ($me) = @_;
my $fb = Facebook::Graph->new(app_id => $me->{cfg}->{fb_app_id},
secret => $me->{cfg}->{fb_app_secret},
return $me->{fbo} = Facebook::Graph->new(access_token => $page_access_token);
}
+sub publish {
+ my ($me, $text, $uri) = @_;
+
+ return $me->{fbo}->add_post($me->{cfg}->{fb_page_id})
+ ->set_message($text)
+ ->set_link_uri($uri)
+ ->publish()
+ ->as_hashref();
+}
+
+sub post_uri {
+ my ($me, $post_id) = @_;
+
+ return $me->{cfg}->{fb_page_url} . 'posts/' . $post_id;
+}
+
+sub remove {
+ my ($me, $post_id) = @_;
+
+ my $req = HTTP::Request::Common::DELETE($me->post_uri($post_id));
+ $req->header('Content-Length', 0);
+ my $resp;
+ eval{$resp = LWP::UserAgent->new->request($req)};
+ return !$@;
+}
+
+1;
+package Mubot4FB;
+
+use strict;
+use utf8;
+
+use base 'Bot::BasicBot';
+use Facebook::Graph;
+use LWP::UserAgent;
+use HTTP::Request::Common;
+use DBI qw/:sql_types/;
+use POSIX 'strftime';
+
+use Data::Dumper;
+
+my $mu_re = qr/^([^\s]+)\s+((?:https?|ftps?):\/\/[^\s]+)\s+(.+)$/i;
+my $irc_type = 1;
+
sub db_init {
my ($me) = @_;
$me->{dbh} = DBI->connect('DBI:mysql:'.$me->{cfg}->{database}, $me->{cfg}->{db_user}, $me->{cfg}->{db_pass},{mysql_enable_utf8 => 1}) || die $DBI::errstr;
$me->{last_search} = {};
}
-sub publish {
- my ($me, $text, $uri) = @_;
-
- return $me->{fbo}->add_post($me->{cfg}->{fb_page_id})
- ->set_message($text)
- ->set_link_uri($uri)
- ->publish()
- ->as_hashref();
-}
-
sub init {
my ($me) = @_;
- $me->fb_init();
+ $me->{fb} = Mubot4FB::FB->new($me->{cfg});
$me->db_init();
}
return $ret;
}
-sub _fb_post_uri {
- my ($me, $post_id) = @_;
-
- return $me->{cfg}->{fb_page_url} . 'posts/' . $post_id;
-}
-
-sub _fb_delete {
- my ($me, $post_id) = @_;
-
- my $req = HTTP::Request::Common::DELETE($me->_fb_post_uri($post_id));
- $req->header('Content-Length', 0);
- my $resp;
- eval{$resp = LWP::UserAgent->new->request($req)};
- return !$@;
-}
-
sub _format_submit {
my ($me, $e) = @_;
return 0 if ($me->_check_dup($args, $uri));
- eval{$resp = $me->publish($text, $uri)};
+ eval{$resp = $me->{fb}->publish($text, $uri)};
if ($@) {
$me->fb_init();
- eval{$resp = $me->publish($text, $uri)};
+ eval{$resp = $me->{fb}->publish($text, $uri)};
$post_ok = 0 if ($@);
}
uri => $uri,
prefix => $prefix,
comment => $comment});
- $resp_msg = $args->{who} . ': うい '.$me->_fb_post_uri($post_id).' で登録';
+ $resp_msg = $args->{who} . ': うい '.$me->{fb}->post_uri($post_id).' で登録';
} else {
$resp_msg = 'can not post to facebook';
}
if (!defined $last_post) {
return $args->{who}.': いまのっていつの? というか ないし';
} elsif ($last_post->{post_time} < time() - 3600) {
- return $args->{who}.': いまのっていつの? 最後のはこれだけど古いんだにゃ ' . $me->_fb_post_uri($last_post->{fb_post_id});
+ return $args->{who}.': いまのっていつの? 最後のはこれだけど古いんだにゃ ' . $me->{fb}->post_uri($last_post->{fb_post_id});
} else {
return $me->_delete($args, $last_post->{'fb_post_id'});
}
$me->{dbh}->begin_work;
if ($resp = $me->_db_delete({fb_post_id => $post_id, submitter => $args->{who}})) {
# fb 側のエントリを削除しないといけない
- if ($me->_fb_delete($post_id)) {
+ if ($me->{fb}->remove($post_id)) {
$me->{dbh}->commit;
- $resp_msg = $args->{who} . ': 削除しました ' . $me->_fb_post_uri($post_id);
+ $resp_msg = $args->{who} . ': 削除しました ' . $me->{fb}->post_uri($post_id);
} else {
- $resp_msg = $args->{who} . ': 削除に失敗しましたよ? ' . $me->_fb_post_uri($post_id);
+ $resp_msg = $args->{who} . ': 削除に失敗しましたよ? ' . $me->{fb}->post_uri($post_id);
$me->{dbh}->rollback;
}
} else {
- $resp_msg = $args->{who} . ': そんな投稿ないよ? ' . $me->_fb_post_uri($post_id);
+ $resp_msg = $args->{who} . ': そんな投稿ないよ? ' . $me->{fb}->post_uri($post_id);
$me->{dbh}->rollback;
}
return $resp_msg;
my $count = @{$me->{last_search}->{$args->{who}}};
if ($count) {
}
- $resp_msg = $args->{who} . ': ' . $me->_format_submit($ent).'に言ってた '.($count ? '[ほか'.$count.'件] ' : '[ほかにはもうないよ] ').$me->_fb_post_uri($ent->{fb_post_id});
+ $resp_msg = $args->{who} . ': ' . $me->_format_submit($ent).'に言ってた '.($count ? '[ほか'.$count.'件] ' : '[ほかにはもうないよ] ').$me->{fb}->post_uri($ent->{fb_post_id});
}
}
return $resp_msg;