Created
November 4, 2011 11:30
-
-
Save aerith/1339144 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# こんな感じ | |
--- | |
- | |
target: aerith | |
when: 17 | |
content: "test" | |
option: "#reminder" | |
start: | |
year: 2011 | |
month: 11 | |
day: 1 | |
end: | |
year: 2011 | |
month: 11 | |
day: 4 | |
--- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use strict; | |
use warnings; | |
use utf8; | |
use YAML::Syck; | |
use Config::Pit; | |
use Net::Twitter; | |
use File::Slurp; | |
binmode STDIN, ':utf8'; | |
binmode STDOUT, ':utf8'; | |
my $config = pit_get('aerith.twitter.com'); | |
my $result = "success"; | |
my %date = get_date(); | |
eval { | |
my $client = load_client($config) or die "You must set valid configuration."; | |
my $data_file = sprintf "%s/local/bin/twitter/notifier/messages.yaml" , $ENV{HOME}; | |
my $content = File::Slurp::read_file($data_file, { binmode => ':utf8' }); | |
my $messages = YAML::Syck::Load($content); | |
foreach my $message (@$messages) { | |
next unless validate_data($message); | |
my @when = (); | |
if (ref $message->{when} eq 'ARRAY') { | |
@when = @{$message->{when}}; | |
} elsif (not ref $message->{when}) { | |
@when = (int $message->{when}); | |
} else { | |
next; | |
} | |
my $status = create_status($message); | |
foreach my $when (@when) { | |
$client->update($status) if int $when == int $date{hour}; | |
} | |
} | |
}; if ($@) { | |
$result = $@; | |
} | |
print $result; | |
print "\n"; | |
exit; | |
sub load_client { | |
my $config = shift or return; | |
return Net::Twitter->new( | |
traits => [qw/OAuth API::REST/], | |
consumer_key => $config->{consumer_key}, | |
consumer_secret => $config->{consumer_secret}, | |
access_token => $config->{access_token}, | |
access_token_secret => $config->{access_token_secret}, | |
); | |
} | |
sub get_date { | |
my %date = (); | |
my $time = shift || time(); | |
@date{qw/second minute hour month_day month year week_day year_day is_dst/} = localtime($time); | |
$date{year} += 1900; | |
$date{month} += 1; | |
return %date; | |
} | |
sub validate_data { | |
my $message = shift or return; | |
return unless exists $message->{when}; | |
return unless exists $message->{target}; | |
return unless exists $message->{content}; | |
return unless is_started($message); | |
return if is_ended($message); | |
return 1; | |
} | |
sub create_status { | |
my $message = shift or return; | |
my $status = sprintf '@%s %s', $message->{target}, $message->{content}; | |
$status .= ' ' . $message->{option} if exists $message->{option} && length $message->{option}; | |
$status .= ' #' . time(); | |
$status = Encode::decode("utf8", $status); | |
return $status; | |
} | |
sub is_started { | |
my $message = shift or return; | |
my ($year, $month, $day) = (); | |
if (exists $message->{start} && ref $message->{start} eq 'HASH') { | |
$year = $message->{start}->{year}; | |
$month = $message->{start}->{month}; | |
$day = $message->{start}->{day}; | |
return unless $year && $month && $day; | |
if (int $year <= int $date{year} && | |
int $month <= int $date{month} && | |
int $day <= int $date{month_day}) { | |
return 1; | |
} | |
return; | |
} | |
return 1; | |
} | |
sub is_ended { | |
my $message = shift or return; | |
my ($year, $month, $day) = (); | |
if (exists $message->{end} && ref $message->{end} eq 'HASH') { | |
$year = $message->{end}->{year}; | |
$month = $message->{end}->{month}; | |
$day = $message->{end}->{day}; | |
return 1 unless $year && $month && $day; | |
if (int $year <= int $date{year} && | |
int $month <= int $date{month} && | |
int $day <= int $date{month_day}) { | |
return 1; | |
} | |
return; | |
} | |
return; | |
} | |
__DATA__ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment