pinephone-scripts

Scripts and small applications for the PinePhone
git clone https://src.jayvii.de/pub/pinephone-scripts.git
Log | Files | Refs | README | LICENSE

podcatch.sh (1916B)


      1 #!/usr/bin/env bash
      2 
      3 if [[ ! -d "$HOME/Podcasts/.data" ]]; then
      4     mkdir -p "$HOME/Podcasts/.data"
      5 fi
      6 
      7 function check_connection {
      8     if [[ -z $(ip a s "$1" | grep "UP") ]]; then
      9         echo "DOWN"
     10     else
     11         echo "UP"
     12     fi
     13 }
     14 
     15 function sort_and_link {
     16     # find files (recursively) and sort them by date
     17     FILES=$(find $1 -type f -printf '%T+\t%p\n' | sort -nr | awk '{ print $2 }')
     18 
     19     # set counters
     20     INDEX=0
     21 
     22     # linking loop
     23     for f in $FILES; do
     24 
     25         if [ $(echo "$f" | grep -E ".mp3$|.mp4$|.opus$|.m4a$") ]; then
     26 
     27             # increment index
     28             INDEX=$(echo "$INDEX + 1" | bc)
     29 
     30             # add leading zero for 1-9
     31             if [ $INDEX -lt 10 ]; then
     32                 INDEX=0$INDEX
     33             fi
     34 
     35             # construct new filename
     36             PERFORMER=$(mediainfo "$f" --Inform="General;%Performer%" | sed -e 's/|//g' -e 's/\"//g')
     37             TITLE=$(mediainfo "$f" --Inform="General;%Track%" | sed -e 's/\///g' -e 's/|//g' -e 's/\"//g')
     38             NFN=$(echo "$PERFORMER - $TITLE" | cut -b -120)
     39             NF="${2}/${INDEX}_$NFN"
     40             ln -s "$f" "$NF"
     41 
     42         fi
     43     done
     44 }
     45 
     46 function delete_old {
     47     # create list of days that are not deleted...
     48     for dat in $(seq 0 1 $2); do
     49         NOT_DEL="$NOT_DEL\|$(date +%Y-%m-%d -d -${dat}\ days)"
     50     done
     51     DELETE=$(find "$1" -type f -printf '%T+\t%p\n' | sort -nr | grep -v "$NOT_DEL" | awk '{ print $2 }' | grep -v "/.LOG/")
     52     # delete old files
     53     for f in $DELETE; do
     54         echo "[INFO] Deleting $f"
     55         rm "$f"
     56     done
     57 }
     58 
     59 if [[ $(check_connection wlan0) == "UP" ]];
     60 then
     61 
     62     # Download Podcasts
     63     cpulimit --limit 25 --foreground -- podget
     64 
     65     # Delete old Episodes (> 7 days)
     66     delete_old "$HOME/Podcasts/.data" 7
     67 
     68     # Create collection folder
     69     rm $HOME/Podcasts/* # does not include .data
     70 
     71     # Link collection
     72     sort_and_link "$HOME/Podcasts/.data" "$HOME/Podcasts"
     73 
     74 fi