Moving videos watched with kodi to another directory

I have a kodi mediacenter at home that I use heavily to watch videos. Many of the videos will be recorded from a DVB-C adapter, others will be downloaded from youtube. To not lose my place I have build a script to move watched content into a hidden directory, so that kodi will no longer show it (and I know to delete the files — otherwise kodis “Hide watched” would be enough!).

First I had to configure kodi to use a central database. For that, edit the file “.kodi/userdata/advancedsettings.xml” and add this block:

1
2
3
4
5
6
7
<videodatabase>
    <type>mysql</type>
    <host>192.168.3.10</host>
    <port>3306</port>
    <user>xbmc</user>
    <pass>xbmc</pass>
</videodatabase>

Or any other IP-Adress. I created the user on the database and allowed it to create databases.

This is my script “move-kodi-seen-videos.sh”:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/bin/bash -eu

MEDIAPATH=/media/
SHAREPATH=smb://FILER/media/
DATABASEHOST=192.168.3.10

cd $MEDIAPATH

# Create the folder for .s(een) content
mkdir -p .s

export IFS=$'\t'

# The name of the database (MyVideos99) is incremented with some kodi-updates
mysql -h $DATABASEHOST -N -u xbmc --password=xbmc -B MyVideos99 -e 'SELECT CONCAT(strPath, "."), strFilename FROM path JOIN files USING (idPath) WHERE strPath LIKE "'$SHAREPATH'%" and playCount > 0;' | sed -e "s,$SHAREPATH,," | while read line; do
        set $line

        # If there is only one field in the output this is a directory
        if [[ $# -lt 2 && -d $1 ]]; then
                mv -v ${1%/.} .s
        # else its a file
        elif [[ $# -eq 2 ]]; then
                # split off the extension
                filename="${2%.*}"

                # Move all files (including subtitle-files)
                shopt -s nullglob
                for move_file in "$1/$filename".*; do
                        shopt -u nullglob
                        mkdir -pv .s/$1
                        touch -r $1 /tmp/datemarker
                        mv -v $move_file .s/$1
                        # Keep the "modified-date" on directories
                        touch -r /tmp/datemarker $1 || true
                        shopt -s nullglob
                done
                shopt -u nullglob
        fi
done

find -name \*.nfo -delete
find -name \*.txt -delete
# Remove all empty directories
find -type d -empty -exec rmdir -v '{}' +