commit 55c53932358b4bbbe87ef9494935f8de89902da3 parent bd6dbba5223f85854797f26b3ea369d38a121985 Author: mobian <mobian@mobian> Date: Tue, 10 Nov 2020 18:19:37 +0100 Initial code Diffstat:
A | podcatch/podcatch.sh | | | 74 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1 file changed, 74 insertions(+), 0 deletions(-)
diff --git a/podcatch/podcatch.sh b/podcatch/podcatch.sh @@ -0,0 +1,74 @@ +#!/usr/bin/env bash + +if [[ ! -d "$HOME/Podcasts/.data" ]]; then + mkdir -p "$HOME/Podcasts/.data" +fi + +function check_connection { + if [[ -z $(ip a s "$1" | grep "UP") ]]; then + echo "DOWN" + else + echo "UP" + fi +} + +function sort_and_link { + # find files (recursively) and sort them by date + FILES=$(find $1 -type f -printf '%T+\t%p\n' | sort -nr | awk '{ print $2 }') + + # set counters + INDEX=0 + + # linking loop + for f in $FILES; do + + if [ $(echo "$f" | grep -E ".mp3$|.mp4$|.opus$|.m4a$") ]; then + + # increment index + INDEX=$(echo "$INDEX + 1" | bc) + + # add leading zero for 1-9 + if [ $INDEX -lt 10 ]; then + INDEX=0$INDEX + fi + + # construct new filename + PERFORMER=$(mediainfo "$f" --Inform="General;%Performer%" | sed -e 's/|//g' -e 's/\"//g') + TITLE=$(mediainfo "$f" --Inform="General;%Track%" | sed -e 's/\///g' -e 's/|//g' -e 's/\"//g') + NFN=$(echo "$PERFORMER - $TITLE" | cut -b -120) + NF="${2}/${INDEX}_$NFN" + ln -s "$f" "$NF" + + fi + done +} + +function delete_old { + # create list of days that are not deleted... + for dat in $(seq 0 1 $2); do + NOT_DEL="$NOT_DEL\|$(date +%Y-%m-%d -d -${dat}\ days)" + done + DELETE=$(find "$1" -type f -printf '%T+\t%p\n' | sort -nr | grep -v "$NOT_DEL" | awk '{ print $2 }' | grep -v "/.LOG/") + # delete old files + for f in $DELETE; do + echo "[INFO] Deleting $f" + rm "$f" + done +} + +if [[ $(check_connection wlan0) == "UP" ]]; +then + + # Download Podcasts + cpulimit --limit 25 --foreground -- podget + + # Delete old Episodes (> 7 days) + delete_old "$HOME/Podcasts/.data" 7 + + # Create collection folder + rm $HOME/Podcasts/* # does not include .data + + # Link collection + sort_and_link "$HOME/Podcasts/.data" "$HOME/Podcasts" + +fi