Files
Kapisi/roles/Yggdrasil/package/yggdrasil-get

136 lines
2.6 KiB
Plaintext
Raw Normal View History

2024-09-02 23:54:57 -05:00
#!/bin/bash
2025-10-21 14:04:09 -05:00
source /opt/aninix/Uniglot/Bash/header
2024-09-02 23:54:57 -05:00
2025-10-21 14:04:09 -05:00
function ygdl() {
### Download a file
# param path: path to make
# param newname (optional): output location
2024-09-02 23:54:57 -05:00
if [ "$newname" != "" ]; then
2025-10-21 14:04:09 -05:00
wget -O "$newname" "$path";
2024-09-02 23:54:57 -05:00
else
2025-10-21 14:04:09 -05:00
wget "$path";
2024-09-02 23:54:57 -05:00
fi
2025-10-21 14:04:09 -05:00
}
function ygyt() {
### Cache from YouTube
# param path: YouTube URI
# param newname: output location
2024-09-02 23:54:57 -05:00
if [ `echo "$path" | grep -c '/watch?v='` -eq 1 ]; then
path="$(echo "$path" | cut -f 1 -d '&')"
fi
if [ "$newname" != "" ]; then
2025-10-21 14:04:09 -05:00
yt-dlp -o "$newname" "$path";
2024-09-02 23:54:57 -05:00
else
2025-10-21 14:04:09 -05:00
yt-dlp "$path";
2024-09-02 23:54:57 -05:00
fi
2025-10-21 14:04:09 -05:00
}
function ygmp3() {
uri="$path"
2024-09-02 23:54:57 -05:00
if [ "$newname" != "" ]; then
2025-10-21 14:04:09 -05:00
ygmkdir "$newname"
cd "$newname"
2024-09-02 23:54:57 -05:00
fi
2025-10-21 14:04:09 -05:00
if [ `echo "$uri" | grep -c '/watch?v='` -eq 1 ]; then
uri="$(echo "$uri" | cut -f 1 -d '&')"
2024-09-02 23:54:57 -05:00
fi
2025-10-21 14:04:09 -05:00
youtube-mp3 "$uri";
2024-09-02 23:54:57 -05:00
yggdrasil-set-music-data *.mp3
2025-10-21 14:04:09 -05:00
}
function ygcache() {
### Cache files
# param path: path to move
# param newname (option): somewhere other than $PWD to move
# param exec: what to use
if [ -n "$1" ]; then
exec="$1"
fi
2024-09-02 23:54:57 -05:00
if [ "$newname" != "" ]; then
2025-10-21 14:04:09 -05:00
$exec "$path" ./"$newname"
2024-09-02 23:54:57 -05:00
else
2025-10-21 14:04:09 -05:00
$exec "$path" .
2024-09-02 23:54:57 -05:00
fi
2025-10-21 14:04:09 -05:00
}
function ygmkdir() {
### Make a directory
# path: directory to make
if [ -n "$1" ]; then
path="$1"
fi
if [ -d "$path" ]; then
infoheader "Directory '$path' already existed."
2024-09-02 23:54:57 -05:00
else
2025-10-21 14:04:09 -05:00
mkdir -p "$path"
chmod u+w "$path"
2024-09-02 23:54:57 -05:00
fi
2025-10-21 14:04:09 -05:00
}
function usage() {
### Usage
# param retcode: what to return
if [ -z "$1" ]; then
retcode=0;
2024-09-02 23:54:57 -05:00
else
2025-10-21 14:04:09 -05:00
retcode=$1;
2024-09-02 23:54:57 -05:00
fi
2025-10-21 14:04:09 -05:00
cat << EOM
AniNIX/Yggdrasil Bash API
Syntax: yggdrasil-get {dl|yt|mp3|cp|mv|mkdir} PATH [new file name in $PWD]
Option:
-- dl: Use wget
-- yt: Use yt-dlp
-- mp3: Use youtube-mp3
-- cp: Copy the file here.
-- mv: Move the file here.
-- mkdir: Make a folder
EOM
exit $retcode
}
### Main
export YGGDRASIL="/home/yggdrasil"
if [ "$1" == '-v' ]; then
set -x
shift
2024-09-02 23:54:57 -05:00
fi
2025-10-21 14:04:09 -05:00
option="$1"
path="$2"
newname="$3"
if [ "$option" == "" ] || [ "$option" == "-h" ] || [ "$option" == "--help" ] || [ "$path" == "" ]; then
usage 0;
2024-09-02 23:54:57 -05:00
fi
2025-10-21 14:04:09 -05:00
#Make sure we're in the Yggdrasil project.
if [ $(expr match "$PWD" "$YGGDRASIL") -ne $(expr length "$YGGDRASIL") ]; then
echo "Not in the Yggdrasil directory.";
exit;
fi
#unlock this directory
chmod ug+w "$PWD"
if [ $? -ne 0 ]; then
echo Couldn\'t unlock $PWD
exit 1
fi
# Functional loop
case "$option" in
dl) ygdl ;;
yt) ygyt ;;
mp3) ygmp3 ;;
cp) ygcache cp ;;
mv) ygcache mv ;;
mkdir) ygmkdir ;;
*) usage 1 ;;
esac
2024-09-02 23:54:57 -05:00
#Lock
chmod -R ug-w "$PWD"