/
rtm
executable file
·825 lines (640 loc) · 20.3 KB
/
rtm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
#! /usr/bin/perl -w
# Command-line interface to www.rememberthemilk.com
# Author: Chris Miller (chrisamiller@gmail.com)
#
# Based loosely on rtm by Yves Rutschle (http://www.rutschle.net/rtm/)
#
# This program is free software; you can redistribute it
# and/or modify it under the terms of the GNU General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
# PURPOSE. See the GNU General Public License for more
# details.
#
# The full text for the General Public License is here:
# http://www.gnu.org/licenses/gpl.html
#libs, etc
use WebService::RTMAgent;
use Pod::Usage;
use Getopt::Long;
use Date::Manip;
use DateTime;
use Data::Dumper;
use strict;
my $ua = new WebService::RTMAgent;
$ua->verbose(''); # /netout|netin/
# These are mine! Please request your own keys if you're going to build an
# application on top of the RTM module. Otherwise (if you use this rtm script)
# just leave them as is.
$ua->api_key("149ce058a1667e914ff370c0e565d77b");
$ua->api_secret("40963b4c7dc48eae");
$ua->no_proxy();
$ua->env_proxy;
my $res;
my $verbose = 1;
#------------------------------------------------
# prints full usage information
#
sub usage{
print "
Access rememberthemilk.com via the command line
This script isn't meant to implement a complete RTM feature
set, just to make the four or five things I do often easy.
It supports only a single list and a basic set of functions.
Setting task priorities, for example, isn't supported.
COMMAND LINE USAGE:
rtm authorize - prints an authorization URL. This is the
first thing you should do before using this
program. You should then go to the specified
URL, log in using your username and password,
and authorize this program to use your data.
This only need to be done once.
rtm add <task>
rtm a <task> - quick add a task
rtm today - prints just tasks due today
rtm help - lists this screen
INTERACTIVE USAGE:
(a)dd - add a task. Parses the task name for
dates automatically.
(c)omplete - complete a task
(r)emove - remove a task from the list
(p)ostpone - postpones a task to one day later
(d)ueDate - change the due date of a task
(l)ist - reload the entire list from the server
(q) quit - quit
Not implemented yet:
uncomplete?
filter?
Configuration Notes:
rtm can be configured to use a proxy server. Simply
define environment variables 'https_proxy' and 'http_proxy'.
As this is pretty standard, your system might already be
configured accordingly.
SEE ALSO
<WebService::RTMAgent.pm>, which implements the rtm API.
AUTHOR
Chris Miller <chrisamiller\@gmail.com>
CREDITS
Based loosely on rtm by Yves Rutschle (http://www.rutschle.net/rtm/)
";
}
#-----------------------------------------------------
# In interactive mode, gives a quick list of commands
#
sub quickHelp{
print "
(a)dd - add a task. Parses the task name for
dates automatically.
(c)omplete - complete a task
(r)emove - remove a task from the list
(p)ostpone - postpones a task to one day later
(d)ueDate - change the due date of a task
(l)ist - reload the entire list from the server
(q) quit - quit
Hit any key to continue\;
";
}
#-----------------------------------------------------
# prints an authorization URL
sub authorize{
print($ua->get_auth_url."\n");
exit 0;
}
#-----------------------------------------------------
# Takes a filter and the last sync date as inputs,
#
# returns a list of tasks that have changed or been
# added since the last sync date.
#
sub getTaskList {
my ($filter,$lastsync) = @_;
$res = $ua->tasks_getList($filter,$lastsync);
die $ua->error unless defined $res;
my @out;
# for each list
foreach my $list (@{$res->{tasks}}) {
# for each task series
foreach my $taskseries (@{$list->{list}}) {
my $list_id = $taskseries->{id};
next unless exists $taskseries->{taskseries};
# for each task
foreach my $task (@{$taskseries->{taskseries}}) {
my $taskseries_id = $task->{id};
my $task_id = $task->{task}->[0]->{id};
my $dueDate = $task->{task}->[0]->{due};
# if no due date, set it to some unreasonable value so that
# a simple numerical sort will give us tasks without dates
# at the end of the list
if ($dueDate eq ""){
$dueDate = 999999999999999;
}
else{
$dueDate = UnixDate($dueDate,"%s");
}
#push task details onto an array
push @out, {
list_id =>$list_id,
taskseries_id => $taskseries_id,
task_id => $task_id,
name => $task->{name},
dueDate => $dueDate,
task => $task, # Keep a reference to the whole data
};
}
}
}
#sort by due date, then name, return a hash with numeric keys in sorted order
my $cnt = 0;
return map { $cnt++, $_ } sort { $a->{dueDate} <=> $b->{dueDate} || $a->{name} cmp $b->{name} } @out;
}
#-----------------------------------------------------
# Retrieve the list of possible lists
# returns a hash of number => list hash
# with number guaranteed to be consistent (sorted by list_id)
#
sub list_lists {
my $res = $ua->lists_getList;
die $ua->error if not defined $res;
my $cnt = 0;
return map { $cnt++, $_ } sort { $a->{id} <=> $b->{id} }
@{$res->{lists}->[0]->{list}};
}
#-----------------------------------------------------
# Displays the nicely formatted task list
#
sub showList{
clearScreen();
my (%list) = @_;
my $prevDate = 0;
my @asdf = keys(%list);
# for each item in the list, sorted by hash key
foreach my $i (sort{$a <=> $b} keys %list) {
my $dueDate = $list{$i}->{dueDate};
#convert blank dueDates to blanks
if ($dueDate == 999999999999999){
$dueDate = "___"
}
else{ #convert from unix dueDates to "Month Day"
$dueDate = UnixDate(ParseDate("epoch $dueDate"),"%b %e");
}
#spacing between days
unless ("$prevDate" eq "$dueDate"){
print "----------------------\n" ;
}
print "$i:\t" . padName($list{$i}->{name}) . "\t" . $dueDate . "\n";
$prevDate = $dueDate
}
print "\n" #some trailing space for CLI
}
#-----------------------------------------------------
# pads (or truncates) a string to a set number of
# characters for nice display
#
sub padName {
my $name=shift;
my $len = 50;
if (length($name) < $len){
$name = sprintf("%-${len}s", $name);
}
elsif (length($name) > $len) {
$name = substr($name,0,$len);
}
return $name;
}
#-----------------------------------------------------
# Turns "1,4,8-12" into a list [1, 4, 8, 9, 11, 12];
# allows for multiple tasks to be altered at once
sub expandList {
return map { /(\d+)-(\d+)/ ? ($1..$2): $_; } split ',',$_[0];
}
#-----------------------------------------------------
# after a task is added, query RTM to get an updated list.
# This requires more syncs, but allows us to use the RTM
# server's excellent date parsing
#
sub updateTaskListPostAdd{
my ($filter,$lastSync,%list) = @_;
#get the new tasks
my %newTasks = getTaskList($filter,"last_sync=$lastSync");
print Dumper(%newTasks) ."\n" if $verbose;
#toss the old and new values into an array
my @vals = values(%list);
while (my ($key, $val) = each %newTasks) {
push(@vals, $val);
}
#sort by due date, then name, return a hash with numeric keys in sorted order
my $cnt = 0;
return map { $cnt++, $_ } sort { $a->{dueDate} <=> $b->{dueDate} || $a->{name} cmp $b->{name} } @vals;
}
#-----------------------------------------------------
# after a alteration, update the list we have
# locally - no need to do a server query
#
sub updateTaskListPostAlter{
my ($taskNum,$action,$filter,$lastSync,%list) = @_;
# delete and complete are functionally the same,
# here since they won't show up in the normal list
if ($action eq "delete" || $action eq "complete"){
delete($list{$taskNum});
}
#query the server, get the changed task, update the list
elsif ($action eq "postpone" || $action eq "date" || $action eq "uncomplete"){
#get the changes
my %newTasks = getTaskList($filter,"last_sync=$lastSync");
print Dumper(%newTasks) ."\n" if $verbose;
#for each change, find it's match and update the hash
while (my ($key, $val) = each %newTasks) {
if ($val->{'taskseries_id'} eq $list{$taskNum}{'taskseries_id'}){
$list{$taskNum} = $val;
}
}
}#end elsif
#renumber list after updates have been applied
my $cnt = 0;
my @vals = values(%list);
return map { $cnt++, $_ } sort { $a->{dueDate} <=> $b->{dueDate} || $a->{name} cmp $b->{name} } @vals;
}
#-----------------------------------------------------
# Adds a task to given list
# takes 1 argument: name of task to add
#
sub addTask{
my ($name) = @_;
# print "adding: $name\n";
my $res = $ua->tasks_add("name=$name","parse=1","");
die $ua->error unless defined $res;
print "Added new task: \"$name\"\n";
}
#----------------------------------------------------
# Changes the date of the specified task
#
sub setDueDate{
my ($taskList,$date,%list) = @_;
my $action = "tasks_setDueDate";
#for each task
foreach my $taskNum (expandList $taskList) {
my %task = %{$list{$taskNum}};
my ($lid, $tsid, $id, $name) =
@task{'list_id', 'taskseries_id', 'task_id', 'name'};
no strict 'refs';
my $res = $ua->$action("list_id=$lid","taskseries_id=$tsid","task_id=$id","due=$date","parse=1");
warn $ua->error if not defined $res;
print "Date of \"$name\" changed to $date\n"
}
}
#-----------------------------------------------------
# alters the task in the specified manner
# (delete, postpone, complete, uncomplete)
#
sub alterTask{
my ($action,$taskList,$message,%list) = @_;
#for each task
foreach my $taskNum (expandList $taskList) {
die "task $taskNum does not exist\n" if not exists $list{$taskNum};
# print "$taskNum\n"
my %task = %{$list{$taskNum}};
my ($lid, $tsid, $id, $name) =
@task{'list_id', 'taskseries_id', 'task_id', 'name'};
no strict 'refs';
my $res = $ua->$action("list_id=$lid","taskseries_id=$tsid","task_id=$id");
warn $ua->error if not defined $res;
print "$message '$name'\n"
}
}
#-----------------------------------------------------
# Should provide cross-platform clearing of the screen.
# (haven't tested it on windows yet)
#
sub clearScreen{
if ($^O =~ /MSWin32/) {
#windows
system("cls");
}
else {
#*nix
system("clear");
}
}
#-----------------------------------------------------
# print interactive prompt, return input
#
sub iPrompt(){
print "---------------------------------------------------------------------\n";
print " (a)dd (r)emove (c)omplete (p)ostpone (d)ate (h)elp (q)uit\n\n";
print ": ";
my $input = <STDIN>;
chomp($input);
return $input
}
#-----------------------------------------------------
# prompts for task number to alter, calls alterTask to
# make the update, returns list and new sync date
#
sub interactiveAlter{
my ($action,$filter,$lastSync,%list) = @_;
my $message = "";
#rest are simple, but change 'delete' to 'remove'
if ($action eq "delete"){
print "task to remove: ";
$message = "deleted";
}else{
print "task to $action: ";
$message = $action . "d";
}
my $taskNum = <STDIN>;
chomp($taskNum);
alterTask("tasks_$action", expandList($taskNum), $message, %list);
%list = updateTaskListPostAlter($taskNum,$action,$filter,$lastSync,%list);
if ($action eq "postpone" || $action eq "uncomplete"){
$lastSync = DateTime->now;
}
return ($lastSync,%list);
}
#-----------------------------------------------------
# prompts for name to add, calls addTask to add it to
# the list
#
sub interactiveAdd{
my ($filter,$lastSync,%list) = @_;
print "task to add: ";
my $taskName = <STDIN>;
chomp($taskName);
print "adding: $taskName \n" if $verbose;
addTask($taskName);
%list = updateTaskListPostAdd($filter,$lastSync,%list);
$lastSync = DateTime->now;
return ($lastSync,%list);
}
#-----------------------------------------------------
# prompts for the task to change, then alters the due date
# uses RTM server's date parsing
#
sub interactiveDate{
my ($filter,$lastSync,%list) = @_;
print "task to change due date of: ";
my $taskNum = <STDIN>;
chomp($taskNum);
print "new due date: ";
my $newDate = <STDIN>;
chomp($newDate);
print "changing date to $newDate \n" if $verbose;
setDueDate($taskNum,$newDate,%list);
%list = updateTaskListPostAlter($taskNum,"date",$filter,$lastSync,%list);
$lastSync = DateTime->now;
return ($lastSync,%list);
}
# =item B<--undo> [I<action>]
# If no parameter is given, prints a list of the actions that
# can be undone. If a parameter is given, it is the number of
# the action to be undone, as found in that list. (just try
# it, it's quite intuitive really).
# =cut
sub undo{
#get the list of items we can undo
my $list = $ua->get_undoable;
my $cnt = 0;
my %undoHash;
clearScreen();
print "Recent actions that can be undone:\n" ;
print "(most recent at bottom of list)\n\n";
# parse the undoable actions
foreach my $item (@$list){
#parse out the action name ("add", "postpone", etc)
my $action = $item->{op};
$action =~ s/rtm\.tasks\.//;
#print a number and the action
print $cnt . ":\t" . $action;
#if the task was an add, I can print the name
#of the added task too
if ($action eq "add"){
my @arr = @{$item->{params}};
my $name = "";
foreach my $param (@arr){
if ($param =~ /name=(.+)/){
$name = $1;
}
}
print "\t" . $name
}
#if the action was a date set, I can print the
#date set by that action
elsif($action eq "setDueDate"){
my @arr = @{$item->{params}};
my $date = "";
foreach my $param (@arr){
if ($param =~ /due=(.+)/){
$date = $1;
}
}
print "\t" . $date
}
# if it's something else (like postpone), I have
# no info useful to the user (just task IDs, etc)
print "\n";
#create the commands, put them in a hash
# my $params = join(", ",@{$item->{params}});
# my $cmd = $item->{op} . "(" . $params . ")";
$undoHash{$cnt} = $item->{id};
$cnt++;
}
#prompt for the item to undo
$cnt = $cnt-1;
print "item to undo (default $cnt) ";
print "or 'c' to cancel: ";
my $input = <STDIN>;
chomp($input);
#if no item is given, undo the latest activity
if ($input eq "c"){
return;
}
elsif($input eq ""){
$input = $cnt;
}
#finally, perform the undo
my $id = $undoHash{"$input"};
# die "action $input not found\n" unless $act->{id};
$ua->transactions_undo("transaction_id=$id}") or die $ua->error;
$ua->clear_undo($input);
}
#####################################################
#interactive loop. shows list, allows user to act upon it
sub mainLoop {
my $filter = shift(@_);
my $quit = 0;
my $action = "list";
my $putList = 1;
my %list;
my $lastSync = "";
while ($quit == 0){
# check the action
unless ($action eq ""){
# print "checking for action $action\n" if $verbose;
#remove a task
if (($action eq "remove") || ($action eq "r")){
($lastSync, %list) = interactiveAlter("delete",$filter,$lastSync,%list);
$action = "";
}
#complete a task
elsif (($action eq "complete") || ($action eq "c")){
($lastSync, %list) = interactiveAlter("complete",$filter,$lastSync,%list);
$action = "";
}
#uncomplete a task
# elsif (($action eq "uncomplete")){
# ($lastSync, %list) = interactiveAlter("uncomplete",$filter,$lastSync,%list);
# $action = "";
# }
#postpone a task
elsif (($action eq "postpone") || ($action eq "p")){
($lastSync, %list) = interactiveAlter("postpone",$filter,$lastSync,%list);
$action = "";
}
#add a task
elsif (($action eq "add") || ($action eq "a")){
($lastSync, %list) = interactiveAdd($filter,$lastSync,%list);
$action = "";
}
#change the due date of a task
elsif (($action eq "dueDate") || ($action eq "d") || ($action eq "date")){
($lastSync, %list) = interactiveDate($filter,$lastSync,%list);
$action = "";
}
#undo something
elsif (($action eq "undo") || ($action eq "u")){
undo();
#safest just to refresh the whole list after an undo
%list = getTaskList($filter,"");
$lastSync = DateTime->now;
$action = "";
}
#refresh the entire list
elsif (($action eq "list") || ($action eq "l")){
%list = getTaskList($filter,"");
# showList(%list);
$lastSync = DateTime->now;
$action = "0"
}
#quit
elsif (($action eq "help") || ($action eq "h")){
quickHelp();
my $input = <STDIN>; #any key to continue
$action = "";
}
#quit
elsif (($action eq "quit") || ($action eq "q")){
$quit = 1;
exit;
}
#invalid input
else{
print "invalid action\n";
}
}
showList(%list);
$action = iPrompt();
}#end while
}#end mainLoop
#####################################################
# #list only today's tasks
# elsif ($arg0 eq "today"){
# showList($arg0);
# }
# #show list with provided filter applied
# elsif ($arg0 eq "filter"){
# my $filter = $ARGV[0];
# foreach my $num (1 .. $#ARGV) {
# $filter = $filter . " " . $ARGV[$num];
# }
# showList($filter);
# }
# =item B<--undo> [I<action>]
# If no parameter is given, prints a list of the actions that
# can be undone. If a parameter is given, it is the number of
# the action to be undone, as found in that list. (just try
# it, it's quite intuitive really).
# =cut
# if (defined $param_undo) {
# my $list = $ua->get_undoable;
# if ($param_undo) {
# my $t = $list->[$param_undo];
# die "action $param_undo not found\n" unless $t->{id};
# $ua->transactions_undo("transaction_id=$t->{id}") or die $ua->error;
# $ua->clear_undo($param_undo);
# } else {
# my $cnt = 0;
# map { print($cnt++, ": ", $_->{op}, "(",
# (join ", ",
# grep { /=/ } @{$_->{params}})
# .")\n");
# } @$list;
# }
# }
# end subs
##############################################################
#begin main
#if parameters are passed
if (@ARGV){
my $action = shift(@ARGV);
# print auth URL
if ($action eq "authorize"){
authorize();
exit 0;
}
# print usage information
if (($action eq "h") || ($action eq "help") || ($action eq "-h") || ($action eq "--help") || ($action eq "usage")){
usage();
exit 0;
}
$ua->init;
# quick add a task
if (($action eq "add") || ($action eq "a")){
my $taskName = "";
#cat arguments into one big string
foreach my $num (0 .. $#ARGV) {
$taskName = $taskName . " " . $ARGV[$num];
}
addTask($taskName);
exit;
}
# quick list of today's tasks
elsif (($action eq "today") || ($action eq "t")){
my %list = getTaskList("filter=due:today","");
showList(%list);
exit;
}
elsif (($action eq "list") || ($action eq "l")){
my %list = getTaskList("filter=status:incomplete","");
showList(%list);
exit;
}
# # list with filter
# elsif (($action eq "filter") || ($action eq "f")){
# #cat arguments into one big string
# my $filter = "";
# foreach my $num (1 .. $#ARGV) {
# $filter = $filter . " " . $ARGV[$num];
# }
# if ($filter =~ /filter=.+\:.+/){
# mainLoop($filter);
# }
# else{
# die "\nError: filter format must be 'filter=operator:value'\nsee: http://www.rememberthemilk.com/help/answers/search/advanced.rtm\n\n";
# }
# }
# some other params that aren't recognized
# ignore them and enter interactive mode
else{
mainLoop("filter=status:incomplete");
}
}
#no arguments given, interactive mode
else{
$ua->init;
mainLoop("filter=status:incomplete");
exit;
}