This repository has been archived by the owner on Dec 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
castblock
executable file
·278 lines (229 loc) · 7.74 KB
/
castblock
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
#!/bin/bash
# set -x;
[ -e "/tmp/castblock-updates" ] && rm "/tmp/castblock-updates";
mkfifo "/tmp/castblock-updates";
touch "/tmp/castblock-state";
function cleanup () {
rm -f "/tmp/castblock-updates";
rm -f "/tmp/castblock-state";
exit 0;
}
function printTable()
{
local -r delimiter="${1}"
local -r data="$(removeEmptyLines "${2}")"
if [[ "${delimiter}" != '' && "$(isEmptyString "${data}")" = 'false' ]]
then
local -r numberOfLines="$(wc -l <<< "${data}")"
if [[ "${numberOfLines}" -gt '0' ]]
then
local table=''
local i=1
for ((i = 1; i <= "${numberOfLines}"; i = i + 1))
do
local line=''
line="$(sed "${i}q;d" <<< "${data}")"
local numberOfColumns='0'
numberOfColumns="$(awk -F "${delimiter}" '{print NF}' <<< "${line}")"
# Add Line Delimiter
if [[ "${i}" -eq '1' ]]
then
table="${table}$(printf '%s#+' "$(repeatString '#+' "${numberOfColumns}")")"
fi
# Add Header Or Body
table="${table}\n"
local j=1
for ((j = 1; j <= "${numberOfColumns}"; j = j + 1))
do
table="${table}$(printf '#| %s' "$(cut -d "${delimiter}" -f "${j}" <<< "${line}")")"
done
table="${table}#|\n"
# Add Line Delimiter
if [[ "${i}" -eq '1' ]] || [[ "${numberOfLines}" -gt '1' && "${i}" -eq "${numberOfLines}" ]]
then
table="${table}$(printf '%s#+' "$(repeatString '#+' "${numberOfColumns}")")"
fi
done
if [[ "$(isEmptyString "${table}")" = 'false' ]]
then
# echo -e "${table}" | column -s '#' -t | awk '/^\+/{gsub(" ", "-", $0)}1'
echo -e "${table}" | column -s '#' -t | awk '/\+/{gsub(" ", "-", $0)}1'
fi
fi
fi
}
function removeEmptyLines()
{
local -r content="${1}"
echo -e "${content}" | sed '/^\s*$/d'
}
function repeatString()
{
local -r string="${1}"
local -r numberToRepeat="${2}"
if [[ "${string}" != '' && "${numberToRepeat}" =~ ^[1-9][0-9]*$ ]]
then
local -r result="$(printf "%${numberToRepeat}s")"
echo -e "${result// /${string}}"
fi
}
function isEmptyString()
{
local -r string="${1}"
if [[ "$(trimString "${string}")" = '' ]]
then
echo 'true' && return 0
fi
echo 'false' && return 1
}
function trimString()
{
local -r string="${1}"
sed 's,^[[:blank:]]*,,' <<< "${string}" | sed 's,[[:blank:]]*$,,'
}
# No args, returns casts object
getChromecasts () {
chromecasts=$(go-chromecast ls);
output=$(echo "{}" | jq .);
while read -r cast;
do
device=$(echo "$cast" | grep -Po "device=\"\K[^\"]*");
device_name=$(echo "$cast" | grep -Po "device_name=\"\K[^\"]*");
uuid=$(echo "$cast" | grep -Po "uuid=\"\K[^\"]*");
output=$(echo "$output" | jq -c ". + {\"${uuid}\": {\"device\": \"${device}\", \"device_name\": \"${device_name}\", \"playhead\": 0, \"videoid\": \"\", \"segmentsid\": \"\", \"segments\": []}}")
done < <(printf '%s\n' "$chromecasts");
echo "$output";
}
castsObjectToArray () {
echo $1 | jq -c 'to_entries | .[] | .value += {"uuid": .key} | .value';
}
castObjectToHash () {
echo "$1" | jq -c '{(.uuid): (. | del(.uuid)) }';
}
statusIngest () {
uuid=$(echo "$1" | jq -r '.uuid');
while true;
do
result=$(timeout 10s go-chromecast status -u "$uuid");
if echo "$result" | grep -Eq "time remaining=[0-9]+s\/[0-9]+s"; then
playhead=$(echo "$result" | grep -Po "time remaining=\K[0-9]+(?=s\/[0-9]+s)");
echo $1 | jq -c "{\"uuid\": .uuid, \"playhead\": \"$playhead\"}"
fi
if echo "$result" | grep -Pq "\[\K[^\]]{11}(?=\])"; then
videoid=$(echo "$result" | grep -Po "\[\K[^\]]{11}(?=\])");
echo $1 | jq -c "{\"uuid\": .uuid, \"videoid\": \"$videoid\"}";
fi
sleep 0.1;
done;
}
statusLoop () {
casts=$(castsObjectToArray "$1");
while read -r cast;
do
statusIngest "$cast" > "/tmp/castblock-updates" &
done < <(printf '%s\n' "$casts");
}
# $1 initialstate
stateLoop () {
chromecasts=$1;
while read -r update;
do
update=$(castObjectToHash "$update");
chromecasts=$(echo "$chromecasts$update" | jq --slurp -c 'reduce .[] as $item ({}; . * $item)');
echo "$chromecasts" > "/tmp/castblock-state";
done < <(cat /tmp/castblock-updates);
}
sbLoop () {
while true;
do
state=$(tail -n 1 "/tmp/castblock-state");
casts=$(castsObjectToArray "$state");
while read -r cast;
do
videoid=$(echo "$cast" | jq -r '.videoid');
# Check if video is playing
if [ ! -z "$videoid" -a "$videoid" != "null" ]; then
# Check if segments loaded
segmentsid=$(echo "$cast" | jq -r '.segmentsid');
if [ "$videoid" != "$segmentsid" ]; then
# Load api
segments=$(curl -fs "https://sponsor.ajay.app/api/skipSegments?videoID=$videoid");
if [ $? -eq 0 ]; then
echo "$cast" | jq -c "{\"uuid\": .uuid, \"segmentsid\": \"$videoid\", \"segments\": $segments}" > "/tmp/castblock-updates";
else
echo "$cast" | jq -c "{\"uuid\": .uuid, \"segmentsid\": \"$videoid\", \"segments\": []}" > "/tmp/castblock-updates";
fi
fi
fi
done < <(printf '%s\n' "$casts");
sleep 1;
done;
}
skipLoop () {
while true;
do
state=$(tail -n 1 "/tmp/castblock-state");
casts=$(castsObjectToArray "$state");
while read -r cast;
do
videoid=$(echo "$cast" | jq -r '.videoid');
segmentsid=$(echo "$cast" | jq -r '.segmentsid');
playhead=$(echo "$cast" | jq -r '.playhead');
uuid=$(echo "$cast" | jq -r '.uuid');
if [ ! -z $uuid ] && [ ! -z $videoid ] && [ ! -z $segmentsid ] && [ ! -z $playhead ] && [ "$videoid" = "$segmentsid" ] && [ "$playhead" -gt 0 ]; then
segments=$(echo "$cast" | jq -c '.segments[]');
while read -r segment;
do
start=$(echo "$segment" | jq -r '.segment[0]');
end=$(echo "$segment" | jq -r '.segment[1]');
if [ ! -z $start ] && [ ! -z $end ] && (( $(echo "$playhead > $start" | bc -l) )) && (( $(echo "$playhead < $end" | bc -l) )) && (( $(echo "$end - $start > 5" | bc -l) )); then
go-chromecast seek-to $(echo "$end") -u "$uuid";
sleep 10;
break 2;
fi
done < <(printf '%s\n' "$segments");
fi
done < <(printf '%s\n' "$casts");
sleep 0.1;
done;
}
getTableData () {
device=$(echo "$1" | jq -r '.device');
device_name=$(echo "$1" | jq -r '.device_name');
uuid=$(echo "$1" | jq -r '.uuid');
playhead=$(echo "$1" | jq -r '.playhead');
segments=$(echo "$1" | jq -c '.segments[]');
printf '%s\n' "$playhead,$device,$device_name";
printf '%s\n' "START,END,TYPE";
while read -r segment; do
printf '%s\n' "$(echo "$segment" | jq -r '.segment[0]'),$(echo "$segment" | jq -r '.segment[1]'),$(echo "$segment" | jq -r '.category')";
done < <(printf '%s\n' "$segments");
}
formatTableData () {
printf '%s\n' "$2";
}
# Casts as objects
chromecasts=$(getChromecasts);
echo "$chromecasts" > "/tmp/castblock-state";
statusLoop "$chromecasts";
stateLoop "$chromecasts" &
sbLoop &
skipLoop &
clear;
trap 'cleanup' SIGINT;
while true;
do
state=$(tail -n 1 "/tmp/castblock-state")
if ! (cmp -s <(echo "$state" | jq -cS '.') <(echo "$prevState" | jq -cS '.')); then
display="";
while read -r cast;
do
data=$(getTableData "$cast");
display+=$(printTable ',' "$(echo -e "$data")")"\n";
done < <(printf '%s\n' "$state" | jq -c '.[]');
echo -e "\033[0;0f";
echo -e "$display";
fi
prevState=$state;
done;
wait;