-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutmp
executable file
·441 lines (359 loc) · 11.1 KB
/
utmp
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
#!/bin/bash
source $(dirname $0)/utmp-lib.sh
#-SETUP
WDIR=$(dirname $0)
#INPUT
mode=$1
key=$2
opt=$3
what=$4
#CONSTANTS YOU CAN CHANGE
DIR=~/utmp
TEMPDIR=~/utmp/temp
VOLUME=10
#in case you want to put your things somewhere else, and don't want utmp to reinstall them every time:
# set the corresponding variable to it's call command
YTDLP=yt-dlp
FFPLAY=ffplay
FFMPEG=ffmpeg
#for those with wacky OSs - change it to one of the ones in utmp-install
MOS="changethis"
#CONSTANTS YOU CAN CHANGE
#FILE SETUP
[ ! -d $DIR ] && ( ech "no existing DIR, creating new directory!" && mkdir $DIR )
[ ! -d $TEMPDIR ] && ( ech "no existing TEMPDIR, creating new directory!" && mkdir $TEMPDIR )
#SCRIPT SETUP
trap 'echred " Utmp stopped :::during::: script setup"; exit 1' SIGINT
shopt -s extglob
#^ for nice regex in list expansion things
die () { # only exit if they replaced one of the necessary inputs with -c
if [[ "$mode" = "-c" ]]|| [[ "$key" = "-c" ]]; then
trap - SIGINT
exit $1
fi
}
force=0
if [[ "$opt" = "-C" ]] || [[ "$mode" = "-C" ]]|| [[ "$key" = "-C" ]] || [[ "$what" = "-C" ]]; then
force=1
fi
if [[ "$opt" = "-c" ]] || [[ "$mode" = "-c" ]]|| [[ "$key" = "-c" ]] || [[ "$what" = "-c" ]] || [[ $force -eq 1 ]]; then
#check that all libs/commands installed
$WDIR/utmp-install $YTDLP $FFPLAY $FFMPEG $MOS $force
istatus=$?
if [ $istatus -eq 1 ]; then
echred "UTMP has missing dependencies, please check the file at $WDIR/utmp for advanced guidance"
die 1
elif [ $istatus -eq 0 ]; then
ech "UTMP dependency installs confirmed."
die 0
else
echred "UTMP's install script has broken!"
die 1
fi
fi
#no arguments, return manual.
if [ "X$mode" = "X" ]; then
$WDIR/utmp-desc
exit 1
fi
echo
got_queue=""
get_queue () {
local prequeue=$(ls $DIR/{*.wav,*.m4a,*.webm,*.mp3} | tr " " "^" | sort)
local queue=""
#STAGE 1 : Path removal
#some files may not follow utmp's file format and so doing all the cuts on them would destroy their filenames BROKEN
for file in $prequeue; do
#supposedly char 16 is the first char
if [ "${file:16:1}X" == "[X" ]; then # follows utmp format :), chop off url
local file=$(echo $file | cut -d ] -f 2- | cut -d - -f 2- )
else
local file=${file:16:9999}
fi
# if it didn't, just be OK with that and sort as is
local queue="${queue}=${file}"
done
#STAGE 2 : Strip =, Strip & Sort
local queue=$(echo ${queue:2:99999} | sed 's/\^/ /g')
IFS="="
echo "" > $DIR/queue.txt
for file in $queue; do
#remove leading whitespaces
local file=$(echo $file | sed -e 's/^[[:space:]]*//' | sed 's|^/||')
echo $file >> $DIR/queue.txt
done
local queue=$(cat $DIR/queue.txt | sort)
#read from the file and sort :)
local queue=$(echo ${queue:1:99999} | tr '\n' '=')
got_queue=$queue
}
trap 'echred " Utmp stopped :::during::: ???"; exit 1' SIGINT
if [ $mode = "list" ]; then
ech "Current Stored Library:"
ech "Keyword: $key"
ech "----------------------"
get_queue
count=0
for file in $got_queue; do
tex="\033[0;32m$count\033[0;0m : $(echo $file | sed 's/.*\///g')"
echo -e $tex | grep ".*$key.*" | sort
((count++))
done
ech "----------------------"
elif [ "$mode" = "find" ] || [ "$mode" = "down" ]; then
[ -z "$key" ] && echred "Need <key> to download/find from Youtube!" && exit 1
#^ there is key OR I exit now
if [ $mode = "find" ]; then
ech "Searching for '$key'..."
trap 'echred " Utmp stopped :::during::: find"; exit 1' SIGINT
echo "$YTDLP "$key" -f ba --default-search auto --max-downloads 1 --abort-on-error --quiet -o \"$TEMPDIR/[%(id)s] - %(title)s.%(ext)s\""
$YTDLP "$key" -f ba --default-search auto --max-downloads 1 --abort-on-error --quiet -o "$TEMPDIR/[%(id)s] - %(title)s.%(ext)s"
else
ech "Downloading from '$key'..."
trap 'echred " Utmp stopped :::during::: down"; exit 1' SIGINT
echo "$YTDLP "$key" -f ba --max-downloads 1 --abort-on-error --quiet -o \"$TEMPDIR/[%(id)s] - %(title)s.%(ext)s\""
$YTDLP "$key" -f ba --max-downloads 1 --abort-on-error --quiet -o "$TEMPDIR/[%(id)s] - %(title)s.%(ext)s"
#still force 1 download because we only move the 1 last download, not all downloaded
fi
name=$(ls -t $TEMPDIR | tail -1)
[ "$name" = "" ] && echred "Could not find downloaded file!" && ech "Did you forget to use -s while dowloading or not have wifi?" && trap - SIGINT && exit 0
ech "Downloaded result as: $name"
echo "mv \"$TEMPDIR/$name\" \"$DIR/$name\""
mv "$TEMPDIR/$name" "$DIR/$name"
if [ "$opt" = "-t" ]; then
ech "[-t] Playing '$name'"
echo "$FFPLAY -i \"$DIR/$name\" -showmode 1 -autoexit -volume $VOLUME -loglevel fatal"
trap 'echred " Utmp stopped :::during::: file playback"; exit 1' SIGINT
$FFPLAY -i "$DIR/$name" -showmode 1 -autoexit -volume $VOLUME -loglevel fatal
fi
if [ "$opt" = "-t" ] || [ "$opt" = "-p" ]; then
read -r -p "Delete file? [n/Y]: " inp
if [ "$inp" = "Y" ]; then
echwarn "Deleting $name!"
echo "rm \"$DIR/$name\""
rm "$DIR/$name"
ech "Done"
else
ech "Will not delete file"
fi
fi
ech "Finished!"
trap - SIGINT
exit 0
elif [ $mode = "play" ]; then
[ -z "$key" ] && echred "Need <key> to play songs!" && trap - SIGINT && exit 1
trap 'echred " Utmp stopped :::during::: file playback"; exit 1' SIGINT
get_queue
queue=$got_queue
if [ "$key" = "all" ]; then
echwarn "Playing ALL songs!"
ls $DIR/{*.wav,*.m4a,*.webm,*.mp3} 2>/dev/null | tr " " "^" | sort -R > $TEMPDIR/okie.txt
if [ "X$queue" == "X" ]; then
echred "There are no songs!"
trap - SIGINT
exit 0
fi
while read file; do
file=$(echo $file | tr "^" " ")
ech "Playing $file..."
echo "$FFPLAY -i \"$file\" -showmode 1 -autoexit -volume $VOLUME -loglevel fatal"
$FFPLAY -i $file -showmode 1 -autoexit -volume $VOLUME -loglevel fatal
((count++))
done < $TEMPDIR/okie.txt
ech "Finished!"
trap - SIGINT
exit 0
fi
IFS=',' read -ra dexes <<< "$key"
ech "Playing songs: ${dexes[@]}"
for index in ${dexes[@]}; do
count=0
# queue=$(ls $DIR/{*.wav,*.m4a,*.webm,*.mp3} 2>/dev/null | tr " " "^")
plaid=0
if [ "X$queue" == "X" ]; then
echred "There are no songs!"
trap - SIGINT
exit 0
fi
for file in $queue; do
if [ $count = $index ]; then
ech "Playing [$index]: $file"
echo "$FFPLAY -i $DIR/**\"$file\" -showmode 1 -autoexit -volume $VOLUME -loglevel fatal"
$FFPLAY -i $DIR/**"$file" -showmode 1 -autoexit -volume $VOLUME -loglevel fatal
plaid=1
break
fi
((count++))
done
if [ $plaid -eq 0 ]; then
echwarn "Cannot play [$index]: Index not found!"
ech "Using "$index" as a search keyword..."
else
continue
#don't do the thing below then
fi
for rawfile in $queue; do
file=$(echo $rawfile | tr "^" " ")
if [[ "$file" == *"$index"* ]]; then
ech "Found '$index': $file"
echo "$FFPLAY -i $DIR/**\"$file\" -showmode 1 -autoexit -volume $VOLUME -loglevel fatal"
$FFPLAY -i $DIR/**"$file" -showmode 1 -autoexit -volume $VOLUME -loglevel fatal
plaid=1
fi
done
[ $plaid -eq 0 ] && ( echred "Cannot play or find: $index!" )
done
elif [ $mode = "expo" ]; then
trap 'echred " Utmp stopped :::during::: library export"; exit 1' SIGINT
out="echo \"::UTMP LIBRARY IMPORT::\""
if [ "$key" = "all" ] || [ -z "$key" ]; then
echwarn "Exporting ALL songs!"
queue=$(ls $DIR/{*.wav,*.m4a,*.webm,*.mp3} 2>/dev/null | tr " " "^")
if [ "X$queue" == "X" ]; then
echred "There are no songs!"
trap - SIGINT
exit 0
fi
for file in $queue; do
file=$(echo $file | tr "^" " ")
#cope with both yt-dlp ID's and full url's!
url=$(echo $file | sed 's#_#/#g')
#remove left side, remove right side
url=$(echo $url | sed 's#.*\[##g')
url=$(echo $url | sed 's#\].*##g')
if [ "$url" = "" ] || [ "$url" = "$file" ]; then
echwarn "$file has no formatted url, skipping!"
else
ech "Export: $file \033[0;0m->\033[0;32m $url"
out="$out && utmp down \"$url\""
# echo $out
fi
((count++))
done
echo $out
trap - SIGINT
exit 1
fi
IFS=',' read -ra dexes <<< "$key"
ech "Exporting songs: ${dexes[@]}"
for index in ${dexes[@]}; do
foundf=/tmp/found.$$
echo 0 > $foundf
count=0
queue=$(ls $DIR/{*.wav,*.m4a,*.webm,*.mp3} 2>/dev/null | tr " " "^")
if [ "X$queue" == "X" ]; then
echred "There are no songs!"
trap - SIGINT
exit 0
fi
for file in $queue; do
file=$(echo $file | tr "^" " ")
if [ $count = $index ]; then
cfile=$(echo $file | tr "^" " ")
#cope with both yt-dlp ID's and full url's!
url=$(echo $cfile | sed 's#_#/#g')
#remove left side, remove right side
url=$(echo $url | sed 's#.*\[##g')
url=$(echo $url | sed 's#\].*##g')
if [ "$url" = "" ] || [ "$url" = "$file" ] || [ "$url" = "$cfile" ]; then
echwarn "$file has no formatted url, skipping!"
else
ech "Export: $file \033[0;0m->\033[0;32m $url"
out="$out && utmp down \"$url\""
# echo $out
fi
echo 1 > $foundf
fi
((count++))
done
found=$(cat $foundf)
[ $found -eq 0 ] && ( echwarn "Cannot export [$index]: File not found!" )
rm $foundf
done
echo $out
elif [ $mode = "remv" ]; then
trap 'echred " Utmp stopped :::during::: media deletion"; exit 1' SIGINT
if [ "$key" = "all" ] || [ -z "$key" ]; then
echwarn "Are you sure you want to delete ALL songs?"
read -r -p "Proceed? [n/Y]: " inp
if [ "$inp" = "Y" ]; then
echred "Deleting everything..."
rm -R $DIR/*
echo "rm -R $DIR/*"
ech "Finised deletion."
else
ech "Will not delete."
trap - SIGINT
exit 1
fi
fi
IFS=',' read -ra dexes <<< "$key"
ech "Deleting songs: ${dexes[@]}"
min=0
for index in ${dexes[@]}; do
foundf=/tmp/found.$$
echo 0 > $foundf
count=0
get_queue
queue=$got_queue
if [ "X$queue" == "X" ]; then
echred "There are no songs!"
trap - SIGINT
exit 0
fi
for file in $queue; do
file="$(echo $file | sed 's#\^# #g')"
temp=$(($index-$min))
if [ $count = $temp ]; then
echwarn "Deleting [$index]: $file"
rm -i $DIR/**"$file"
echo 1 > $foundf
((min++))
fi
((count++))
done
found=$(cat $foundf)
[ $found -eq 0 ] && ( echwarn "Cannot delete [$index]: File not found!" )
rm $foundf
done
ech "Finished deletion."
elif [ $mode = "clen" ]; then
trap 'echred " Utmp stopped :::during::: file cleaning"; exit 1' SIGINT
queue=$(ls $DIR/{*.wav,*.m4a,*.webm,*.mp3})
queue=$(echo $queue | sed "s# /#∆#g" | sed "s# #π#g" | sed "s#∆# #g")
foundf=0
for fil in $queue; do
pfil="${fil//π/ }"#I am going to go insane
nfil="${pfil%.*}"
nfilxt="${pfil##*.}"
# ech "Checking: $pfil"
for file in $queue; do
pfile="${file//π/ }"
nfile="${pfile%.*}"
nfilext="${pfile##*.}"
if [ "$nfil" = "$nfile" ] && [ "$nfilxt" != "$nfilext" ]; then
echwarn "Found duplicate: $pfile"
if [ "$nfilxt" = "webm" ]; then
rm /"$pfile"
elif [ "$nfilext" = "webm" ]; then
rm /"$pfil"
elif [ "$nfilxt" = "wav" ]; then
rm /"$pfile"
else
rm /"$pfil"
fi
foundf=1
queue=$(ls $DIR/{*.wav,*.m4a,*.webm,*.mp3})
queue=$(echo $queue | sed "s# /#∆#g" | sed "s# #π#g" | sed "s#∆# #g")
fi
done
done
[ $foundf -eq 0 ] && ( ech "Found no duplicates!" )
else
echred "Invalid mode $mode!"
trap - SIGINT
echo $out
exit 1
fi
trap - SIGINT
exit 1