-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzapping.sh
More file actions
executable file
·325 lines (297 loc) · 7.71 KB
/
zapping.sh
File metadata and controls
executable file
·325 lines (297 loc) · 7.71 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
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#!/bin/bash
print_help() {
echo "Arguments:"
echo " -h or --help: Prints this message"
echo " -v or --verbose: Prints verbose messages"
echo " -r or --record: Records the stream to a file instead of playing"
echo " \"channel name\": Auto-plays the channel by its name"
}
# Read arguments
while [[ $# -gt 0 ]]; do
case $1 in
-v|--verbose)
VERBOSE=1
shift # past argument
;;
-r|--record)
RECORD=1
shift # past argument
;;
-h|--help)
print_help
exit
;;
*)
AUTO_CHANNEL=$1
shift
;;
esac
done
echo "verbose: ${VERBOSE}"
echo "Record: ${RECORD}"
echo "Auto channel: $AUTO_CHANNEL"
# Constants
CONFIG_FILE="${HOME}/.config/zapping"
USER_AGENT="Zapping/bash-1.0"
# Check dependencies
case $OSTYPE in
linux-gnu)
echo "OS detected: GNU+Linux"
;;
darwin)
echo "OS detected: macOS"
if ! command -v gdate &> /dev/null
then
echo "This script requires gdate"
exit
fi
;;
*)
echo "WARNING: OS \"${OSTYPE}\" not supported. Trying anyways."
;;
esac
if ! command -v mpv &> /dev/null
then
echo "This script requires mpv"
MISSING_DEPS=1
fi
if ! command -v jq &> /dev/null
then
echo "This script requires jq"
MISSING_DEPS=1
fi
if ! command -v http &> /dev/null
then
echo "This script requires HTTPie"
MISSING_DEPS=1
fi
if ! command -v uuidgen &> /dev/null
then
echo "This script requires uuidgen"
MISSING_DEPS=1
fi
if [ -n "${RECORD}" ] && ! command -v ffmpeg &> /dev/null
then
echo "This script requires ffmpeg to record"
MISSING_DEPS=1
fi
if [ -n "${MISSING_DEPS}" ]
then
exit
fi
# Functions
to_array() {
IFS=$'\n' read -d '' -r -a "$1"
}
join_arrays() {
local -n FIRST_ARRAY=$1
local -n SECOND_ARRAY=$2
local i
for (( i = 0; i < ${#FIRST_ARRAY[*]}; ++i))
do
echo "${FIRST_ARRAY[$i]}" "${SECOND_ARRAY[$i]}"
done
}
zdate() {
if type -t gdate &>/dev/null
then
gdate "$@"
else
date "$@"
fi
}
timestamp_to_hh_mm() {
zdate -d "@$1" +'%H:%M'
}
zargs() {
while IFS=$'$\n' read -r line; do
"$1" "$line"
done
}
echo_verbose() {
if [ -n "${VERBOSE}" ]
then
echo "$@"
fi
}
ping_drhouse() {
while true
do
echo
echo "Pinging drhouse..."
http --verify=no --form --ignore-stdin \
https://drhouse.zappingtv.com/hb/V20/androidtv/ \
User-Agent:"${USER_AGENT}" \
Content-Type:"application/x-www-form-urlencoded; charset=UTF-8" \
playtoken="$1" \
uuid="${UUID}" \
deviceInfo="{}" > /dev/null
sleep 25
done
}
# Export functions
export -f timestamp_to_hh_mm
# Load token from file?
if [ -f "${CONFIG_FILE}" ]
then
ZAPPING_TOKEN=$(cat "${CONFIG_FILE}")
fi
# Set UUID
UUID=$(uuidgen)
echo_verbose "UUID: ${UUID}"
if [ -z "$ZAPPING_TOKEN" ];
then
# Login
echo "Logging in..."
GETCODE_RESPONSE=$(http -f --verify=no \
https://meteoro.zappingtv.com/activation/V20/androidtv/getcode \
uuid="${UUID}" \
acquisition="Android TV" \
User-Agent:"${USER_AGENT}")
CODE=$(echo "${GETCODE_RESPONSE}" | jq -r .data.code)
echo "Visit https://app.zappingtv.com/smart"
echo "Code: ${CODE}"
read -r -p "Pres [ENTER] to continue..."
# Check code
echo "Checking if the code is linked..."
CHECKCODE_RESPONSE=$(http -f --verify=no \
https://meteoro.zappingtv.com/activation/V20/androidtv/linked \
code="${CODE}" \
User-Agent:"${USER_AGENT}")
CHECKCODE_STATUS=$(echo "${CHECKCODE_RESPONSE}" | jq -r .status)
ZAPPING_TOKEN=$(echo "${CHECKCODE_RESPONSE}" | jq -r .data.data)
echo "Status: ${CHECKCODE_STATUS} Token: ${ZAPPING_TOKEN}"
# Save token
echo "Saving token to ${CONFIG_FILE}..."
echo "${ZAPPING_TOKEN}" > "${CONFIG_FILE}"
fi
# Play function
play_or_record() {
# Get play token
echo "Getting play token..."
echo http -f --verify=no \
https://drhouse.zappingtv.com/login/V20/androidtv/ \
token="${ZAPPING_TOKEN}" \
uuid="${UUID}" \
User-Agent:"${USER_AGENT}"
DRHOUSE_RESPONSE=$(http -f --verify=no \
https://drhouse.zappingtv.com/login/V20/androidtv/ \
token="${ZAPPING_TOKEN}" \
uuid="${UUID}" \
User-Agent:"${USER_AGENT}")
PLAY_TOKEN=$(echo "${DRHOUSE_RESPONSE}" | jq -r .data.playToken)
echo_verbose "Play token: ${PLAY_TOKEN}"
# Play
PLAY_URL=$(echo "${CHANNEL_LIST_RESPONSE}" | jq -r ".data[] | select((.number|tostring) + \" - \" + .name == \"${CHANNEL_NAME}\") | .url")
PLAY_URL="${PLAY_URL}?token=${PLAY_TOKEN}${PLAY_EXTRA}"
if [ -n "${START_TIME}" ]
then
PLAY_URL="${PLAY_URL}&startTime=${START_TIME}"
fi
if [ -n "${END_TIME}" ]
then
PLAY_URL="${PLAY_URL}&endTime=${END_TIME}"
fi
MPV_VERBOSE_PARAMS=""
if [ -n "${VERBOSE}" ]
then
MPV_VERBOSE_PARAMS="-v"
fi
ping_drhouse "${PLAY_TOKEN}" &
PING_DRHOUSE_PID=$!
echo "PID ping ${PING_DRHOUSE_PID}"
if [ -n "${RECORD}" ]
then
RECORDING_FILE="recording-$(date +%Y-%m-%d-%H%M%S).ts"
echo "Recoding to file ${RECORDING_FILE}"
echo_verbose "Record URL: ${PLAY_URL}"
ffmpeg \
-user_agent "${USER_AGENT}" \
-live_start_index -99999 \
-i "${PLAY_URL}" \
-acodec copy \
-vcodec copy \
"${RECORDING_FILE}"
else
echo "Playing..."
echo_verbose "Play URL: ${PLAY_URL}"
mpv \
--user-agent="${USER_AGENT}" \
--demuxer-lavf-o=live_start_index=-99999 \
$MPV_VERBOSE_PARAMS \
--force-seekable=yes \
"${PLAY_URL}"
fi
kill $PING_DRHOUSE_PID
}
# Get channel list
echo_verbose "Zapping token: $ZAPPING_TOKEN"
echo "Getting channel list..."
CHANNEL_LIST_RESPONSE=$(http -f --verify=no \
https://alquinta.zappingtv.com/v20/androidtv/channelswithurl/ \
quality=auto \
hevc=1 \
is3g=0 \
token="${ZAPPING_TOKEN}" \
User-Agent:"${USER_AGENT}")
# Auto play?
if [ -n "${AUTO_CHANNEL}" ]
then
echo "Playing channel automatically: ${AUTO_CHANNEL}"
CHANNEL_NAME="${AUTO_CHANNEL}"
play_or_record
exit
fi
# Choose channel
declare CHANNEL_NAMES
to_array CHANNEL_NAMES <<< "$(echo "${CHANNEL_LIST_RESPONSE}" | jq '(.data[])' | jq -r '(.number|tostring) + " - " + .name' | sort -V)"
PS3='Select channel: '
select CHANNEL_NAME in "${CHANNEL_NAMES[@]}"
do
# Live / VOD?
echo "${CHANNEL_NAME}"
START_TIME=""
END_TIME=""
read -r -p "Play [L]ive, [V]od or [T]ime? (default: Live) " TIME_PLAY_OPTION
case $TIME_PLAY_OPTION in
V | v)
echo "Getting catchup data..."
CHANNEL_IMAGE=$(echo "${CHANNEL_LIST_RESPONSE}" | jq -r ".data[] | select(.name == \"${CHANNEL_NAME}\") | .image")
CATCHUP_RESPONSE=$(http POST --verify=no \
"https://charly.zappingtv.com/v3.1/androidtv/${CHANNEL_IMAGE}/catchup/0/live")
declare SECTION_NAMES
to_array SECTION_NAMES <<< "$(echo "${CATCHUP_RESPONSE}" | jq -r .data[].title)"
PS3='Select section: '
select SECTION_NAME in "${SECTION_NAMES[@]}"
do
echo "Section selected: ${SECTION_NAME}"
to_array CARDS_TITLES <<< "$(echo "${CATCHUP_RESPONSE}" | jq -r ".data[] | select(.title == \"${SECTION_NAME}\") | .cards[].title")"
to_array CARDS_START_TIMESTAMPS <<< "$(echo "${CATCHUP_RESPONSE}" | jq -r ".data[] | select(.title == \"${SECTION_NAME}\") | .cards[].start_time")"
to_array CARDS_END_TIMESTAMPS <<< "$(echo "${CATCHUP_RESPONSE}" | jq -r ".data[] | select(.title == \"${SECTION_NAME}\") | .cards[].end_time")"
to_array CARDS_TIMES <<< "$(echo "${CATCHUP_RESPONSE}" | jq -r ".data[] | select(.title == \"${SECTION_NAME}\") | .cards[].start_time" | zargs timestamp_to_hh_mm)"
to_array CARDS <<< "$(join_arrays CARDS_TIMES CARDS_TITLES)"
PS3='Select card: '
select CARD_NAME in "${CARDS[@]}"
do
echo "${CHANNEL_NAME}: ${CARD_NAME}"
CARD_INDEX=$(( REPLY - 1 ))
START_TIME=${CARDS_START_TIMESTAMPS[$CARD_INDEX]}
END_TIME=${CARDS_END_TIMESTAMPS[$CARD_INDEX]}
break
done
break
done
;;
T | t)
read -r -p "How much time back (example: 20 minute)? " TIME_BACK
START_TIME=$(zdate -d"-${TIME_BACK}" +%s)
echo "${CHANNEL_NAME}: -${TIME_BACK}"
;;
L | *)
echo "${CHANNEL_NAME}: Live"
;;
esac
play_or_record
# Reset prompt
PS3='Select channel: '
done