-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpiny
executable file
·139 lines (105 loc) · 2.85 KB
/
piny
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
#! /bin/sh -e
user_token=`cat $HOME/.pebble_timeline_token`
timeline_url="https://timeline-api.getpebble.com/v1/user/pins"
# defualt pin ID (as unique as possible)
pin_id="T:`date '+%s_%N'`"
# ------------------------------------------------------------------------------
# take yaml as input and send to timeline API
send_pin()
{
input=`cat /dev/stdin`
# grap the pin ID from the yaml input stream
local pin_id=`echo "$input" | awk '/id/ {print $2}'`
echo "$input" | _send_pin $pin_id
}
# take yaml as input and pin ID as arg1 and send to timeline API
_send_pin()
{
local pin_id=$1
## debug
#yaml2json /dev/stdin
#return 1
rsp=`yaml2json /dev/stdin | curl -s -X PUT "$timeline_url/$pin_id" \
--header "Content-Type: application/json" \
--header "X-User-Token: $user_token" \
-d @- `
# handle errors
if ! [ "$rsp" = "OK" ]
then
echo "$rsp"
return 1
fi
}
# ------------------------------------------------------------------------------
# take pin ID as arg1 and send deletion request to timeline API
delete_pin()
{
local pin_id=$1
curl -s -X DELETE "$timeline_url/$pin_id" \
--header "X-User-Token: $user_token"
}
# ------------------------------------------------------------------------------
piny()
{
title="$*"
time=`date -Iseconds`
body=`cat /dev/stdin`
send_pin << pin.yml
id: $pin_id
time: "$time"
layout:
type: genericPin
title: "$title"
body: "$body"
pin.yml
}
# ------------------------------------------------------------------------------
piny_notify()
{
title="$*"
time=`date -Iseconds`
body="`cat /dev/stdin`"
send_pin << pin.yml
id: $pin_id
time: "$time"
layout:
type: genericPin
title: "$title"
body: "$body"
createNotification:
layout:
type: genericNotification
title: "$title"
body: "$body"
pin.yml
}
# ------------------------------------------------------------------------------
piny_remind()
{
time="$1"; shift
title="$*"
# if time is before now, assume user means that time tomorrow
if test $(date -d "$time" +%s) -lt $(date +%s)
then
time=$(date -Iseconds -d "$time + 1day")
else
time=$(date -Iseconds -d "$time")
fi
send_pin << pin.yml
id: $pin_id
time: "$time"
layout:
type: genericPin
title: "$title"
tinyIcon: system://images/NOTIFICATION_REMINDER
reminders:
- time: "$time"
layout:
type: "genericReminder"
title: "$title"
tinyIcon: system://images/NOTIFICATION_REMINDER
pin.yml
}
# ------------------------------------------------------------------------------
# if called as "piny.foo, call internal function piny_foo()"
`basename $0 | sed 's/\./_/'` "$@"