forked from Red5d/pushbullet-bash
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpushbullet
executable file
·260 lines (226 loc) · 6.24 KB
/
pushbullet
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
#!/bin/bash
# Bash interface to the PushBullet api.
# Original Author: Red5d - https://github.com/Red5d
# Author: Sean Payne - https://github.com/zerodivide1
if [[ -z "$CONFIG" ]]
then
CONFIG=${XDG_CONFIG_HOME:-$HOME/.config}/pushbullet
fi
API_URL=https://api.pushbullet.com/v2
source $CONFIG
die() {
echo $1
exit 1
}
printUsage() {
echo "Usage: pushbullet <action> <device> <type> <data>
Actions:
list - Lists all devices in your PushBullet account. (does not require additional parameters)
recent - Retrieves recent pushes (optional type as argument; returns newest since last zero-arg invocation)
push - Pushes data to a device. (the device name can simply be a unique part of the name that \"list\" returns)
get - Retrieve a specific push (requires the iden of the push as parameter)
delete - Delete a specific push (requires the iden of the push as a parameter)
stream - Stream and execute given command for each new push
Types:
note
address
list
file
link
Type Parameters:
(all parameters must be put inside quotes if more than one word)
\"note\" type: give the title, then the note text.
\"address\" type: give the address name, then the address or Google Maps query.
\"link\" type: give the title of the link, then the url.
Streaming Info:
Command to execute given as subsequent params.
Executes the command on every new push since the last time 'recent' was used.
Iden of the push is provided as the last parameter
Ex.
pushbullet monitor echo
"
}
if [ "$1" = "" ];then
if [ "$API_KEY" = "" ];then
echo -e "\e[0;33mWarning, your API key is not set.\nPlease create $CONFIG with a line starting with API_KEY= and your PushBullet key\e[00m"
fi
printUsage
exit
fi
getdevices() {
curl -s "$API_URL/devices" -u $API_KEY: | jq ".devices"
}
dopush() {
curl_flag="$1"
shift
device_id="$1"
shift
push_type="$1"
shift
args=()
while [ "$1" ]
do
arg_name="$1"
shift
arg_value="$1"
shift
args+=(-$curl_flag "$arg_name"="$arg_value")
done
if [ -z "$device_id" ]
then
curl_result=$( curl -qSfsw '\n%{http_code}' "$API_URL/pushes" -u $API_KEY: -X POST -$curl_flag type=$push_type "${args[@]}" ) 2>/dev/null
else
curl_result=$( curl -qSfsw '\n%{http_code}' "$API_URL/pushes" -u $API_KEY: -X POST -$curl_flag device_iden=$device_id -$curl_flag type=$push_type "${args[@]}" ) 2>/dev/null
fi
curl_result_code=$(echo "$curl_result" | tail -n1)
curl_result_body=$(echo "$curl_result" | head -n-1)
case "$curl_result_code" in
"200")
push_id=$(echo "$curl_result_body" | jq -r ".iden")
echo "OK $push_id"
;;
*)
echo "Failed:"
echo "$curl_result_body"
;;
esac
}
case $1 in
list)
echo "Available devices (use empty string in quotes to push to all devices):"
echo "----------------------------------------------------------------------"
getdevices | jq -r '.[].nickname'
;;
push)
if [ -z "$2" ]
then
dev_id=""
else
dev_id=$(getdevices | jq ".[] | select(.nickname==\"$2\")" | jq -r ".iden")
fi
case $3 in
note)
note_result=$(dopush d "$dev_id" note title "$4" body "$5")
echo "$note_result"
;;
address)
address_result=$(dopush d "$dev_id" address name "$4" address "$5")
echo "$address_result"
;;
link)
link_result=$(dopush d "$dev_id" link title "$4" url "$5")
echo "$link_result"
;;
esac
;;
recent)
if [[ -z "$LASTMODIFIED" ]]
then
LASTMODIFIED=0
fi
curl_result=$( curl -qSfsw '\n%{http_code}' "$API_URL/pushes?modified_after=$LASTMODIFIED" -u $API_KEY: ) 2>/dev/null
curl_result_code=$(echo "$curl_result" | tail -n1)
curl_result_body=$(echo "$curl_result" | head -n-1)
case "$curl_result_code" in
"200")
LASTMODIFIEDTEMP=$(echo "$curl_result_body" | jq -r '.pushes[].modified' | sort -r | head -1)
case "$2" in
"note")
pushes=$(echo "$curl_result_body" | jq ".pushes | map(select(.type==\"note\")) | [.[] | {iden,title,body}]")
;;
"link")
pushes=$(echo "$curl_result_body" | jq ".pushes | map(select(.type==\"link\")) | [.[] | {iden,title,body,url}]")
;;
"list")
pushes=$(echo "$curl_result_body" | jq ".pushes | map(select(.type==\"list\")) | [.[] | {iden,title,items}]")
;;
"address")
pushes=$(echo "$curl_result_body" | jq ".pushes | map(select(.type==\"address\")) | [.[] | {iden,name,address}]")
;;
"file")
pushes=$(echo "$curl_result_body" | jq ".pushes | map(select(.type==\"file\")) | [.[] | {iden,file_name,file_type,file_url,body}]")
;;
*)
pushes=$(echo "$curl_result_body" | jq ".pushes | map(select(.type==\"note\" or .type==\"link\" or .type==\"list\" or .type==\"address\" or .type==\"file\")) | [.[] | {iden,type,title,name,file_name}]")
if [[ -z "$LASTMODIFIEDTEMP" ]]
then
echo "API_KEY=$API_KEY
LASTMODIFIED=$LASTMODIFIED" > $CONFIG
else
echo "API_KEY=$API_KEY
LASTMODIFIED=$LASTMODIFIEDTEMP" > $CONFIG
fi
;;
esac
echo "$pushes"
;;
*)
echo "Failed:"
echo "$curl_result_body"
;;
esac
;;
get)
if [[ -z "$2" ]]
then
printUsage
exit
fi
curl_result=$( curl -qSfsw '\n%{http_code}' "$API_URL/pushes?modified_after=0" -u $API_KEY: ) 2>/dev/null
curl_result_code=$(echo "$curl_result" | tail -n1)
curl_result_body=$(echo "$curl_result" | head -n-1)
case "$curl_result_code" in
"200")
push=$(echo "$curl_result_body" | jq ".pushes | map(select(.iden==\"$2\"))")
echo "$push"
;;
*)
echo "Failed:"
echo "$curl_result_body"
;;
esac
;;
delete)
if [[ -z "$2" ]]
then
printUsage
exit
fi
curl_result=$( curl -qSfsw '\n%{http_code}' "$API_URL/pushes/$2" -u $API_KEY: -X DELETE ) 2>/dev/null
curl_result_code=$(echo "$curl_result" | tail -n1)
curl_result_body=$(echo "$curl_result" | head -n-1)
case "$curl_result_code" in
"200")
echo "OK:"
;;
*)
echo "Failed:"
;;
esac
echo "$curl_result_body"
;;
stream)
which wssh > /dev/null || die "wssh not installed: https://github.com/progrium/wssh"
if [[ -z "${@:2}" ]]
then
die "No command to execute specified for new items"
fi
wsshCmd=$(which wssh)
$wsshCmd -n wss://stream.pushbullet.com/websocket/$API_KEY | while read -r line ; do
MSGTYPE=$(echo $line | jq -r '.type')
if [[ "$MSGTYPE" == "tickle" ]]
then
SUBTYPE=$(echo $line | jq -r '.subtype')
if [[ "$SUBTYPE" == "push" ]]
then
$0 recent | jq -c -M -r '.[].iden' | while read -r pushIden ; do
${@:2} $pushIden
done
fi
fi
done
;;
*)
printUsage
;;
esac