scripts

Scripts
git clone git://git.wimdupont.com/scripts.git
Log | Files | Refs | LICENSE

commit 5228e17590f5c678984b953bd123487de0b8ca2f
parent 2a21068cf6d4cdef67e0a1f0317818735d361b18
Author: Wim Dupont <wim@wimdupont.com>
Date:   Sat, 20 Jan 2024 10:54:48 +0100

update and added scripts

Diffstat:
MREADME.adoc | 49+++++++++++++++++++++++++++++++++++++++++++++++++
Abin/askpass | 7+++++++
Abin/auru | 21+++++++++++++++++++++
Mbin/bookmark | 23++++++++++++++---------
Mbin/coin | 6+++---
Mbin/decr | 6+++---
Mbin/encr | 6+++---
Mbin/mpvd | 14+++++++-------
Abin/mpvpop | 110+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mbin/otp | 4++--
Mbin/pclip | 4++--
Mbin/pwu | 8++++----
Mbin/radio | 15+++++++--------
Abin/ugamma | 21+++++++++++++++++++++
Mbin/yt | 1+
Mbin/ytd | 9+++++----
Mbin/ytd-album | 32+++++++++++++++++++-------------
17 files changed, 278 insertions(+), 58 deletions(-)

diff --git a/README.adoc b/README.adoc @@ -3,6 +3,29 @@ For certain scripts I've added a _"requires"_ section to specify software which is less likely to be installed by default that the script uses. Most of these can be changed with alternatives by choice if willing to edit the script. +== link:bin/askpass[askpass] + +Prompts for sudo password - using Zenity GUI only when using display server. Meant to be used with addition to the sudo.conf: + +[source, conf] +---- +Path askpass /path/to/askpass +---- + +With this, sudo -A can be used to prompt this script. + +.Requires: + +* https://gitlab.gnome.org/GNOME/zenity[zenity] + +== link:bin/auru[auru] + +Arch Linux AUR updater. Edit path to your AUR repositories in the script before running. For every updated repository, a file selector GUI will prompt to the delete outdated files. + +.Requires: + +* https://gitlab.gnome.org/GNOME/zenity[zenity] + == link:bin/bookmark[bookmark] Uses dmenu to browse and open URL's in your browser saved in a textfile (default in ~/.config/scripts/bookmarks). @@ -41,6 +64,22 @@ Uses dmenu to browse through music directory, including option to shuffle when c * http://cvs.schmorp.de/rxvt-unicode/[urxvt] * https://github.com/mpv-player/mpv[mpv] +== link:bin/mpvpop[mpvpop] + +Uses mpv socket to be able to update and grab info of currently played mpv file and/or playlist. + +Can be used using '--input-ipc-server=/tmp/mpvsocket' mpv argument. If you always want this active than you are better off adding to mpv.conf: + +[source,conf] +---- +input-ipc-server=/tmp/mpvsocket +---- + +.Requires: + +* https://github.com/mpv-player/mpv[mpv] +* https://github.com/dunst-project/dunst[dunst] + == link:bin/otp[otp] Lists files safed under the "~/.password-store/totp/" directory in dmenu or as argument and generates TOTP code based on chosen file. @@ -78,6 +117,16 @@ Uses dmenu to browse and play online radio stations saved in a textfile (default * http://cvs.schmorp.de/rxvt-unicode/[urxvt] * https://github.com/mpv-player/mpv[mpv] +== link:bin/ugamma[ugamma] + +Helper script to update X.org gamma value. + +.Requires: + +* https://github.com/dunst-project/dunst[dunst] +* https://gitlab.freedesktop.org/xorg/app/xgamma[xorg-xgamma] +* https://github.com/lcn2/calc[calc] + == link:bin/yt[yt] Uses mpv and youtube-dl (or forks such as yt-dlp) to easily look up videos from command-line with an audio-only option and an option to specify the amount of results. diff --git a/bin/askpass b/bin/askpass @@ -0,0 +1,7 @@ +#!/bin/bash + +if [[ -n $DISPLAY ]]; then + zenity --password --title="Sudo password prompt" +else + read -s -p "Sudo password: " password && echo -n "${password}" +fi diff --git a/bin/auru b/bin/auru @@ -0,0 +1,21 @@ +#!/bin/bash + +readonly AUR_DIR="$HOME/aur" +readonly AUR_LIST=($(find "${AUR_DIR}" -type d -name '.git' | sed 's/\.git//g')) + +for aur in "${AUR_LIST[@]}"; do + cd "${aur}" + git_result=$(git pull) + + test "${git_result}" == "Already up to date." && echo "${aur} up to date." && continue; + + echo "${git_result}" + + makepkg -rcsi + + IFS='|' read -ra to_delete <<< $(zenity --file-selection --title "Select old-version files to delete." --filename "${aur}/*" --multiple) + + for file_to_delete in "${to_delete[@]}"; do + rm -rfv "${file_to_delete}" + done +done diff --git a/bin/bookmark b/bin/bookmark @@ -4,22 +4,27 @@ source ~/.config/scripts/properties shopt -s nullglob globstar -font=$DMENU_FONT -lines=$DMENU_LINES +readonly FONT=$DMENU_FONT +readonly LINES=$DMENU_LINES +readonly DMENU=dmenu +readonly BOOKMARK_FILE=~/.config/scripts/bookmarks +readonly SEARCH_URI="http://localhost/search?q=" -dmenu=dmenu -file=~/.config/scripts/bookmarks +readarray -t bookmarks < "$BOOKMARK_FILE" -readarray -t bookmarks < "$file" - -bookmark=$(printf '%s\n' "${bookmarks[@]}" | "$dmenu" -fn "$font" -i -l "$lines" "$@") +bookmark=$(printf '%s\n' "${bookmarks[@]}" | "$DMENU" -fn "$FONT" -i -l "$LINES" "$@") if [ -z "${bookmark}" ]; then exit 0 fi -if [[ "${bookmark}" != https://* ]]; then - bookmark="http://localhost/search?q=${bookmark}" +if [[ ! ${bookmark} =~ ^(https?://*) ]]; then + readonly WITH_SEARCH_ENGINE=$(printf "yes\nno" | "$DMENU" -fn "$FONT" -i "$@" -p "Use search engine?") + if [[ "${WITH_SEARCH_ENGINE}" == yes ]]; then + bookmark="${SEARCH_URI}${bookmark}" + else + bookmark="https://${bookmark}" + fi fi xdg-open "${bookmark}" >/dev/null 2>&1 & disown diff --git a/bin/coin b/bin/coin @@ -26,7 +26,7 @@ shift $((OPTIND-1)) # function to join arguments with the first one being the separator -joinByString() { +join_by_string() { local separator="$1" shift local first="$1" @@ -37,7 +37,7 @@ joinByString() { # turns script arguments into api currency params if not empty if [[ ! -z "${@// }" ]] ; then - currencyParam=$(joinByString ',' "$@") + currencyParam=$(join_by_string ',' "$@") fi # grab data from coinggecko api @@ -53,5 +53,5 @@ data=$(curl -X 'GET' \ if [ -z "$dataFilter" ]; then echo ${data} | jq else - echo ${data} | jq ".[] | $(joinByString ', ' ${dataFilter[@]/#/.})" + echo ${data} | jq ".[] | $(join_by_string ', ' ${dataFilter[@]/#/.})" fi diff --git a/bin/decr b/bin/decr @@ -2,8 +2,8 @@ gpg $1 -tar=$(echo $1 | sed 's/.gpg//g') +readonly TAR=$(echo $1 | sed 's/.gpg//g') -tar -xf "$tar" +trap "shred -u $TAR" EXIT -shred -u "$tar" +tar -xf "$TAR" diff --git a/bin/encr b/bin/encr @@ -1,7 +1,7 @@ #!/bin/bash #Encrypted file with default symmetric password -sym_pwd=$HOME/.sym-pwd.gpg +readonly SYM_PWD=$HOME/.sym-pwd.gpg disable_default_pwd=0 asymmetric=0 @@ -28,8 +28,8 @@ tar -cf "$file".tar "$file" trap 'shred -u "$file".tar' EXIT if [ $asymmetric -eq 0 ]; then - if [ $disable_default_pwd -eq 0 ] && [ -f "$sym_pwd" ]; then - pwd=$(gpg --decrypt "${sym_pwd}" 2>/dev/null) + if [ $disable_default_pwd -eq 0 ] && [ -f "$SYM_PWD" ]; then + pwd=$(gpg --decrypt "${SYM_PWD}" 2>/dev/null) gpg --passphrase "${pwd}" --batch --yes -c --no-symkey-cache --cipher-algo AES256 "$file".tar else gpg -c --no-symkey-cache --cipher-algo AES256 "$file".tar diff --git a/bin/mpvd b/bin/mpvd @@ -3,10 +3,10 @@ source ~/.config/scripts/properties shopt -s nullglob globstar -font=$DMENU_FONT -lines=$DMENU_LINES -dmenu=dmenu +readonly FONT=$DMENU_FONT +readonly LINES=$DMENU_LINES +readonly DMENU=dmenu prefix=${MUSIC_DIR-~/Music} musicdirs=( "$prefix"/*/) @@ -22,11 +22,11 @@ done songdirs=( "${songdirs[@]#"$prefix"/}" ) songdirs=( "${songdirs[@]%/}" ) -songdir=$(printf '%s\n' "${songdirs[@]}" | "$dmenu" -fn "$font" -i -l "$lines" "$@") +songdir=$(printf '%s\n' "${songdirs[@]}" | "$DMENU" -fn "$FONT" -i -l "$LINES" "$@") [[ -n $songdir ]] || exit -playsongdir=$(printf "no\nyes" | "$dmenu" -fn "$font" -i "$@" -p "Play full directory?") +playsongdir=$(printf "no\nyes" | "$DMENU" -fn "$FONT" -i "$@" -p "Play full directory?") [[ -n $playsongdir ]] || exit @@ -35,10 +35,10 @@ if [[ "$playsongdir" == no ]]; then songdirs=( "$prefix"/**/*) songdirs=( "${songdirs[@]#"$prefix"/}" ) songdirs=( "${songdirs[@]%}" ) - songdir=$(printf '%s\n' "${songdirs[@]}" | "$dmenu" -fn "$font" -i -l "$lines" "$@") + songdir=$(printf '%s\n' "${songdirs[@]}" | "$DMENU" -fn "$FONT" -i -l "$LINES" "$@") [[ -n $songdir ]] || exit else - random=$(printf "no\nyes" | "$dmenu" -fn "$font" -i "$@" -p "Shuffle?") + random=$(printf "no\nyes" | "$DMENU" -fn "$FONT" -i "$@" -p "Shuffle?") [[ -n $random ]] || exit diff --git a/bin/mpvpop b/bin/mpvpop @@ -0,0 +1,110 @@ +#!/bin/bash + +shopt -s nullglob + +readonly SOCKET='/tmp/mpvsocket' + +help_msg() { + echo " + Usage: + ${PROGRAM} info + Show track info + ${PROGRAM} volume + Show current track volume or change it by passing value as argument (negative to lower) + ${PROGRAM} pause + Pause/unpause current track + ${PROGRAM} stop + Stop track/playlist + ${PROGRAM} next + Play next in playlist + ${PROGRAM} previous + Play previous in playlist + ${PROGRAM} help + Show this help menu +" +} + +trim() { + echo $1 | sed -re 's/^[[:blank:]]+|[[:blank:]]+$//g' +} + +mpv_property_data() { + printf '{ "command": ["get_property", "%s"] }\n' "$1" | socat - "${SOCKET}" | jq -r ".data" 2> /dev/null +} + +mpv_metadata() { + local -r METADATA=($(mpv_property_data "metadata")) + local -r KEYS=$(echo -n "${METADATA[*]}" | jq -r "keys[]" 2> /dev/null) + + if [[ "${KEYS[@]}" =~ 'icy-title' ]]; then + local -r RESULTDATA=$(echo "${METADATA[*]}" | jq -r '."icy-title"') + # split by divider + IFS=$'-' data=(${RESULTDATA}) + # print with newline splitter + printf "%s\n" "${data[@]}" + elif [[ "${KEYS[@]}" =~ 'title' ]]; then + echo "${METADATA[*]}" | jq -r '.artist,.title' + else + local -r FILENAME=$(mpv_property_data "filename") + # Remove extension + # print with newline splitter + printf "%s\n%s" "Track" "${FILENAME%.*}" + fi +} + +mpv_filename() { + local -r FILENAME=$(mpv_property_data "filename") + # Remove extension + echo "${FILENAME%.*}" +} + +track_info() { + local -r METADATA="$(mpv_metadata)" + + # split by newline + IFS=$'\n' data=(${METADATA[*]}) + + dunstify -h string:x-dunst-stack-tag:track-info $(trim "${data[0]}") $(trim "${data[1]}" ) +} + +track_volume() { + test ! -z $1 && echo add volume $1 | socat - "${SOCKET}" + dunstify -h string:x-dunst-stack-tag:track-volume "Track Volume": $(mpv_property_data "volume") +} + +track_pause() { + echo cycle pause | socat - "${SOCKET}" + dunstify -h string:x-dunst-stack-tag:track-pause "Track cycle pause" +} + +track_stop() { + echo stop | socat - "${SOCKET}" + dunstify -h string:x-dunst-stack-tag:track-stop "Track stopped" +} + +track_next() { + echo playlist-next | socat - "${SOCKET}" + sleep 0.5 + track_info +} + +track_prev() { + echo playlist-prev | socat - "${SOCKET}" + sleep 0.5 + track_info +} + +readonly PROGRAM="${0##*/}" +readonly COMMAND="$1" + +case "$1" in + help|--help) shift; help_msg ;; + info|--info) shift; track_info ;; + volume|--volume) shift; track_volume "$@" ;; + pause|--pause) shift; track_pause ;; + stop|--stop) shift; track_stop ;; + next|--next) shift; track_next ;; + previous|--previous|prev|--prev) shift; track_prev ;; + *) notify-send "Unknown option" "Valid arg is required. --help for available options" ;; +esac +exit 0 diff --git a/bin/otp b/bin/otp @@ -3,7 +3,7 @@ source ~/.config/scripts/properties shopt -s nullglob globstar -font=$DMENU_FONT +readonly FONT=$DMENU_FONT typeit=0 while getopts ":t" arg; do @@ -64,7 +64,7 @@ if [ -n "${value}" ]; then fi fi else - password=$(printf '%s\n' "${password_files[@]}" | "$dmenu" -fn "$font" -i ) + password=$(printf '%s\n' "${password_files[@]}" | "$dmenu" -fn "$FONT" -i ) [[ -n $password ]] || exit use_arg_file "${password}" diff --git a/bin/pclip b/bin/pclip @@ -4,7 +4,7 @@ source ~/.config/scripts/properties shopt -s nullglob globstar -font=$DMENU_FONT +readonly FONT=$DMENU_FONT shift $((OPTIND-1)) @@ -28,7 +28,7 @@ clipboards[0]=$(echo -n "Primary: ${primary}" | tr -d '\n') clipboards[1]=$(echo -n "Secondary: ${secondary}" | tr -d '\n') clipboards[2]=$(echo -n "Clipboard: ${clipboard}" | tr -d '\n') -selected=$(printf '%s\n' "${clipboards[@]}" | "$dmenu" -fn "$font" -i -l 10 "$@" ) +selected=$(printf '%s\n' "${clipboards[@]}" | "$dmenu" -fn "$FONT" -i -l 10 "$@" ) [[ -n "${selected}" ]] || exit diff --git a/bin/pwu b/bin/pwu @@ -4,7 +4,7 @@ source ~/.config/scripts/properties shopt -s nullglob globstar -font=$DMENU_FONT +readonly FONT=$DMENU_FONT typeit=0 no_console=0 @@ -63,7 +63,7 @@ function use_file () { if [[ "$url" ]]; then if [[ $quiet == 0 ]]; then if [[ $typeit -eq 0 ]]; then - openUrl=$(printf "yes\nno" | "$dmenu" -fn "$font" -i -p "Open $url in browser?") + openUrl=$(printf "yes\nno" | "$dmenu" -fn "$FONT" -i -p "Open $url in browser?") if [[ "$openUrl" == yes ]]; then xdg-open "${url}" >/dev/null 2>&1 & disown fi @@ -110,7 +110,7 @@ function use_otp () { [[ $no_console == 0 ]] || exit if [[ $quiet == 0 ]]; then read -p "Press enter to continue" - copyOtp=$(printf "yes\nno" | "$dmenu" -fn "$font" -i -p "Copy OTP to clipboard?") + copyOtp=$(printf "yes\nno" | "$dmenu" -fn "$FONT" -i -p "Copy OTP to clipboard?") if [[ "$copyOtp" == yes ]]; then clip_or_type_otp "$file" fi @@ -169,7 +169,7 @@ if [ -n "${value}" ]; then fi fi else - value=$(printf '%s\n' "${password_files[@]}" | "$dmenu" -fn "$font" -i ) + value=$(printf '%s\n' "${password_files[@]}" | "$dmenu" -fn "$FONT" -i ) [[ -n $value ]] || exit use_file "${value}" diff --git a/bin/radio b/bin/radio @@ -4,22 +4,21 @@ source ~/.config/scripts/properties shopt -s nullglob globstar -font=$DMENU_FONT -lines=$DMENU_LINES +readonly FONT=$DMENU_FONT +readonly LINES=$DMENU_LINES +readonly DMENU=dmenu +readonly FILE=~/.config/scripts/radiostations -dmenu=dmenu -file=~/.config/scripts/radiostations +readarray -t stations < "${FILE}" -readarray -t stations < "${file}" - -stationName=$(printf '%s\n' "${stations[@]}" | cut -f1 -d ';' | "$dmenu" -fn "$font" -i -l "$lines" "$@") +station_name=$(printf '%s\n' "${stations[@]}" | cut -f1 -d ';' | "$DMENU" -fn "$FONT" -i -l "$LINES" "$@") for i in "${stations[@]}" do name=$(echo "$i" | cut -f1 -d ';') - if [[ "${name}" == "${stationName}" ]]; then + if [[ "${name}" == "${station_name}" ]]; then station=$(echo "$i" | cut -f2 -d ';') urxvt -e mpv --no-video "${station}" exit 0; diff --git a/bin/ugamma b/bin/ugamma @@ -0,0 +1,21 @@ +#!/bin/bash + +#requires calc and xorg-xgamma + +while getopts "v:" arg; do + case $arg in + v) value=(${OPTARG});; + esac +done + +shift $((OPTIND-1)) + +if [ -z "${value}" ]; then + echo "No value argument (-v) given, resetting to 1.0 instead." + xgamma -gamma 1 +else + readonly GAMMA_VALUE=$(xgamma 2>&1 | awk '{print $3}' | sed "s/,//") + xgamma -gamma $(calc "${GAMMA_VALUE}" + "${value}" | sed "s/\t//g") +fi + +dunstify -h string:x-dunst-stack-tag:gamma "Gamma" "$(xgamma 2>&1 | sed 's/-> //g')" diff --git a/bin/yt b/bin/yt @@ -25,6 +25,7 @@ while getopts ":ahn:" arg; do done shift $((OPTIND-1)) + query=$@ if [[ -z "$query" ]]; then diff --git a/bin/ytd b/bin/ytd @@ -14,9 +14,10 @@ while getopts ":pas" arg; do done shift $((OPTIND-1)) + url=$* -getUrl() { +get_url() { if [[ -z "${url}" ]]; then echo "Give the search criteria to download." >&2 read -r url @@ -29,14 +30,14 @@ getUrl() { echo "${url}" } -getArgs() { +get_args() { if [ "${is_audio}" -eq 1 ]; then echo "-x -f bestaudio" fi } -url="$(getUrl)" -args="$(getArgs)" +url="$(get_url)" +args="$(get_args)" if [ "${is_playlist}" -eq 1 ]; then yt-dlp ${args} -i -o "%(playlist_index)s - %(title)s.%(ext)s" "${url}" diff --git a/bin/ytd-album b/bin/ytd-album @@ -11,8 +11,9 @@ if [ -z ${URL} ]; then read URL fi -mkdir -pv ~/tmp +readonly URL +mkdir -pv ~/tmp mkdir -pv ~/Downloads/tmp echo "Downloading $URL in ~/tmp" @@ -26,23 +27,27 @@ cd ~/Downloads/tmp && yt-dlp -x -f bestaudio -i -o "%(playlist_index)s - %(title echo "What's the band name?" read BAND +readonly BAND echo "What's the album name?" read ALBUM +readonly ALBUM echo "What's the year of release?" read YEAR +readonly YEAR echo "What's the genre?" read GENRE +readonly GENRE if [ -d "$HOME/tmp/$BAND/$ALBUM" ]; then echo "Album directory already exists - (y) or (yes) to continue." - read ANSWER - if [[ ! $ANSWER =~ ^(y|yes)$ ]]; then + read answer + if [[ ! $answer =~ ^(y|yes)$ ]]; then echo "Exiting." exit 0; fi @@ -50,7 +55,7 @@ fi mkdir -pv "$HOME/tmp/$BAND/$ALBUM" -DIR=$HOME/tmp/$BAND/$ALBUM +readonly DIR=$HOME/tmp/$BAND/$ALBUM mv $HOME/Downloads/tmp/* "$DIR" @@ -69,28 +74,29 @@ done for f in "$DIR"/*; do #remove path to get songname - SONGNAME=$(echo "$f" | sed 's/.*\///g' | sed 's/.mp3//g') + songname=$(echo "$f" | sed 's/.*\///g' | sed 's/.mp3//g') - TRACK=$(echo $SONGNAME | cut -d ' ' -f 1) - TITLE=$(echo $SONGNAME | sed "s/$TRACK - //g") + readonly TRACK=$(echo $songname | cut -d ' ' -f 1) + readonly TITLE=$(echo $songname | sed "s/$TRACK - //g") - OLDNAME=$SONGNAME + readonly OLDNAME=$songname echo "Type song name for : ${TITLE}" echo "Enter/Return to keep as is." read NEWNAME + readonly NEWNAME if [[ ! -z $NEWNAME ]]; then - SONGNAME=$NEWNAME; + songname=$NEWNAME; - echo $OLDNAME to $SONGNAME + echo $OLDNAME to $songname - mv "$DIR/$OLDNAME.mp3" "$DIR/$TRACK - $SONGNAME.mp3" + mv "$DIR/$OLDNAME.mp3" "$DIR/$TRACK - $songname.mp3" - id3 -t "$SONGNAME" -a "$BAND" -l "$ALBUM" -y "$YEAR" -n $TRACK -g "$GENRE" "$DIR/$TRACK - $SONGNAME.mp3" + id3 -t "$songname" -a "$BAND" -l "$ALBUM" -y "$YEAR" -n $TRACK -g "$GENRE" "$DIR/$TRACK - $songname.mp3" else - id3 -t "$TITLE" -a "$BAND" -l "$ALBUM" -y "$YEAR" -n "$TRACK" -g "$GENRE" "$DIR/$SONGNAME.mp3" + id3 -t "$TITLE" -a "$BAND" -l "$ALBUM" -y "$YEAR" -n "$TRACK" -g "$GENRE" "$DIR/$songname.mp3" fi done