/
sblink.sh
executable file
·303 lines (287 loc) · 13.4 KB
/
sblink.sh
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
#!/bin/bash
#Folder in ~ for Blink auth/cred files to be saved
BLINKDIR=".sblink"
#API endpoint
URL="rest.prod.immedia-semi.com"
URL_SUBDOMAIN="prod"
TIMEZONE=":US/Pacific"
#Output directory for videos
OUTPUTDIR="/tmp/"
preReq () {
if ! [ -x "$(command -v jq)" ]; then
clear
echo
echo "Error: jq package not detected..."
echo
echo " Please install the jq package for your system:"
echo " https://stedolan.github.io/jq/ "
echo
exit
else
JQ=true
SWITCH=""
fi
}
banner () {
echo ' '
echo ' '
echo ' .--.--. ,---,. ,--, ,-. '
echo ' / / `. ,` .` \,--.`| ,--, ,--/ /| '
echo '| : /`. / ,---.` .` || | : ,--.`| ,---, ,--. :/ | '
echo '; | |--` | | |: |: : ` | |, ,-+-. / |: : ` / '
echo '| : ;_ : : : /| ` | `--`_ ,--.`|` || ` / '
echo ' \ \ `. : | ; ` | | ,` ,`| | | ,`` |` | : '
echo ' `----. \| : \| | : ` | | | | / | || | \ '
echo ' __ \ \ || | . |` : |__ | | : | | | | |` : |. \ '
echo ' / /`--` /` : `; || | `.`|` : |__ | | | |/ | | ` \ \ '
echo '`--`. / | | | ; ; : ;| | `.`|| | |--` ` : |--` '
echo ' `--`---` | : / | , / ; : ;| |/ ; |,` '
echo ' | | ,` ---`-` | , / `---` `--` '
echo ' `----` ---`-` '
echo ' '
}
credGet () {
# Read the cached URL
URL2=$(cat ~/${BLINKDIR}/url 2>/dev/null)
if [ ! "${URL2}" == "" ]; then
URL=${URL2}
fi
# Read the cached auth code
AUTHCODE=$(cat ~/${BLINKDIR}/authcode 2>/dev/null)
AUTHTEST=$(curl -s -H "Host: ${URL}" -H "TOKEN_AUTH: ${AUTHCODE}" --compressed https://${URL}/homescreen | grep -o '\"message\":\".\{0,12\}' | cut -c12-)
if [ "${AUTHTEST}" == "Unauthorized" ]; then
# Create the temp dir if nothing is cached yet
if [ ! -d ~/${BLINKDIR} ]; then
mkdir ~/${BLINKDIR}
fi
# Enter the creds
echo null > ~/${BLINKDIR}/authcode
echo Enter your username \(email\):
read EMAIL
echo
echo Enter your password:
read -s PASSWORD
# Auth the creds and cache the authcode
AUTH=$(curl -s -H "Host: ${URL}" -H "Content-Type: application/json" --data-binary '{ "password" : "'"${PASSWORD}"'", "client_specifier" : "https://github.com/se7enack/SBlink", "email" : "'"${EMAIL}"'" }' --compressed https://${URL}/login )
# Read the authcode
AUTHCODE=$(echo $AUTH | grep -o '\"authtoken\":\".\{0,22\}' | cut -c14-)
echo $AUTHCODE > ~/${BLINKDIR}/authcode
if [ "${AUTHCODE}" == "" ]; then
echo "No Authcode received, please check credentials"
exit
fi
# Read the domain, adjust and cache the URL
SUBDOMAIN=$(echo $AUTH | grep -o '\"region\":{"[^"]\+"' | cut -c12- | grep -o '[[:alnum:]]*')
URL=${URL/.${URL_SUBDOMAIN}./.${SUBDOMAIN}.}
echo $URL > ~/${BLINKDIR}/url
fi
# Query the network ID
NETWORKID=$(curl -s -H "Host: ${URL}" -H "TOKEN_AUTH: ${AUTHCODE}" --compressed https://${URL}/networks | grep -o '\"summary\":{\".\{0,6\}' | cut -c13- | grep -o '[[:digit:]]*')
echo Network ID: ${NETWORKID}
}
theMenu () {
PS3='What do you want to do? : '
options=("Download all videos" "Get network information" "Get Sync Module information" \
"Arm network" "Disarm network" "Get homescreen information" \
"Get events for network" "Capture a new thumbnail" "Capture a new video" \
"Get a total on the number of videos" "Get paginated video information" \
"Unwatched video list" "Get a list of all cameras" "Get camera information" "Get camera sensor information" \
"Enable motion detection" "Disable motion detection" "Get information about connected devices" \
"Get information about supported regions" "Get information about programs" "Quit")
select opt in "${options[@]}"
do
case $opt in
"Download all videos")
echo;echo "Download all videos"
echo "AUTHCODE = ${AUTHCODE}"
echo "URL = ${URL}"
DATE='2016-01-01T23:11:21+0000'
n=1
while : ; do
RESPONSE=$(curl -s -H "Host: ${URL}" -H "TOKEN_AUTH: ${AUTHCODE}" --compressed -X GET -G https://${URL}/api/v2/videos/changed -d since=${DATE} -d page=${n})
VIDEOS=$(echo ${RESPONSE} | jq -c '.videos[] | select(.deleted == false) | { address: .address, id: .id }')
COUNT=$(echo ${RESPONSE} | jq -c '[.videos[]] | length')
if [ $COUNT -eq 0 ]; then
echo "Done"
break
fi
for VIDEO in $VIDEOS; do
ADDRESS=$(echo $VIDEO | jq -r '.address')
ID=$(echo $VIDEO | jq -r '.id')
ADDRESS2=$( echo $ADDRESS | sed 's:.*/::' )
PAD=$(echo $ADDRESS | tr -dc '_' | awk '{ print length; }')
ADDRESS3=$(echo $ADDRESS2 | cut -d '_' -f $(($PAD-4))-99)
CAMERA=$(dirname $ADDRESS | xargs basename)
DATESTAMP=$(echo $ADDRESS3 | grep -Eo '[0-9]{1,4}' | tr -d '\n' | sed 's/.$//')
DATESTAMP2=$( TZ=${TIMEZONE} date -j -f %Y%m%d%H%M%z ${DATESTAMP}+0000 +%Y%m%d%H%M )
ADDRESS3_FILENAME="${ADDRESS3%.*}"
ADDRESS3_EXTENSION="${ADDRESS3##*.}"
ADDRESS4=${ADDRESS3_FILENAME}-${CAMERA}
ADDRESS5=${ADDRESS4}-${ID}.${ADDRESS3_EXTENSION}
ls ${OUTPUTDIR}/${ADDRESS5} &> /dev/null
if ! [ $? -eq 0 ]; then
echo "Downloading ${ADDRESS5} to ${OUTPUTDIR} with timestamp ${DATESTAMP}"
# download the file
curl -s -H "Host: ${URL}" -H "TOKEN_AUTH: ${AUTHCODE}" --compressed https://${URL}/${ADDRESS} > ${OUTPUTDIR}/${ADDRESS5}
# touch the file so it appears with the right datestamp
TZ=UTC touch -a -m -t ${DATESTAMP2} ${OUTPUTDIR}/${ADDRESS5}
# Print in green
tput setaf 2
echo "[ ** ${ADDRESS5} is new! ** ]"
# Print back in black
tput sgr0
fi
done
n=$((n+1))
done
echo "Download complete. Your videos can be found here: ${OUTPUTDIR}"
exit
;;
"Get network information")
echo;echo "Get network information"
CALL="/networks"
break
;;
"Get Sync Module information")
echo;echo "Get Sync Module information"
CALL="/network/${NETWORKID}/syncmodules"
break
;;
"Arm network")
echo;echo "Arm network ${NETWORKID}"
CALL="/network/${NETWORKID}/arm"
SWITCH="--data-binary"
break
;;
"Disarm network")
echo;echo "Disarm network ${NETWORKID}"
CALL="/network/${NETWORKID}/disarm"
SWITCH="--data-binary"
break
;;
"Get homescreen information")
echo;echo "Get homescreen information"
CALL="/homescreen"
break
;;
"Get events for network")
echo;echo "Get events for network ${NETWORKID}"
CALL="/events/network/${NETWORKID}"
break
;;
"Capture a new thumbnail")
curl -s -H "Host: ${URL}" -H "TOKEN_AUTH: ${AUTHCODE}" --compressed https://${URL}/network/${NETWORKID}/cameras | jq '.' | grep -E '"name"|"id"'
echo "Please enter the camera's ID number:"
read CAMERAID
echo "Capture a new thumbnail from camera ${CAMERAID}"
CALL="/network/${NETWORKID}/camera/${CAMERAID}/thumbnail"
SWITCH="--data-binary"
break
;;
"Capture a new video")
curl -s -H "Host: ${URL}" -H "TOKEN_AUTH: ${AUTHCODE}" --compressed https://${URL}/network/${NETWORKID}/cameras | jq '.' | grep -E '"name"|"id"'
echo "Please enter the camera's ID number:"
read CAMERAID
echo "Capture a new video from camera ${CAMERAID}"
CALL="/network/${NETWORKID}/camera/${CAMERAID}/clip"
SWITCH="--data-binary"
break
;;
"Get a total on the number of videos")
echo;echo "Get a total on the number of videos"
CALL="/api/v2/videos/count"
break
;;
"Get paginated video information")
echo;echo "Get paginated video information"
COUNT=$(curl -s -H "Host: ${URL}" -H "TOKEN_AUTH: ${AUTHCODE}" --compressed https://${URL}//api/v2/videos/count | sed -n 's/\"count"\://p' | tr -d '{}')
COUNT=$(((${COUNT} / 10)+2))
for ((n=0;n<${COUNT};n++)); do
curl -s -H "Host: ${URL}" -H "TOKEN_AUTH: ${AUTHCODE}" --compressed https://${URL}//api/v2/videos/page/${n} | jq -C
done
exit
;;
"Unwatched video list")
echo;echo "Get a list of unwatched videos"
CALL="/api/v2/videos/unwatched"
break
;;
"Get a list of all cameras")
echo;echo "Get a list of all cameras"
CALL="/network/${NETWORKID}/cameras"
break
;;
"Get camera information")
curl -s -H "Host: ${URL}" -H "TOKEN_AUTH: ${AUTHCODE}" --compressed https://${URL}/network/${NETWORKID}/cameras | jq '.' | grep -E '"name"|"id"'
echo "Please enter the camera's ID number:"
read CAMERAID
echo "Get information for camera ${CAMERAID}"
CALL="/network/${NETWORKID}/camera/${CAMERAID}"
break
;;
"Get camera sensor information")
curl -s -H "Host: ${URL}" -H "TOKEN_AUTH: ${AUTHCODE}" --compressed https://${URL}/network/${NETWORKID}/cameras | jq '.' | grep -E '"name"|"id"'
echo "Please enter the camera's ID number:"
read CAMERAID
echo "Get camera sensor information for camera ${CAMERAID}"
CALL="/network/${NETWORKID}/camera/${CAMERAID}/signals"
break
;;
"Enable motion detection")
curl -s -H "Host: ${URL}" -H "TOKEN_AUTH: ${AUTHCODE}" --compressed https://${URL}/network/${NETWORKID}/cameras | jq '.' | grep -E '"name"|"id"'
echo "Please enter the camera's ID number:"
read CAMERAID
echo "Enable motion detection for camera ${CAMERAID}"
CALL="/network/${NETWORKID}/camera/${CAMERAID}/enable"
SWITCH="--data-binary"
break
;;
"Disable motion detection")
curl -s -H "Host: ${URL}" -H "TOKEN_AUTH: ${AUTHCODE}" --compressed https://${URL}/network/${NETWORKID}/cameras | jq '.' | grep -E '"name"|"id"'
echo "Please enter the camera's ID number:"
read CAMERAID
echo "Disable motion detection for camera ${CAMERAID}"
CALL="/network/${NETWORKID}/camera/${CAMERAID}/disable"
SWITCH="--data-binary"
break
;;
"Get information about connected devices")
echo;echo "Get information about connected devices"
CALL="/account/clients"
break
;;
"Get information about supported regions")
echo;echo "Get information about supported regions"
CALL="/regions"
break
;;
"Get information about programs")
echo;echo "Get information about programs"
CALL="/api/v1/networks/${NETWORKID}/programs"
break
;;
"Quit")
exit
;;
*) echo invalid option;;
esac
done
}
while [ true ]; do
preReq;credGet;banner;theMenu
if [ ${JQ} == true ]; then
clear
echo
curl -s -H "Host: ${URL}" -H "TOKEN_AUTH: ${AUTHCODE}" ${SWITCH} --compressed https://${URL}${CALL} | jq -C
echo
echo
else
clear
echo
curl -s -H "Host: ${URL}" -H "TOKEN_AUTH: ${AUTHCODE}" ${SWITCH} --compressed https://${URL}${CALL}
echo
echo
fi
read -p "Press any key..."
clear
done