Project

General

Profile

RE: Auto delete recorded files: solution ยป pvrclean.sh

PVR Cleanup script - Hiro Protagonist, 2015-08-13 03:56

 
1
#!/bin/bash
2

    
3
# Settings:
4
MyEmail="[email protected]" 
5
MyTVhFolder="/home/pi/.hts/tvheadend/dvr/log" 
6
MyRecordingsFolder="/var/spool/pvr"
7
FTYPE="mkv"
8

    
9
# Create temp files:
10
PVR_files="/tmp/PVR_files.txt" 
11
TEMP_file="/tmp/TEMP_file.txt" 
12
TVH_files="/tmp/TVH_files.txt" 
13
DIFF_files="/tmp/DIFF_files.txt" 
14
for file in $PVR_files $TEMP_file $TVH_files $DIFF_files
15
do
16
  if [ -f $file ]
17
  then
18
    rm $file
19
  fi
20
done
21

    
22
# List all recordings in recordings folder and write to PVR_files:
23
echo "" 
24
echo "All recordings in recordings directory:" 
25
find $MyRecordingsFolder -type f > $TEMP_file
26
sort $TEMP_file > $PVR_files
27
# Clear temp file for re-use
28
echo -n > $TEMP_file
29
wc -l $PVR_files
30
# List all recordings in tvheadend, sort and write to TVH_files:
31
echo "" 
32
echo "All recordings in TVheadend:" 
33
for file in $MyTVhFolder/*
34
do
35
    FNAME=$(cat $file | grep filename | awk -F'"' '{print $4}')
36
    if [ -n "$FNAME" ]
37
    then    
38
      # PROG-NAME.YYYY-MM-DD.HH-MM<-X>.xyz
39
      SUFFIX=$(echo $FNAME | cut -d '.' -f 3 | cut -d '-' -f 3)
40
      if [ -n "$SUFFIX" ]
41
      then
42
          # If we have a suffix of say, 3 - then we need to account for:
43
          # PROG-NAME.YYYY-MM-DD.HH-MM-3.xyz
44
          # PROG-NAME.YYYY-MM-DD.HH-MM-2.xyz
45
          # PROG-NAME.YYYY-MM-DD.HH-MM-1.xyz
46
          # PROG-NAME.YYYY-MM-DD.HH-MM.xyz
47
          PROG=$(echo $FNAME | cut -d '.' -f 1)
48
          DATE=$(echo $FNAME | cut -d '.' -f 2)
49
          STAMP=$(echo $FNAME | cut -d '.' -f 3)
50
          TIME=${STAMP:0:5}
51
          while [ $SUFFIX -gt 0 ]
52
          do
53
            echo $PROG.$DATE.$TIME-$SUFFIX.$FTYPE >> $TEMP_file
54
            let SUFFIX--
55
    	  done
56
    	  echo $PROG.$DATE.$TIME.$FTYPE >> $TEMP_file
57
      else
58
          echo $FNAME >> $TEMP_file
59
      fi
60
    fi
61
done
62
sort $TEMP_file > $TVH_files
63
wc -l $TVH_files
64
# Difference of PVR_files and TVH_files for subsequent usage:
65
echo "" 
66
comm -3 $PVR_files $TVH_files > $DIFF_files
67

    
68
# Actual deletion, if any:
69
if [ -s $DIFF_files ]; then
70
    echo "Yes, the following files will be deleted:" 
71
    wc -l $DIFF_files
72
    #cat $DIFF_files | mail -s "TVheadend: deleted recordings from $(date +"%d.%m.%Y")" $MyEmail
73
    while read line
74
    do 
75
      if [ -n "$line" ]
76
      then
77
        echo $line
78
        rm $line
79
        # Delete the directory if it's empty
80
        rmdir $(dirname $line) 2>/dev/null
81
      fi
82
    done < $DIFF_files
83
else
84
    echo "No, nothing will be deleted." 
85
fi
86

    
87
# Cleanup:
88
rm $PVR_files $TEMP_file $TVH_files $DIFF_files
89
echo "" 
    (1-1/1)