Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for stream titles/custom date format in filename #6

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
36 changes: 33 additions & 3 deletions tools/record.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,19 @@ WEBSOCKET_URL="wss://pubsub-edge.twitch.tv/v1"
CLIENT_ID="jzkbprff40iqj646a697cyrvl0zt2m6"

PRINT_FILENAME=0
FILENAME_TITLE=0
FILENAME_DATE=0

errf(){ >&2 printf "$@"; }

get_id () {
curl --silent --fail -H "Client-ID: $CLIENT_ID" "$API_URL$1" | jq -r ._id
}

get_stream_title () {
curl --silent --fail -H "Client-ID: $CLIENT_ID" "$API_URL$1" | jq -r .status
}

print_usage() {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new switches should be added to the usage text.

errf "Usage: record [OPTIONS...] <USERNAMES...>\n"
errf "\n"
Expand Down Expand Up @@ -41,7 +47,7 @@ invalid_input() {

check_deps

while getopts ":ph" opt
while getopts "d:pht" opt
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
while getopts "d:pht" opt
while getopts ":phtd:" opt

This should be ":phtd:", ":" at the start of a getopt argument prevents it from displaying debug errors.

do
case "$opt" in
p )
Expand All @@ -51,6 +57,14 @@ do
print_usage
exit 0
;;
t )
FILENAME_TITLE=1
;;
d )
FILENAME_DATE=1
DFORMAT="+""$OPTARG"
#OPTIND=`echo $OPTIND + 1 | bc`
;;
\? )
invalid_input "$(printf $'unknown option \'-%s\'' "$OPTARG")"
;;
Expand Down Expand Up @@ -113,11 +127,27 @@ done
then
(
username=${id_username[$id]}

errf '[%s] stream started\a\n' "$username"
while :
do
mkdir -p "$username"
filename=$(printf "%s/%s.ts" $username "$(date -u '+%Y_%m_%d_%H_%M_%S_(%Z)')")

filename="$username/"

if [ "$FILENAME_TITLE" == "1" ];
then
title=$(get_stream_title $username)
filename+="${title}_"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to sanitize the filename somehow, as it is, if a streamer has their title set to ../title, the file will be created up a directory. We also have to consider special characters that are not allowed in filenames, both on Windows and Linux.

fi
if [ "$FILENAME_DATE" == "1" ];
then
filename+=$(date $DFORMAT)
else
filename+=$(date -u '+%Y-%m-%d-%H-%M-%S-(%Z)')
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
filename+=$(date -u '+%Y-%m-%d-%H-%M-%S-(%Z)')
filename+=$(date -u '+%Y_%m_%d_%H_%M_%S_(%Z)')

I would also prefer the date format stay the same as it was before, +%Y_%m_%d_%H_%M_%S_(%Z)

fi
filename+=".ts"

errf '[%s] recording to %s\n' "$username" "$filename"
(twitchpipe --archive "$username" >> "$filename") 2>&1 | (
while read -r line;
Expand Down Expand Up @@ -148,4 +178,4 @@ done
;;
esac
done
)
)