From 201caaf75fcd00235dde85cfab1364d1e17e5344 Mon Sep 17 00:00:00 2001 From: Matthew Lemon Date: Mon, 23 Nov 2020 20:28:27 +0000 Subject: working script --- tw_hooks/first_attempt.pl | 88 ++++++++++++++++++++++++++++++++++ tw_hooks/on-add_schedule_tg.pl | 88 ---------------------------------- tw_hooks/on-add_scheduled_work_task.pl | 73 ++++++++++++++++++++++++++++ tw_hooks/scratch.pl | 68 -------------------------- 4 files changed, 161 insertions(+), 156 deletions(-) create mode 100644 tw_hooks/first_attempt.pl delete mode 100644 tw_hooks/on-add_schedule_tg.pl create mode 100755 tw_hooks/on-add_scheduled_work_task.pl delete mode 100644 tw_hooks/scratch.pl (limited to 'tw_hooks') diff --git a/tw_hooks/first_attempt.pl b/tw_hooks/first_attempt.pl new file mode 100644 index 0000000..5423180 --- /dev/null +++ b/tw_hooks/first_attempt.pl @@ -0,0 +1,88 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature qw(say); +use JSON; +use DateTime; +use DateTime::Format::ISO8601; + +# a test hook in Perl for taskwarrior + + +# ALGORITHM + +# Parse the due attribute from TW +# Convert it into Remind format +# Log into remote server +# Check for presece or remind file +# If it is there, back it up +# Append the Remind formatted line to the original remind file + +my @short_months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec); +my @days_of_week = qw(Sunday Monday Tuesday Wednesday Thursday Friday Saturday); + +# this must be JSON - this gets passed in my taskwarrior +my $added_task = ; + +my $hashref = decode_json $added_task; + +sub parse_scheduled +{ + my $sched_date = shift; + return DateTime::Format::ISO8601->parse_datetime($sched_date); +} + +if ($hashref->{scheduled}) { + my $scheduled_dt = parse_scheduled $hashref->{scheduled}; + print "Scheduled Date:\n"; + print "---------------\n"; + print "Year is: ".$scheduled_dt->year() . "\n"; + print "Month is: ".$scheduled_dt->month() . "\n"; + print "Month again is: ".$short_months[$scheduled_dt->month()-1] . "\n"; + print "Day is: ".$scheduled_dt->day() . "\n"; + # O + # # wday() is builtin? + # https://stackoverflow.com/questions/10919585/extract-day-of-week-using-perl-and-mysql-date-format + # https://metacpan.org/pod/Time::Piece + print "Day of week is: ".$days_of_week[$scheduled_dt->day_of_week() % 7] . "\n"; + print "Quarter is: ".$scheduled_dt->quarter(). "\n"; + print "\n"; +} + +# if ($hashref->{scheduled}) { +# my $sched_date = $hashref->{scheduled}; +# my $year = substr $sched_date, 0, 4; +# my $month = substr $sched_date, 4, 2; +# my $day = substr $sched_date, 6, 2; + +# my $dt = DateTime->new( +# year => $year, +# month => $month, +# day => $day, +# time_zone => 'Europe/London', +# ); + +# print "Year is: $dt->year" . "\n"; +# print "Month is: $dt->month" . "\n"; +# print "Day is: $dt->$day" . "\n"; +# print "\n"; +# } + +my $original_description = ${$hashref}{description}; +# my $original_description = $hashref->{description}; # alternative (and +# preferred) + +my $tags = ${$hashref}{tags}; # alternative - not using -> in the ref + +$hashref->{description} = "DfT Task: " . $original_description if scalar grep {$_ eq "dft" } @{$tags}; +# same as +# $hashref->{description} = "DfT Task: " . $original_description if scalar grep {$_ eq "dft" } @$tags; +# You don't need the {} brackets! see perlreftut - The Rest section near the +# end. + +my $output = encode_json $hashref; + +print $output; + +exit 0; diff --git a/tw_hooks/on-add_schedule_tg.pl b/tw_hooks/on-add_schedule_tg.pl deleted file mode 100644 index 5423180..0000000 --- a/tw_hooks/on-add_schedule_tg.pl +++ /dev/null @@ -1,88 +0,0 @@ -#!/usr/bin/env perl - -use strict; -use warnings; -use feature qw(say); -use JSON; -use DateTime; -use DateTime::Format::ISO8601; - -# a test hook in Perl for taskwarrior - - -# ALGORITHM - -# Parse the due attribute from TW -# Convert it into Remind format -# Log into remote server -# Check for presece or remind file -# If it is there, back it up -# Append the Remind formatted line to the original remind file - -my @short_months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec); -my @days_of_week = qw(Sunday Monday Tuesday Wednesday Thursday Friday Saturday); - -# this must be JSON - this gets passed in my taskwarrior -my $added_task = ; - -my $hashref = decode_json $added_task; - -sub parse_scheduled -{ - my $sched_date = shift; - return DateTime::Format::ISO8601->parse_datetime($sched_date); -} - -if ($hashref->{scheduled}) { - my $scheduled_dt = parse_scheduled $hashref->{scheduled}; - print "Scheduled Date:\n"; - print "---------------\n"; - print "Year is: ".$scheduled_dt->year() . "\n"; - print "Month is: ".$scheduled_dt->month() . "\n"; - print "Month again is: ".$short_months[$scheduled_dt->month()-1] . "\n"; - print "Day is: ".$scheduled_dt->day() . "\n"; - # O - # # wday() is builtin? - # https://stackoverflow.com/questions/10919585/extract-day-of-week-using-perl-and-mysql-date-format - # https://metacpan.org/pod/Time::Piece - print "Day of week is: ".$days_of_week[$scheduled_dt->day_of_week() % 7] . "\n"; - print "Quarter is: ".$scheduled_dt->quarter(). "\n"; - print "\n"; -} - -# if ($hashref->{scheduled}) { -# my $sched_date = $hashref->{scheduled}; -# my $year = substr $sched_date, 0, 4; -# my $month = substr $sched_date, 4, 2; -# my $day = substr $sched_date, 6, 2; - -# my $dt = DateTime->new( -# year => $year, -# month => $month, -# day => $day, -# time_zone => 'Europe/London', -# ); - -# print "Year is: $dt->year" . "\n"; -# print "Month is: $dt->month" . "\n"; -# print "Day is: $dt->$day" . "\n"; -# print "\n"; -# } - -my $original_description = ${$hashref}{description}; -# my $original_description = $hashref->{description}; # alternative (and -# preferred) - -my $tags = ${$hashref}{tags}; # alternative - not using -> in the ref - -$hashref->{description} = "DfT Task: " . $original_description if scalar grep {$_ eq "dft" } @{$tags}; -# same as -# $hashref->{description} = "DfT Task: " . $original_description if scalar grep {$_ eq "dft" } @$tags; -# You don't need the {} brackets! see perlreftut - The Rest section near the -# end. - -my $output = encode_json $hashref; - -print $output; - -exit 0; diff --git a/tw_hooks/on-add_scheduled_work_task.pl b/tw_hooks/on-add_scheduled_work_task.pl new file mode 100755 index 0000000..f9d6a21 --- /dev/null +++ b/tw_hooks/on-add_scheduled_work_task.pl @@ -0,0 +1,73 @@ +#!/bin/env perl + +use warnings; +use strict; +use feature qw(say); +use JSON; +use DateTime; +use DateTime::Format::ISO8601; +use Net::OpenSSH; + + +my @short_months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec); + +# subs +sub parse_scheduled +{ + my $sched_date = shift; + return DateTime::Format::ISO8601->parse_datetime($sched_date); +} + +# ALGORITHM +# Parse the scheduled attribute from TW + +my $added_task = ; +my $work_rem_file = '~/.reminders/work.rem'; +my $decoded_task = decode_json $added_task; +my $original_description = ${$decoded_task}{description}; +my $tags = ${$decoded_task}{tags}; # alternative - not using -> in the ref +my $scheduled_dt; + +if ($decoded_task->{scheduled} and (scalar grep {$_ eq "dft" } @{$tags})) { + $scheduled_dt = parse_scheduled $decoded_task->{scheduled}; + my $date = $scheduled_dt->day(); + my $month = $short_months[$scheduled_dt->month()-1]; + my $year = $scheduled_dt->year(); + my $hr = $scheduled_dt->hour(); + my $min = $scheduled_dt->minute(); + my $time = $scheduled_dt->hms(); + # Convert it into Remind format + my $remind_line = "REM $date $month $year AT $time MSG $original_description \%b\n"; + + # Log into remote server + + my $host = $ENV{"TW_HOOK_REMIND_REMOTE_HOST"} or die "Cannot get TW_HOOK_REMIND_REMOTE_HOST environment variable"; + my $user = "lemon"; + + my $ssh = Net::OpenSSH->new($host, user => $user); + $ssh->error and die "Couldn't establish SSH connection: " . $ssh->error; + + # Check for presece or remind file + if ($ssh->test("ls $work_rem_file") != 1) { die "Cannot find $work_rem_file on $host."}; + + # If it is there, back it up + $ssh->system("cp $work_rem_file $work_rem_file.bak"); + + # Append the Remind formatted line to the original remind file + $ssh->system({stdin_data => $remind_line}, "cat >> ~/.reminders/work.rem") or die "Cannot append text: " . $ssh->error; + + # Reset the back up + # $ssh->system("mv $work_rem_file.bak $work_rem_file"); + # $decoded_task->{"Description"} = "Cocks"; + + print "Trumpets\n"; + print encode_json $decoded_task; + exit 0; +} else { + print encode_json $decoded_task; + exit 0; +} + + + + diff --git a/tw_hooks/scratch.pl b/tw_hooks/scratch.pl deleted file mode 100644 index ec74df3..0000000 --- a/tw_hooks/scratch.pl +++ /dev/null @@ -1,68 +0,0 @@ -use warnings; -use strict; -use feature qw(say); -use JSON; -use DateTime; -use DateTime::Format::ISO8601; -use Net::OpenSSH; - - -my @short_months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec); - -# subs -sub parse_scheduled -{ - my $sched_date = shift; - return DateTime::Format::ISO8601->parse_datetime($sched_date); -} - -# ALGORITHM -# Parse the scheduled attribute from TW - -my $added_task = ; -my $work_rem_file = '~/.reminders/work.rem'; -my $decoded_task = decode_json $added_task; -my $original_description = ${$decoded_task}{description}; -my $tags = ${$decoded_task}{tags}; # alternative - not using -> in the ref -my $scheduled_dt; - -if ($decoded_task->{scheduled} and (scalar grep {$_ eq "dft" } @{$tags})) { - $scheduled_dt = parse_scheduled $decoded_task->{scheduled}; - my $date = $scheduled_dt->day(); - my $month = $short_months[$scheduled_dt->month()-1]; - my $year = $scheduled_dt->year(); - my $hr = $scheduled_dt->hour(); - my $min = $scheduled_dt->minute(); - my $time = $scheduled_dt->hms(); - # Convert it into Remind format - my $remind_line = "REM $date $month $year AT $time MSG $original_description \%b\n"; - - # Log into remote server - - my $host = $ENV{"TW_HOOK_REMIND_REMOTE_HOST"} or die "Cannot get TW_HOOK_REMIND_REMOTE_HOST environment variable"; - my $user = "lemon"; - - my $ssh = Net::OpenSSH->new($host, user => $user); - $ssh->error and die "Couldn't establish SSH connection: " . $ssh->error; - - # Check for presece or remind file - if ($ssh->test("ls $work_rem_file") != 1) { die "Cannot find $work_rem_file on $host."}; - - # If it is there, back it up - $ssh->system("mv $work_rem_file $work_rem_file.bak"); - - # Append the Remind formatted line to the original remind file - $ssh->system({stdin_data => $remind_line}, "cat >> ~/.reminders/work.rem") or die "Cannot append text: " . $ssh->error; - - # Reset the back up - $ssh->system("mv $work_rem_file.bak $work_rem_file"); - - print encode_json $decoded_task; - -} else { - print encode_json $decoded_task; -} - - - - -- cgit v1.2.3