-
Notifications
You must be signed in to change notification settings - Fork 0
/
tsl
executable file
·163 lines (133 loc) · 4.7 KB
/
tsl
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
#!/bin/bash
# This bash script uses the following CLI commands
# jq [URL]
# gum [URL]
# streamlink [URL]
# twitch [URL]
set -e
EXE=${0##*/}
[[ -z "$EXE" ]] && echo "Could not determine name of executable." && exit 1
declare -A URLS
: "${URLS[jq]:="https://stedolan.github.io/jq/"}"
: "${URLS[gum]:="https://github.com/charmbracelet/gum"}"
: "${URLS[sl]:="https://streamlink.github.io/install.html"}"
: "${URLS[twitch]:="https://github.com/twitchdev/twitch-cli"}"
# Check for dependencies
[[ -z $(command -v jq) ]] && echo "jq is not installed! [${URLS[jq]}]" && exit 1
[[ -z $(command -v gum) ]] && echo "gum is not installed! [${URLS[gum]}]" && exit 1
[[ -z $(command -v streamlink) ]] && echo "streamlink is not installed! [${URLS[sl]}]" && exit 1
[[ -z $(command -v twitch) ]] && echo "twitch CLI is not installed! [${URLS[twitch]}]" && exit 1
# Data setup
declare -A DATA
: "${DATA[path]:="$XDG_DATA_HOME/$EXE"}"
: "${DATA[file]:="streamers"}"
: "${DATA[state]:="online"}"
# Check if directories and files exists
[[ ! -d "${DATA[path]}" ]] && mkdir -p "${DATA[path]}"
[[ ! -e "${DATA[path]}/${DATA[file]}" ]] && touch "${DATA[path]}/${DATA[file]}"
[[ ! -e "${DATA[path]}/${DATA[state]}" ]] && touch "${DATA[path]}/${DATA[state]}" && \
echo "Online,Game category" > "${DATA[path]}/${DATA[state]}"
# Command functions
_list() {
gum pager --border-foreground="4" --line-number.foreground="243" < "${DATA[path]}/${DATA[file]}"
}
_add() {
# TODO: add support for adding more then (1) one entry
ADDED=$(gum input --value="$1" --placeholder "Enter a streamers name")
[[ -n "${ADDED}" ]] && \
echo "${ADDED}" >> "${DATA[path]}/${DATA[file]}" && \
echo " Added: ${ADDED}"
}
_delete() {
while IFS= read -r line; do
nameArray+=("${line}")
done < "${DATA[path]}/${DATA[file]}"
SELECTED=$(gum choose --limit="1" < "${DATA[path]}/${DATA[file]}")
if [[ "${nameArray[*]}" =~ ${SELECTED} ]]; then
for i in "${!nameArray[@]}"; do
if [[ "${nameArray[i]}" = "${SELECTED}" ]]; then
unset "nameArray[i]"
fi
done
echo " Deleting: ${BASH_REMATCH[0]}"
for i in "${nameArray[@]}"; do
echo "${i}"
done > "${DATA[path]}/${DATA[file]}"
fi
}
# Tab competition taken from rwxrob. rwxrob.tv
# add `complete -C <script name> <script name>` to your .bashrc
declare -a commands=(list add delete)
if [[ -n $COMP_LINE ]]; then
for c in "${commands[@]}"; do
[[ ${c:0:${#2}} == "${2,,}" ]] && echo "$c"
done
exit
fi
# Delegation
if [[ -n "$1" ]]; then
declare cmd="$1"; shift
for c in "${commands[@]}"; do
[[ $c == "$cmd" ]] && "_$cmd" "$@" && exit "$?"
done
fi
# Main
#
# gum style
# Margin "0 1" = first number is margin from top second is form left
# Padding "1 0" = first number is top & bottom and second is left & right
gum style \
--border=none \
--margin="0 1" \
--padding="1 0" \
--width="35" \
--align="center" \
--border-foreground="4" \
--foreground="4" \
"Twitch Streamers Live!"
declare -A onlineArray
if [[ -s "${DATA[path]}/${DATA[state]}" ]]; then
rm "${DATA[path]}/${DATA[state]}"
touch "${DATA[path]}/${DATA[state]}"
echo "Online,Game category" > "${DATA[path]}/${DATA[state]}"
fi
while IFS= read -r line; do
isOnline=$(gum spin -s dot \
--spinner.margin="0 1" \
--title="Fetching ${line}..." \
--show-output -- \
twitch api get streams -Puq user_login="${line}" | jq -r .data[].user_name)
if [[ -n "${isOnline}" ]]; then
category=$(gum spin -s line \
--spinner.margin="0 1" \
--title="Fetching ${isOnline}'s category..." \
--show-output -- \
twitch api get streams -Pq user_login="${isOnline}" | jq -r .data[].game_name)
onlineArray+=([${isOnline}]="${category}")
echo "${isOnline},${category}" >> "${DATA[path]}/${DATA[state]}"
fi
done < "${DATA[path]}/${DATA[file]}"
echo "quit, " >> "${DATA[path]}/${DATA[state]}"
opt=$(gum table --separator="," --widths="25" --file="${DATA[path]}/${DATA[state]}")
case "${opt%,*}" in
quit)
gum style \
--border=none \
--margin="0 1" \
--padding="0 0" \
--width="35" \
--align="center" \
--border-foreground="4" \
--foreground="1" \
"Quiting!"
exit 0 ;;
esac
player=$(gum choose --limit="1" Browser Streamlink)
case "${player}" in
Browser)
xdg-open "https://twitch.tv/${opt%,*}" 2>/dev/null &
;;
Streamlink)
exec streamlink --twitch-disable-ads -p mpv "https://twitch.tv/${opt%,*}" best >/dev/null &
;;
esac