OSDN Git Service

d3983ae5be314c06ab422c3f76bf8deff137e917
[mubot4fb/mubot4fb.git] / lib / Mubot4FB / TweetWorker.pm
1 #!/usr/bin/perl
2 #
3 # Copyright (c) 2012 ISHIKAWA Mutsumi <ishikawa@hanzubon.jp>
4 # This program is covered by the GNU General Public License 2
5 #
6 package Mubot4FB::TweetWorker;
7
8 use strict;
9 use utf8;
10
11 use base 'Mubot4FB::Common';
12
13 use Data::Dumper;
14
15 use Mubot4FB::Tweet;
16 use Mubot4FB::DB::TweetMap;
17
18 sub init {
19         my ($me) = @_;
20         $me->{mapdb} = Mubot4FB::DB::TweetMap->new($me->{cfg});
21         $me->{db} = Mubot4FB::DB->new($me->{cfg});
22         $me->{tw} = Mubot4FB::Tweet->new($me->{cfg});
23         return 1;
24 }
25
26 sub compose_text {
27         my ($me, $args) = @_;
28
29         return $args->{submitter} . '曰く、'.$args->{prefix}.' '.$args->{comment};
30 }
31
32 sub work {
33         my ($me, $job) = @_;
34
35         if ($job->{arg}->{command} eq 'remove') {
36                 my $data = $me->{mapdb}->search_by_key_id({key_id => $job->{arg}->{fb_post_id}});
37                 warn Dumper($data);
38                 if ($data) {
39                         my $status = $me->{tw}->remove($data->{tweet_id});
40                         warn Dumper($status) if ($me->{cfg}->{debug});
41                         $me->{mapdb}->delete_map_by_key_id({key_id => $job->{arg}->{fb_post_id}}) if ($status);
42                 }
43         } else {
44                 my $data = $me->{db}->search_by_fb_post_id({fb_post_id => $job->{arg}->{fb_post_id}});
45
46                 if ($data) {
47                         my $text = $me->compose_text($data);
48                         my $uri = $me->post_uri($job->{arg}->{post_id});
49                         my $status = $me->{tw}->publish($text, $uri);
50                         warn Dumper($status) if ($me->{cfg}->{debug});
51                         $me->{mapdb}->add_map({key_id => $job->{arg}->{fb_post_id},
52                                                tweet_id => $status->{id},
53                                                user_id => $status->{user}->{id},
54                                                screen_name => $status->{user}->{screen_name}}) if ($status);
55                 }
56         }
57 }
58
59 1;