forked from Red5d/pushbullet-bash
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpushbullet
More file actions
executable file
·196 lines (163 loc) · 4.93 KB
/
pushbullet
File metadata and controls
executable file
·196 lines (163 loc) · 4.93 KB
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
#! /bin/bash
# Bash interface to the PushBullet api.
# Author: Red5d - https://github.com/Red5d
CONFIG=~/.config/pushbullet
API_URL=https://api.pushbullet.com/v2
source $CONFIG
printUsage() {
echo "Usage: pushbullet <action> <device> <type> <data>
Actions:
list - Lists all devices in your PushBullet account. (does not require
additional parameters)
push - Pushes data to a device. (the device name can simply be a unique part of
the name that \"list\" returns)
pushes active - Get your 'active' pushes (pushes that haven't been deleted)
delete - Deletes a push, you must give the push 'iden', see https://docs.pushbullet.com/v2/pushes/
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. The note text can also be
given via stdin, leaving the note text field empty.
\"address\" type: give the address name, then the address or Google Maps query.
\"list\" type: give the name of the list, then each of the list items,
separated by spaces.
\"file\" type: give the path to the file.
\"link\" type: give the title of the link, then the url.
"
}
function getactivepushes () {
allpushes=$(./JSON.sh -l)
activepushes=$(echo "$allpushes" | egrep "\[\"pushes\",[0-9]+,\"active\"\].*true"|while read line; do echo "$line"|cut -f 2 -d ,; done)
for id in $activepushes
do
iden=$(echo "$allpushes" | grep "^\[\"pushes\",$id,\"iden\"\]"|cut -f 2)
title=$(echo "$allpushes" | grep "^\[\"pushes\",$id,\"title\"\]"|cut -f 2)
if [ -z $title ]; then
echo "(no title) $iden"
else
echo "$title $iden"
fi
done
}
function deletepush () {
curlres=$(curl -s "$API_URL/pushes/$1" -u $API_KEY: -d device_iden=$dev_id -d active="false" -X POST)
checkCurlOutput "$curlres"
}
checkCurlOutput() {
res=$(echo "$1" | grep -o "created" | tail -n1)
if [[ "$res" != "created" ]]; then
echo "Error submitting the request. The POST error message was:"
fi
echo $res
}
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
case $1 in
list)
echo "Available devices:"
echo "------------------"
curl -s "$API_URL/devices" -u $API_KEY: | tr ',' '\n' | grep model | cut -d '"' -f4
echo "all"
;;
pushes)
case $2 in
active)
echo "Your active pushes:"
echo "------------------"
curl -s "$API_URL/pushes" -u $API_KEY: | getactivepushes
;;
esac
;;
delete)
if [ -z "$2" ]; then
printUsage
exit
fi
curlres=$(curl -s "$API_URL/pushes/$2" -u $API_KEY: -X DELETE)
if [ "$curlres" == '{}' ]; then
echo "ok"
fi
;;
push)
if [ -z "$2" ]; then
printUsage
exit
fi
devices=$(curl -s "$API_URL/devices" -u $API_KEY: | tr '{' '\n' | tr ',' '\n' | grep model | cut -d'"' -f4)
idens=$(curl -s "$API_URL/devices" -u $API_KEY: | tr '{' '\n' | tr ',' '\n' | grep iden | cut -d'"' -f4)
lineNum=$(echo "$devices" | grep -i -n "$2" | cut -d: -f1)
dev_id=$(echo "$idens" | sed -n $lineNum'p')
case $3 in
note)
body=""
if [ ! -t 0 ]; then
# we have something on stdin
body=$(cat)
# remove unprintable characters, or pushbullet API fails
body=$(echo "$body"|tr -dc '[:print:]\n')
fi
if [ ! -z "$5" ]; then
body="$5"
fi
if [ "$2" = "all" ]; then
curlres=$(curl -s "$API_URL/pushes" -u $API_KEY: -d type=note -d title="$4" --data-urlencode body="$body" -X POST)
else
curlres=$(curl -s "$API_URL/pushes" -u $API_KEY: -d device_iden=$dev_id -d type=note -d title="$4" --data-urlencode body="$body" -X POST)
fi
checkCurlOutput "$curlres"
;;
address)
curlres=$(curl -s "$API_URL/pushes" -u $API_KEY: -d device_iden=$dev_id -d type=address -d name="$4" -d address="$5" -X POST)
checkCurlOutput "$curlres"
;;
list)
argnum=0
for i in $@
do
let argnum=$argnum+1
if [ $argnum -ge 5 ];then
itemlist=$itemlist" -d items=$i"
fi
done
if [ "$2" = "all" ]; then
curlres=$(curl -s $API_URL/pushes -u $API_KEY: -d type=list -d title=$4 $itemlist -X POST)
else
curlres=$(curl -s $API_URL/pushes -u $API_KEY: -d device_iden=$dev_id -d type=list -d title=$4 $itemlist -X POST)
fi
checkCurlOutput "$curlres"
;;
file)
if [ "$2" = "all" ]; then
curlres=$(curl -s "$API_URL/pushes" -u $API_KEY: -F type=file -F file="@$4" -X POST)
else
curlres=$(curl -s "$API_URL/pushes" -u $API_KEY: -F device_iden=$dev_id -F type=file -F file="@$4" -X POST)
fi
checkCurlOutput "$curlres"
;;
link)
if [ "$2" = "all" ]; then
curlres=$(curl -s "$API_URL/pushes" -u $API_KEY: -d type=link -d title="$4" -d url="$5" -X POST)
else
curlres=$(curl -s "$API_URL/pushes" -u $API_KEY: -d device_iden=$dev_id -d type=link -d title="$4" -d url="$5" -X POST)
fi
checkCurlOutput "$curlres"
;;
*)
printUsage
;;
esac
;;
*)
printUsage
;;
esac