OSDN Git Service

split fb class
authorISHIKAWA Mutsumi <ishikawa@hanzubon.jp>
Sat, 20 Oct 2012 20:50:21 +0000 (05:50 +0900)
committerISHIKAWA Mutsumi <ishikawa@hanzubon.jp>
Sat, 20 Oct 2012 20:50:21 +0000 (05:50 +0900)
mubot4fb.pl

index 865568c..5b5a024 100755 (executable)
@@ -3,24 +3,25 @@
 # 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},
@@ -43,6 +44,50 @@ sub fb_init {
        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;
@@ -54,19 +99,9 @@ sub misc_init {
        $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();
 }
 
@@ -157,22 +192,6 @@ sub _db_search_lastpost {
        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) = @_;
 
@@ -199,10 +218,10 @@ sub _add {
 
                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 ($@);
                }
 
@@ -213,7 +232,7 @@ sub _add {
                                         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';
                }
@@ -231,7 +250,7 @@ sub _delete_prev {
        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'});
        }
@@ -244,15 +263,15 @@ sub _delete {
        $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;
@@ -278,7 +297,7 @@ sub _search_next {
                        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;