From 9eab12a7cf02d8485bd86b46e6f3199d5f3160ec Mon Sep 17 00:00:00 2001 From: Joshua Pickard Date: Fri, 5 May 2023 18:33:51 +0000 Subject: [PATCH] Add 'bash/script.sh' --- bash/script.sh | 139 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 139 insertions(+) create mode 100644 bash/script.sh diff --git a/bash/script.sh b/bash/script.sh new file mode 100644 index 0000000..e81fd6c --- /dev/null +++ b/bash/script.sh @@ -0,0 +1,139 @@ +#!/bin/bash + +# Check if dialog is installed, if not, install it +if ! command -v dialog &> /dev/null +then + if command -v apt-get &> /dev/null + then + sudo apt-get install -y dialog + elif command -v pacman &> /dev/null + then + sudo pacman -S --noconfirm dialog + elif command -v dnf &> /dev/null + then + sudo dnf install -y dialog + else + echo "Could not install dialog. Please install it manually." + exit 1 + fi +fi + +# Check if wget is installed, if not, install it +if ! command -v wget &> /dev/null +then + if command -v apt-get &> /dev/null + then + sudo apt-get install -y wget + elif command -v pacman &> /dev/null + then + sudo pacman -S --noconfirm wget + elif command -v dnf &> /dev/null + then + sudo dnf install -y wget + else + echo "Could not install wget. Please install it manually." + exit 1 + fi +fi + +# Check if unzip is installed, if not, install it +if ! command -v unzip &> /dev/null +then + if command -v apt-get &> /dev/null + then + sudo apt-get install -y unzip + elif command -v pacman &> /dev/null + then + sudo pacman -S --noconfirm unzip + elif command -v dnf &> /dev/null + then + sudo dnf install -y unzip + else + echo "Could not install unzip. Please install it manually." + exit 1 + fi +fi + +# Check if awk is installed, if not, install it +if ! command -v awk &> /dev/null +then + if command -v apt-get &> /dev/null + then + sudo apt-get install -y gawk + elif command -v pacman &> /dev/null + then + sudo pacman -S --noconfirm gawk + elif command -v dnf &> /dev/null + then + sudo dnf install -y gawk + else + echo "Could not install awk. Please install it manually." + exit 1 + fi +fi + +# Check if sed is installed, if not, install it +if ! command -v sed &> /dev/null +then + if command -v apt-get &> /dev/null + then + sudo apt-get install -y sed + elif command -v pacman &> /dev/null + then + sudo pacman -S --noconfirm sed + elif command -v dnf &> /dev/null + then + sudo dnf install -y sed + else + echo "Could not install sed. Please install it manually." + exit 1 + fi +fi + +# Process command line arguments +while getopts ":i:" opt; do + case $opt in + i) + urls_file=$OPTARG + ;; + \?) + echo "Invalid option: -$OPTARG" >&2 + exit 1 + ;; + :) + echo "Option -$OPTARG requires an argument." >&2 + exit 1 + ;; + esac +done + +# Ask for input if no command line argument is given +if [ -z "$urls_file" ]; then + dialog --backtitle "Download Videos" \ + --title "Enter URLs" \ + --inputbox "Enter the URLs separated by spaces:" 8 60 2> urls.txt + urls=$(cat urls.txt) +else + urls=$(cat "$urls_file") +fi + +# Create Videos directory in current directory +mkdir -p "$PWD/Videos" + +# Download and extract videos +while read -r url; do + echo "Downloading $url ..." + wget --progress=bar:force "$url" -P "$PWD" + file_name=$(basename "$url") + if [[ $file_name != *.* ]]; then + mv "$file_name" "$file_name.zip" + file_name="$file_name.zip" + fi + echo "Extracting $file_name ..." + unzip -q "$file_name" -d "$PWD/Videos" + while [ "`find . -type f -name '*.zip' | wc -l`" -gt 0 ]; do find -type f -name "*.zip" -exec unzip -- '{}' \; -exec rm -- '{}' \;; done + find "$PWD" -type f -name "*.mp4" -exec mv {} "$PWD/Videos" \; + find "$PWD" -type f -name "*.zip" -exec rm {} \; + find "$PWD" -type d -empty -delete +done <<< "$urls" +