Last active
May 3, 2025 16:25
-
-
Save h8rt3rmin8r/d8210c35b46bd12230cdd0354c9375e1 to your computer and use it in GitHub Desktop.
Manage a collection of audio tracks from YouTube
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
#==============================================================================# | |
# __ _______ ___ _ _ # | |
# \ \ / /_ _/ _ \ | (_) # | |
# \ V / | |/ /_\ \_ _ __| |_ ___ # | |
# \ / | || _ | | | |/ _` | |/ _ \ # | |
# | | | || | | | |_| | (_| | | (_) | # | |
# |_/ |_/\_| |_/\__,_|\__,_|_|\___/ # | |
# # | |
# Manage a collection of audio tracks from YouTube # | |
# # | |
#------------------------------------------------------------------------------# | |
# # | |
# USAGE: # | |
# # | |
# <URL> | Download YouTube audio track from "URL" # | |
# -h, --help | Print this help text information # | |
# --logs-dump | Dump all YTAudio log data to STDOUT # | |
# --logs-last (<NUM>) | Print the last 10 (or "NUM") log entries # | |
# --logs-list | List all YTAudio log file names # | |
# -p, --playlist <URL> | Download all videos in the playlist at "URL" # | |
# -u, --update | Update script software dependencies # | |
# # | |
# EXAMPLES: # | |
# # | |
# (Download the audio from a YouTube video) # | |
# ytaudio "https://www.youtube.com/watch?v=mlQX_MGA1gc" # | |
# # | |
# (Print the last 275 lines from the YTAudio log files) # | |
# ytaudio --logs-last 275 # | |
# # | |
# INSTALL: # | |
# # | |
# (To install ytaudio, make the script executable and run it.) # | |
# # | |
# sudo chmod +x ytaudio.sh # | |
# ./ytaudio.sh # | |
# rm ytaudio.sh # | |
# # | |
# (The script will set up everything automatically.) # | |
# # | |
#------------------------------------------------------------------------------# | |
# # | |
# AUTHOR: h8rt3rmin8r ([email protected]) # | |
# VERSION: 20191109 # | |
# SOURCE: https://pastebin.com/raw/xtwbUcGQ # | |
# REFERENCE: http://ytdl-org.github.io/youtube-dl/ # | |
# # | |
#==============================================================================# | |
#<start># | |
# Declare variables | |
_q1=$'\u0027' | |
_q2=$'\u0022' | |
user=$(cd; pwd | cut -c 7-) | |
env_brc="/home/${user}/.bashrc" | |
nonce="${RANDOM:0:1}${RANDOM:0:1}${RANDOM:0:1}${RANDOM:0:1}" | |
runtime=$(date '+%s%N') | |
target="" | |
script_file="${0//*\/}" | |
script_name="${script_file//.*}" | |
s_na=$(echo '['$(printf "\e[1;92m${script_name}\e[0m")'] ') | |
tcfe="${s_na}$(echo -en '[_\e[38;5;196merror\e[0m_]')" | |
tcfi="${s_na}$(echo -en '[_\e[38;5;25minfo\e[0m__]')" | |
tcfs="${s_na}$(echo -en '[\e[38;5;82msuccess\e[0m]')" | |
tmpd="/tmp/.${runtime}_${nonce}" | |
yta_inst="N" | |
dir_here="$(pwd)" | |
dir_home="/home/${user}" | |
dir_music="${dir_home}/Music" | |
dir_yta="${dir_music}/yta" | |
yta_audio="${dir_yta}/audio" | |
yta_exif="${dir_yta}/exif" | |
yta_graphics="${dir_yta}/graphics" | |
yta_logs="${dir_yta}/logs" | |
yta_src="${dir_yta}/src" | |
yta_src_cpy="${yta_src}/ytaudio.sh" | |
yta_src_ctl="/usr/local/bin/ytaudio" | |
yta_link="https://gist.githubusercontent.com/h8rt3rmin8r/d8210c35b46bd12230cdd0354c9375e1/raw/3b3dbc79ce6d657e982abfafddc0a13d1f23ec55/ytaudio.sh" | |
ytdl_src="/usr/bin/youtube-dl" | |
ytdl_link="https://github.com/yt-dlp/yt-dlp/releases/latest/download/yt-dlp_linux" | |
# Function declarations | |
function yta_askif() { | |
# Interactive questioning function | |
INST="" | |
if [ -t 0 ]; then | |
local IN=( $(echo -n "$@") ) | |
else | |
local IN=( $(</dev/stdin) $(echo -n "$@") ) | |
fi | |
INST=( $(echo ${IN[@]}) ) | |
# Ask the question to the user | |
echo ##==> line break | |
read -p " $(echo ${INST[@]}) (Y/N): " -n 1 REPLY | |
echo | |
echo ##==> line break | |
# Parse the response and eliminate any potential injections then | |
# export the new variable "OUST" back to the calling function | |
local chk_reply="${REPLY//[^A-Za-z]}" | |
local chk_reply="${chk_reply:0:1}" | |
case $(echo "${chk_reply}" | tr '[A-Z]' '[a-z]') in | |
y) | |
OUST="Y" | |
export ${OUST} | |
return $? | |
;; | |
n) | |
OUST="N" | |
export ${OUST} | |
return $? | |
;; | |
*|'') | |
OUST="X" | |
export ${OUST} | |
return $? | |
;; | |
esac | |
} | |
function yta_check_depends() { | |
local dc_a=$(which ffmpeg &>/dev/null; echo $?) | |
local dc_b=$(which dos2unix &>/dev/null; echo $?) | |
local dc_c=$(which attr &>/dev/null; echo $?) | |
local dc_d=$(yta_check_exe "${ytdl_src}") | |
local dc_e=$(cat "${env_brc}" 2>/dev/null | grep '^alias ' | grep 'youtube-dl' &>/dev/null; echo $?) | |
local dc_f=$(cat "${env_brc}" 2>/dev/null | grep '^alias ' | grep 'ytdl' &>/dev/null; echo $?) | |
local dc_g=$(cat "${env_brc}" 2>/dev/null | grep '^alias ' | grep 'ytaudio' &>/dev/null; echo $?) | |
local dep_chk="${dc_a}${dc_b}${dc_c}${dc_d}${dc_e}${dc_f}${dc_g}" | |
if [[ "${dep_chk}" =~ [^0] ]]; then | |
yta_logger -e "Missing required software or configuration settings" | |
echo "${tcfi} Missing required software or configuration settings" | |
yta_askif "Install required software and configurations?" | |
while [[ ! "${OUST}" =~ [YN] ]]; do | |
echo "${tcfe} Unknown response" | |
echo "${tcfi} Please input either 'Y' (for yes) or 'N' (for no)." | |
yta_askif "Install required software and configurations?" | |
done | |
case "${OUST}" in | |
Y) yta_depends_install ;; | |
N) | |
yta_logger -e "User aborted ytaudio processes" | |
echo "${tcfe} Aborting all ytaudio processes" | |
yta_selfdestruct | |
exit 1 | |
;; | |
esac | |
fi | |
return 0 | |
} | |
function yta_check_exe() { | |
# Check if an input is executable | |
if [[ -x "${1}" ]]; then | |
echo "0" | |
return 0 | |
else | |
echo "1" | |
return 1 | |
fi | |
} | |
function yta_check_hash() { | |
# Compare self hash with input file content hash | |
local hash_s=$(cat "${0}" | md5sum | cut -d ' ' -f1) | |
local hash_i=$(cat "${1}" | md5sum | cut -d ' ' -f1) | |
if [[ "${hash_s}" == "${hash_i}" ]]; then | |
echo 0 | |
return 0 | |
else | |
yta_logger -e "Hash match failed: (${hash_s} vs ${hash_i})" | |
echo 1 | |
return 1 | |
fi | |
} | |
function yta_check_local() { | |
# Check for proper system structure and required componants | |
## directory checks | |
local c_01=$(if [[ -d "${dir_yta}" ]]; then echo 0; else echo 1; fi) | |
local c_02=$(if [[ -d "${yta_audio}" ]]; then echo 0; else echo 1; fi) | |
local c_03=$(if [[ -d "${yta_exif}" ]]; then echo 0; else echo 1; fi) | |
local c_04=$(if [[ -d "${yta_graphics}" ]]; then echo 0; else echo 1; fi) | |
local c_05=$(if [[ -d "${yta_logs}" ]]; then echo 0; else echo 1; fi) | |
local c_06=$(if [[ -d "${yta_src}" ]]; then echo 0; else echo 1; fi) | |
## file checks | |
local c_07=$(if [[ -f "${yta_src_cpy}" ]]; then echo 0; else echo 1; fi) | |
local c_08=$(if [[ -f "${yta_src_ctl}" ]]; then echo 0; else echo 1; fi) | |
## source code checks | |
local c_09=$(yta_check_hash "${yta_src_cpy}") | |
local c_10=$(yta_check_hash "${yta_src_ctl}") | |
## control script executable check | |
local c_11=$(yta_check_exe "${yta_src_ctl}") | |
## concatenate all check outputs into a string and check for any failures | |
local c_all="${c_01}${c_02}${c_03}${c_04}${c_05}${c_06}${c_07}${c_08}${c_09}${c_10}${c_11}" | |
if [[ "${c_all}" =~ [^0] ]]; then | |
yta_logger -i "Additional configurations are required" | |
echo "${tcfi} Additional configurations are required" | |
yta_askif "Would you like to auto configure ytaudio now?" | |
while [[ ! "${OUST}" =~ [YN] ]]; do | |
echo "${tcfe} Unknown response. Please input either 'Y' (for yes) or 'N' (for no)." | |
yta_askif "Would you like to auto configure ytaudio now?" | |
done | |
case "${OUST}" in | |
Y) | |
yta_setup | |
;; | |
N) | |
yta_logger -e "User aborted ytaudio processes" | |
echo "${tcfi} Aborting all ytaudio processes" | |
yta_selfdestruct | |
exit 1 | |
;; | |
esac | |
fi | |
return 0 | |
} | |
function yta_check_target() { | |
# Check target URLs to make sure they will work | |
if [[ "${1}" =~ "youtube.com" || "${1}" =~ "youtu.be" ]]; then | |
yta_logger -s "Input URL validated successfully" | |
echo "0" | |
return 0 | |
else | |
yta_logger -e "Input URL vailed validation" | |
echo "1" | |
return 1 | |
fi | |
} | |
function yta_depends_install() { | |
# Install and configure required software packages (youtube-dl, ffmpeg, etc.) | |
echo "${tcfi} Beginning installation of required software" | |
## get access to 'sudo' | |
echo "${tcfi} This process will require access to 'sudo'..." | |
sudo echo -n "" | |
## kill the script if sudo can't be accessed | |
if [[ "$?" -ne 0 ]]; then | |
yta_logger -e "No access to sudo" | |
echo "${tcfe} Unable to get access to 'sudo'." | |
echo "${tcfi} Aborting software installation process" | |
exit 1 | |
fi | |
## install new stuff ... | |
local dep_chk_a=$(which ffmpeg &>/dev/null; echo $?) | |
local dep_chk_b=$(which dos2unix &>/dev/null; echo $?) | |
local dep_chk_c=$(which attr &>/dev/null; echo $?) | |
local dep_chk="${dep_chk_a}${dep_chk_b}${dep_chk_c}" | |
if [[ "${dep_chk}" =~ [^0] ]]; then | |
sudo apt-get update | |
if [[ "${dep_chk_a}" -ne 0 ]]; then | |
yta_logger -i "Installing ffmpeg" | |
sudo apt install ffmpeg | |
fi | |
if [[ "${dep_chk_b}" -ne 0 ]]; then | |
yta_logger -i "Installing dos2unix" | |
sudo apt install dos2unix | |
fi | |
if [[ "${dep_chk_c}" -ne 0 ]]; then | |
yta_logger -i "Installing attr" | |
sudo apt install attr | |
fi | |
fi | |
local dep_chk=$(yta_check_exe "${ytdl_src}") | |
if [[ "${dep_chk}" -ne 0 ]]; then | |
yta_logger -i "Installing youtube-dl" | |
echo "${tcfi} Downloading youtube-dl (this may take a minute) ..." | |
#sudo curl -s -L "${ytdl_link}" -o ${ytdl_src} | |
sudo apt-get install yt-dlp | |
if [[ "$?" -ne 0 ]]; then | |
yta_logger -e "Encountered errors while downloading youtube-dl" | |
echo "${tcfe} An error occurred whild downloading the required source code!" | |
echo "${tcfe} Try installing manually from here: ${ytdl_link}" | |
echo "${tcfi} Aborting software installation process" | |
exit 1 | |
fi | |
sudo chmod a+rx "${ytdl_src}" | |
fi | |
## add necessary aliases to the system's bashrc file | |
touch "${env_brc}" | |
local env_chk_a=$(cat "${env_brc}" 2>/dev/null | grep '^alias ' | grep 'youtube-dl' &>/dev/null; echo $?) | |
local env_chk_b=$(cat "${env_brc}" 2>/dev/null | grep '^alias ' | grep 'ytdl' &>/dev/null; echo $?) | |
local env_chk_c=$(cat "${env_brc}" 2>/dev/null | grep '^alias ' | grep 'ytaudio' &>/dev/null; echo $?) | |
if [[ "${env_chk_a}" -ne 0 ]]; then | |
yta_logger -i "Adding youtube-dl alias into .bashrc" | |
echo "${tcfi} Configurations for youtube-dl not found in .bashrc" | |
echo "${tcfi} Setting alias 'youtube-dl' ==> '/usr/local/bin/youtube-dl'" | |
echo "" >> "${env_brc}" | |
echo "## Added by ytaudio on $(date '+%Y%m%d')" >> "${env_brc}" | |
echo "alias youtube-dl=${_q1}${ytdl_src}${_q1}" >> "${env_brc}" | |
echo "${tcfs} alias configuration for 'youtube-dl' complete" | |
fi | |
if [[ "${env_chk_b}" -ne 0 ]]; then | |
yta_logger -i "Adding ytdl alias into .bashrc" | |
echo "${tcfi} Configurations for ytdl not found in .bashrc" | |
echo "${tcfi} Setting alias 'ytdl' ==> '/usr/local/bin/youtube-dl'" | |
echo "" >> "${env_brc}" | |
echo "## Added by ytaudio on $(date '+%Y%m%d')" >> "${env_brc}" | |
echo "alias ytdl=${_q1}${ytdl_src}${_q1}" >> "${env_brc}" | |
echo "${tcfs} alias configuration for 'ytdl' complete" | |
fi | |
if [[ "${env_chk_c}" -ne 0 ]]; then | |
yta_logger -i "Adding yta alias into .bashrc" | |
echo "${tcfi} Configurations for yta not found in .bashrc" | |
echo "${tcfi} Setting alias 'yta' ==> '/usr/local/bin/ytaudio'" | |
echo "" >> "${env_brc}" | |
echo "## Added by ytaudio on $(date '+%Y%m%d')" >> "${env_brc}" | |
echo "alias yta=${_q1}${yta_src_ctl}${_q1}" >> "${env_brc}" | |
echo "${tcfs} alias configuration for 'yta' complete" | |
fi | |
## re-source the system environment and run the basic setup function | |
yta_logger -i "Re-sourcing the system environment at ${env_brc}" | |
echo "${tcfi} Re-sourcing the system environment at ${env_brc}" | |
source ${env_brc} &>/dev/null | |
yta_setup | |
## done | |
yta_logger -i "Software and configurations installed" | |
echo "${tcfs} Installation of required software and configurations is complete" | |
echo "${tcfi} Use '--help' for script usage information" | |
export yta_inst="Y" | |
return 0 | |
} | |
function yta_depends_update() { | |
# Update and configure required software packages | |
echo "${tcfi} Updating required software and configuration settings" | |
## get access to 'sudo' | |
echo "${tcfi} This process will require access to 'sudo'..." | |
sudo echo -n "" | |
## kill the script if sudo can't be accessed | |
if [[ "$?" -ne 0 ]]; then | |
yta_logger -e "No access to sudo" | |
echo "${tcfe} Unable to get access to 'sudo'." | |
echo "${tcfi} Aborting the update process" | |
exit 1 | |
fi | |
yta_logger -i "Downloading source: ${ytdl_link}" | |
echo "${tcfi} Downloading youtube-dl (this may take a minute) ..." | |
sudo curl -s -L "${ytdl_link}" -o ${ytdl_src} | |
if [[ "$?" -ne 0 ]]; then | |
yta_logger -e "Errors encountered while downloading the youtube-dl source" | |
echo "${tcfe} An error occurred whild downloading the required source code!" | |
echo "${tcfe} Try installing manually from here: ${ytdl_link}" | |
echo "${tcfi} Aborting software installation process" | |
exit 1 | |
fi | |
sudo chmod a+rx "${ytdl_src}" | |
yta_logger -i "Downloading source: ${yta_link}" | |
echo "${tcfi} Downloading the latest version of ytaudio" | |
local hd_ustr_a="Mozilla/5.0 (Windows NT 6.2; Win64; x64) " | |
local hd_ustr_b="${hd_ustr_a}AppleWebKit/537.36 (KHTML, like Gecko) " | |
local hd_ustr_c="${hd_ustr_b}Chrome/60.0.3112.90 Safari/537.36" | |
local i_dom_x="pastebin.com" | |
local auth_x="Authority: ${i_dom_x}" | |
local orig_x="Origin: https://${i_dom_x}" | |
local refr_x="Referer: ${yta_link}" | |
local host_x="Host: ${i_dom_x}" | |
local usag_x="User-Agent: ${hd_ustr_c}" | |
curl -s -L "${yta_link}" --location-trusted --connect-timeout 3 \ | |
-H "${usag_x}" -H "${auth_x}" -H "${orig_x}" -H "${refr_x}" -H "${host_x}" \ | |
-H "Accept: */*" \ | |
-H 'Upgrade-Insecure-Requests: 1' \ | |
-H 'Cache-Control: max-age=0' \ | |
-H 'TE: Trailers' \ | |
-H 'Pragma: no-cache' \ | |
-H "Connection: keep-alive" \ | |
-H "DNT: 1" | dos2unix > /tmp/tmpfyta.sh | |
if [[ "$?" -ne 0 ]]; then | |
yta_logger -e "Errors encountered while downloading the ytaudio source" | |
echo "${tcfe} An error occurred whild downloading the required source code!" | |
echo "${tcfe} Try installing manually from here: ${yta_link}" | |
echo "${tcfi} Aborting software installation process" | |
rm .tmpcmd.sh &>/dev/null | |
exit 1 | |
fi | |
yta_logger -s "Download successful" | |
yta_logger -i "Running the new source and executing a self-destruct" | |
echo "${tcfs} Download successful" | |
echo "${tcfi} Running the new source and executing a self-destruct" | |
sudo chmod +x /tmp/tmpfyta.sh | |
/tmp/tmpfyta.sh --selfdestruct | |
return 0 | |
} | |
function yta_help() { | |
# YTAudio help text function | |
yta_logger -i "Printing ytaudio help text with yta_help" | |
local ln_pre=$(cat "${0}" | grep -no '^#[<]start[>]#' | cut -d ':' -f1) | |
((ln_end=$ln_pre-1)) | |
cat "${0}" | head -n "${ln_end}" | tail -n +2 | tr '#' ' ' | |
return 0 | |
} | |
function yta_logger() { | |
# General script logging function | |
if [[ "${1:0:1}" =~ [-] ]]; then | |
local l_opt="${1//[-]}" | |
shift 1 | |
else | |
local l_opt="-i" | |
fi | |
local l_message="$@" | |
case "${l_opt}" in | |
e) | |
local l_type="E" | |
;; | |
i) | |
local l_type="I" | |
;; | |
s) | |
local l_type="S" | |
;; | |
esac | |
local l_file="${yta_logs}/$(date '+%Y%m').log" | |
local l_self="${script_name}" | |
local l_timestamp="$(date '+%s%N')" | |
if [[ ! -d "${yta_logs}" ]]; then | |
mkdir -p "${yta_logs}" | |
fi | |
if [[ ! -f "${l_file}" ]]; then | |
touch "${l_file}" | |
fi | |
echo "${l_timestamp}|${l_self}|${l_type}|${l_message}" >> "${l_file}" | |
return $? | |
} | |
function yta_logger_dump() { | |
# Dump all logger content to the standard output | |
yta_logger -i "Dumped all log files with yta_logger_dump" | |
declare -a f_array=( $(yta_lsf "${yta_logs}") ) | |
for i in "${f_array[@]}"; do | |
cat "${yta_logs}/${i}" 2>/dev/null | |
done \ | |
| sort -n | |
unset f_array | |
return 0 | |
} | |
function yta_logger_last() { | |
# Print the latest entries from the YTAudio log files | |
yta_logger -i "Last logfile entries printed with yta_logger_last" | |
declare -a f_array=( $(yta_lsf "${yta_logs}") ) | |
if [[ "x${1}" == "x" ]]; then | |
for i in "${f_array[@]}"; do | |
cat "${yta_logs}/${i}" 2>/dev/null | |
done \ | |
| sort -n \ | |
| tail -n 10 | |
else | |
local ln_count="${1#0}" | |
for i in "${f_array[@]}"; do | |
cat "${yta_logs}/${i}" 2>/dev/null | |
done \ | |
| sort -n \ | |
| tail -n "${ln_count}" | |
fi | |
unset f_array | |
return 0 | |
} | |
function yta_logger_list() { | |
# List all available log file names | |
yta_logger -i "Log files listed with yta_logger_list" | |
yta_lsf "${yta_logs}" | |
return 0 | |
} | |
function yta_lsf() { | |
# List all valid non-hidden files within a directory | |
local lsf_in="$1" | |
local t_d=$(pwd) | |
if [[ "x${lsf_in}" == "x" ]]; then | |
ls -CF "${t_d}" \ | |
| tr '\t' '\n' \ | |
| sed 's/\ \{2\}/\n/g' \ | |
| sed '/^[ ]*$/d' \ | |
| sed 's/^\ *//' \ | |
| sed 's/[*@]$//' \ | |
| sed '/[/]/d' \ | |
| sort | |
return 0 | |
else | |
if [[ -d "${lsf_in}" ]]; then | |
local t_d="${lsf_in}" | |
ls -CF "${t_d}" \ | |
| tr '\t' '\n' \ | |
| sed 's/\ \{2\}/\n/g' \ | |
| sed '/^[ ]*$/d' \ | |
| sed 's/^\ *//' \ | |
| sed 's/[*@]$//' \ | |
| sed '/[/]/d' \ | |
| sort | |
else | |
return 1 | |
fi | |
fi | |
} | |
function yta_run() { | |
# Execute core downloading operations | |
local hd_ustr_a="Mozilla/5.0 (Windows NT 6.2; Win64; x64) " | |
local hd_ustr_b="${hd_ustr_a}AppleWebKit/537.36 (KHTML, like Gecko) " | |
local hd_ustr_c="${hd_ustr_b}Chrome/60.0.3112.90 Safari/537.36" | |
local uag="${hd_ustr}" | |
yta_logger -i "Downloading resource: ${1}" | |
youtube-dl \ | |
--user-agent "${uag}" \ | |
--limit-rate 2.5M \ | |
--ignore-errors \ | |
--no-playlist \ | |
--restrict-filenames \ | |
--no-overwrites \ | |
--abort-on-unavailable-fragment \ | |
--write-info-json \ | |
--write-thumbnail \ | |
--extract-audio \ | |
--audio-format mp3 \ | |
--audio-quality 5 \ | |
--add-metadata \ | |
--embed-thumbnail \ | |
--xattrs "${1}" | |
if [[ "$?" -eq 0 ]]; then | |
yta_logger -s "Download and post-processing completed successfully" | |
return 0 | |
else | |
yta_logger -e "Errors encountered while downloading the target" | |
return 1 | |
fi | |
} | |
function yta_run_playlist() { | |
# Execute playlist downloading operations | |
local hd_ustr_a="Mozilla/5.0 (Windows NT 6.2; Win64; x64) " | |
local hd_ustr_b="${hd_ustr_a}AppleWebKit/537.36 (KHTML, like Gecko) " | |
local hd_ustr_c="${hd_ustr_b}Chrome/60.0.3112.90 Safari/537.36" | |
local uag="${hd_ustr}" | |
yta_logger -i "Downloading resource: ${1}" | |
youtube-dl \ | |
--user-agent "${uag}" \ | |
--limit-rate 2.5M \ | |
--ignore-errors \ | |
--yes-playlist \ | |
--restrict-filenames \ | |
--no-overwrites \ | |
--abort-on-unavailable-fragment \ | |
--write-info-json \ | |
--write-thumbnail \ | |
--extract-audio \ | |
--audio-format mp3 \ | |
--audio-quality 5 \ | |
--add-metadata \ | |
--embed-thumbnail \ | |
--xattrs "${1}" | |
if [[ "$?" -eq 0 ]]; then | |
yta_logger -s "Download and post-processing completed successfully" | |
return 0 | |
else | |
yta_logger -e "Errors encountered while downloading the target" | |
return 1 | |
fi | |
} | |
function yta_selfdestruct() { | |
# Self destruct function (for tempfile-driven update process) | |
if [[ "${script_name}" =~ tmpfyta ]]; then | |
yta_logger -i "Executing self-destruction" | |
rm "${0}" 2>/dev/null | |
fi | |
return 0 | |
} | |
function yta_setup() { | |
# Build the ytaudio system | |
mkdir -p "${yta_audio}" | |
mkdir -p "${yta_exif}" | |
mkdir -p "${yta_graphics}" | |
mkdir -p "${yta_logs}" | |
mkdir -p "${yta_src}" | |
touch "${yta_src_cpy}" &>/dev/null | |
if [[ "$?" -ne 0 ]]; then | |
yta_logger -e "Errors encountered while executing 'touch'; Trying again with 'sudo'" | |
sudo touch "${yta_src_cpy}" &>/dev/null | |
fi | |
touch "${yta_src_ctl}" &>/dev/null | |
if [[ "$?" -ne 0 ]]; then | |
sudo touch "${yta_src_ctl}" &>/dev/null | |
yta_logger -e "Errors encountered while executing 'touch'; Trying again with 'sudo'" | |
fi | |
## copy self to the source code reference file and the control script | |
local hash_slf=$(cat "${0}" 2>/dev/null | md5sum | cut -d ' ' -f1) | |
local hash_cpy=$(cat "${yta_src_cpy}" 2>/dev/null | md5sum | cut -d ' ' -f1) | |
local hash_ctl=$(cat "${yta_src_ctl}" 2>/dev/null | md5sum | cut -d ' ' -f1) | |
if [[ ! "${hash_slf}" == "${hash_cpy}" ]]; then | |
cat "${0}" > "${yta_src_cpy}" 2>/dev/null | |
if [[ "$?" -ne 0 ]]; then | |
yta_logger -e "Errors encountered while writing file; Trying again with 'sudo'" | |
cat "${0}" \ | |
| sudo tee "${yta_src_ctl}" &>/dev/null | |
fi | |
fi | |
if [[ ! "${hash_slf}" == "${hash_ctl}" ]]; then | |
cat "${0}" \ | |
| sudo tee "${yta_src_ctl}" &>/dev/null | |
fi | |
## make sure the control script is executable | |
local chk_exe=$(yta_check_exe "${yta_src_ctl}") | |
if [[ ! "${chk_exe}" == "0" ]]; then | |
sudo echo -n "" | |
if [[ "$?" -ne 0 ]]; then | |
echo "${tcfe} Access to 'sudo' could not be acquired." | |
echo "${tcfi} Aborting system configuration process" | |
yta_logger -e "No access to sudo" | |
exit 1 | |
fi | |
sudo chmod +x "${yta_src_ctl}" | |
fi | |
## done | |
yta_logger -i "Local files and directories configured with yta_setup" | |
echo "${tcfs} Local files and directories have been configured" | |
echo "${tcfi} Use '--help' for script usage information" | |
export yta_inst="Y" | |
return 0 | |
} | |
# Verify the presence of required software and configurations | |
yta_check_depends | |
yta_check_local | |
yta_logger -i "ytaudio STARTED" | |
yta_logger -s "All ytaudio dependencies are met" | |
## run self destruct function to destroy the script if it is a temp file | |
yta_selfdestruct | |
# Process inputs and execute operations | |
if [[ "x${1}" == "x" ]]; then | |
case "${yta_inst}" in | |
Y) | |
## if new installations were just performed, kill the function | |
## with a zero exit code | |
exit 0 | |
;; | |
N) | |
## if new installations were NOT just performed, then kill the | |
## function with a negative exit code and print an error message | |
echo "${tcfe} No inputs detected (use '--help' for more information)" | |
yta_logger -e "No inputs detected" | |
yta_logger -e "All processes aborted" | |
exit 1 | |
;; | |
esac | |
fi | |
case "${1//[-]}" in | |
h|help) | |
yta_help | |
exit 0 | |
;; | |
logsdump|dumplogs) | |
yta_logger_dump | |
exit 0 | |
;; | |
logslast|logslatest) | |
if [[ "${2}" =~ [1-9] ]]; then | |
yta_logger_last "${2//[^0-9]}" | |
else | |
yta_logger_last | |
fi | |
exit 0 | |
;; | |
logslist|listlogs) | |
yta_logger_list | |
exit 0 | |
;; | |
p|pl|playlist) | |
if [[ "$#" -ne 2 ]]; then | |
yta_logger -e "Incorrect number of inputs: $#" | |
echo "${tcfe} Incorrect number of inputs: $#" | |
exit 1 | |
fi | |
shift 1 | |
target="${1}" | |
if [[ "$(yta_check_target ${target})" != 0 ]]; then | |
exit 1 | |
fi | |
mkdir -p "${tmpd}" | |
cd "${tmpd}" | |
yta_run_playlist "${target}" | |
;; | |
selfdestruct) | |
yta_selfdestruct | |
exit 0 | |
;; | |
u|update) | |
yta_logger -i "Updating system dependencies (manually requested)" | |
yta_depends_update | |
exit $? | |
;; | |
*) | |
if [[ "$#" -gt 1 ]]; then | |
yta_logger -e "Incorrect number of inputs: $#" | |
echo "${tcfe} Incorrect number of inputs: $#" | |
exit 1 | |
fi | |
target="${1}" | |
if [[ "$(yta_check_target ${target})" != 0 ]]; then exit 1; fi | |
mkdir -p "${tmpd}" | |
cd "${tmpd}" | |
yta_run "${target}" | |
;; | |
esac | |
if [[ "$?" != 0 ]]; then | |
cd "${dir_here}" | |
rm -rf "${tmpd}" | |
yta_logger -e "Errors encountered. Aborting ytaudio operations" | |
exit 1 | |
fi | |
## move new files into the respective directories based on output file type | |
declare -a f_array=( $(yta_lsf) ) | |
for i in "${f_array[@]}"; do | |
if [[ "${i}" =~ [.]json$ ]]; then | |
mv "${i}" "${yta_exif}/" | |
elif [[ "${i}" =~ [.]mp3$ ]]; then | |
mv "${i}" "${yta_audio}/" | |
else | |
mv "${i}" "${yta_graphics}" | |
fi | |
done | |
## return to the current working directory and die gracefully | |
cd "${dir_here}" | |
rm -rf "${tmpd}" | |
yta_logger -s "All ytaudio operations completed successfully" | |
unset f_array | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment