[seriesfiler] Series Auto-filer

OP
S

Sam Widges

Active Member
Great script, works well. I'm going to look at extending it to allow for renamed folders using the details in the series file. If I come up with anything useful I'll post it up of course.
That sounds good, let me know how you get on.

If people have problems, please post them on this thread and I'll see what we can do to fix them.
 

MattC

Member
This looks great; just what I was after. What I'll probably end up doing is adding a section at the end which finds all the empty folders and deletes them (moving the .series file up to root).. which is another annoyance of the way it displays recordings.

Are you guys pretty much finished developing the script, i.e. is now I good time to install it?
 

MattC

Member
Great instructions.. just installed it. I'd started to encounter the file format problem so instead of using dos2unix, I simply copied an existing script (dedup), renamed it, and pasted in your script. Once my wife has finished watching cr*p I'll jump on and start filing things into sub-folders & check it all works.

Just out of interest, what do the .series files contain? Are they where you're at through each recording? Or do they contain series link info?
 
OP
S

Sam Widges

Active Member
The file contains the name of the series itself and a couple of bytes that count what has and hasn't been watched. This is how the "New programmes" icon for the folder works and that's why I copy it to the new folder since you should then still be notified about a new programme. No-one has reported any problems with that feature yet, so I assume that bit of the filer script is still valid. All the other bytes in the files that I have examined have been zero.
 

Tim.C

Member
Thankyou for a fascinating thread. It gave me a brilliant example to use the other day when one of my students asked "But why would you bother with scripts? What would you use them for?"

OK it was a Windows 7 course, but the example worked and a few are now starting to look at Powershell ;-)
 
OP
S

Sam Widges

Active Member
Wow, I've finally been mentioned in academia!

You never know, maybe one day they'll graduate to a proper operating system :)
 

MattC

Member
Here's a modified version of the script.. it has an option (enabled/disabled at the top) that renames the file / program title as it gets moved to the target folder. It's pretty essential for kids TV shows where the Humax does a poor job at showing you which ep is which. Have a play.. it's not quite 100% there, but it's working on the sample files I've tried so far. For some reason it's complaining about an unterminated string literal on the last line.. apart from that it works.

The renaming code is mainly ripped/recycled from the dedup script :)

Code:
#!/bin/sh

#Set these here otherwise path problems break the script when run from cron
FIND=/mod/bin/busybox/find
GREP=/mod/bin/busybox/grep
SED=/mod/bin/busybox/sed
LSOF=/mod/bin/lsof
BASENAME=/mod/bin/busybox/basename
RMDIR=/mod/bin/busybox/rmdir
MV=/mod/bin/busybox/mv

TR=/mod/bin/busybox/tr
CUT=/mod/bin/busybox/cut
HMT=/mod/bin/hmt
RENAME=1 #Enable or disable file renaming based on EPG data

#Needed to stop the list using spaces as delimiters
IFS=$'\n'
#Despite the pipe following the next find, the IFS still needs to be set

#Find all the top level directories in "My Video" that contain
#a .series file and iterate through them
$FIND /media/My\ Video/ -maxdepth 2 -mindepth 2 -name '.series' | $SED 's/\/.series$//g' | while read dir; do

        #Strip out the series name from the directory name and look for
        #other directories with the same name
        series=`$BASENAME "$dir"`
        echo "Checking $dir"
        location=`$FIND /media/My\ Video/ -mindepth 2 -type d -name "$series"`

        #$location is only set if another directory has been found with the same name
        if [ -n "$location" ]; then
                echo "Filing for $location"
                #Use the hmt files to identify the episode base filename and strip off the suffix using sed
                for episodefile in `$FIND "$dir/" -name '*.hmt' | $SED 's/.hmt$//'`
                do
                        #Find and move all the files for this episode, if they are not in use
                        episode=`$BASENAME "$episodefile"`
                        inuse=`$LSOF | $GREP "$episode".ts`
                        if [ -n "$inuse" ]; then
                                echo "$episodefile is in use - skipping"
                        else
                                #Rename the episodes so that we have a name from the first X characters of the EPG
                                if [ "$RENAME" -eq 1 ]; then
                                    echo "Renaming the episode to give it a nice name"
                                    epg=`$HMT "$dir/$episode" | $GREP '^EPG:' | $CUT -d: -f2`
                                    epg="`echo ${epg:0:40}`" # TRIM THE FILENAME DOWN TO 40 CHARACTERS
                                    $HMT +settitle="$epg" "$dir/$episode.hmt"
                                    echo "    HMT for $episode.hmt renamed title to $epg"
                                fi

                                #Now go and move the file to the new location
                                echo "Moving $episode to $location"
                                $MV "$dir/$episode".* "$location"
                        fi
                done
                if [ "$(ls $dir)" ]; then
                        #There is probably another episode being recorded
                        #we don't want to interfere, so do nothing
                        echo "Series directory not empty"
                else
                        #.series is a hidden file, and the only hidden file that should exist
                        #if there are no other files in the directory, move .series as well
                        #and then remove the series directory at the top level - the filing is now done
                        #.series has to be moved last, in case the box powers down mid script
                        #in which case, re-running it should still cleanly finish the process
                        $MV $dir/.series $location
                        echo "Removing empty series directory"
                        $RMDIR "$dir"
                fi

        fi
done

#And show all the series that have been processed in the last day
echo "Recently filed:"
$FIND /media/My\ Video/ -mindepth 3 -mtime -1 -name '.series' | $SED 's/\/.series$//g'
 

MattC

Member
Edited the last post since I don't think you actually have to rename the file.. just alter the title via the hmt command.
 
OP
S

Sam Widges

Active Member
That looks like a really useful addition to the autofiler script, thanks.

As ever, if there are any comments on or issues with the script, please let us know on this thread.
 

MattC

Member
Thanks. I've just updated it again, with hopefully the final edit. Just removed some of the comments & can confirm it's working on my system when it's executed via the cron job. Not sure on the perfect ep title length.. it's now at 30 characters, but it might be able to handle more.
 

MattC

Member
Looks like it can handle 40 characters.

I've also modified the dedup scrip to be a script called 'nicename' which will simply set the title of all the files in a folder to a nice name.. it won't rename the physical file or do any dedupe stuff. Useful to get your kids TV in order and then use the auto-filer after that. I guess we could do with a cron-compatible script that does this in the background on any new files which are within a sub-folder.

Code:
#!/bin/sh

doit=0
if [ "$1" = "-yes" ]; then
    doit=1
    echo "Renaming titles in the current folder"
    echo "-------------------------------------"
else
    echo "Showing what would be done."
    echo "Run again with -yes to perform changes."
    echo "---------------------------------------"
fi

for hmt in *.hmt; do
    fbase=`basename "$hmt" .hmt`

    epg=`hmt "$hmt" | grep '^EPG:' | cut -d: -f2`
    #title="`echo $epg | tr '[/ &]' '[._.]'`"
    title="`echo ${epg:0:40}`" # TRIM THE FILENAME DOWN TO 40 CHARACTERS

    echo "$fbase - new title = $title"

    [ $doit -eq 0 ] && continue

    hmt +settitle="$title" "$fbase.hmt"
done
 
OP
S

Sam Widges

Active Member
MattC, could you to edit the changes for the 40 character rename back into post #28 as I have linked to that from the first post in the thread. I assume it's just a case of changing both instances of 30 to 40 in one line, but I can't edit your posts and I don't want to do another howto post just yet.

Cheers,

Sam
 

MattC

Member
MattC, could you to edit the changes for the 40 character rename back into post #28 as I have linked to that from the first post in the thread. I assume it's just a case of changing both instances of 30 to 40 in one line, but I can't edit your posts and I don't want to do another howto post just yet
Sure. All done.
 

MattC

Member
Between taking my 3yr old to the museum and burping my 3wk old daughter, I'd altered the script around so that the renaming happens in spite of the file move.. that means it'll rename files (for series) as soon as they're recorded. You won't get instances of the first episode not getting renamed nicely, before you move it into your preferred sub-folder. Once the file has been renamed, I alter the permissions on the file slightly so it doesn't attempt to rename it indefinitely.

Give it a try and let me know if you encounter any problems.

Code:
#!/bin/sh

#Set these here otherwise path problems break the script when run from cron
FIND=/mod/bin/busybox/find
GREP=/mod/bin/busybox/grep
SED=/mod/bin/busybox/sed
LSOF=/mod/bin/lsof
BASENAME=/mod/bin/busybox/basename
RMDIR=/mod/bin/busybox/rmdir
MV=/mod/bin/busybox/mv

TR=/mod/bin/busybox/tr
CUT=/mod/bin/busybox/cut
HMT=/mod/bin/hmt
CHMOD=/mod/bin/busybox/chmod
RENAME=1 #Enable or disable file renaming based on EPG data

#Needed to stop the list using spaces as delimiters
IFS=$'\n'
#Despite the pipe following the next find, the IFS still needs to be set

#Find all the top level directories in "My Video" that contain
#a .series file and iterate through them
$FIND /media/My\ Video/ -maxdepth 2 -mindepth 2 -name '.series' | $SED 's/\/.series$//g' | while read dir; do

        #Rename the episodes so that we have a name from the first X characters of the EPG
        if [ "$RENAME" -eq 1 ]; then
            for episodefile in `$FIND "$dir/" -name '*.hmt' ! -perm -020 | $SED 's/.hmt$//'`
            do
                echo "Renaming the episode to give it a nice name"

                episode=`$BASENAME "$episodefile"`

                inuse=`$LSOF | $GREP "$episode".ts`
                if [ -n "$inuse" ]; then
                    echo "$episodefile is in use - skipping"
                else
                    epg=`$HMT "$dir/$episode" | $GREP '^EPG:' | $CUT -d: -f2`
                    epg="`echo ${epg:0:40}`" # TRIM THE FILENAME DOWN TO 40 CHARACTERS
                    $HMT +settitle="$epg" "$dir/$episode.hmt"
                    echo "    HMT for $episode.hmt renamed title to $epg"
                    $CHMOD g+w "$dir/$episode.hmt"
                fi
            done
        fi

        #Strip out the series name from the directory name and look for
        #other directories with the same name
        series=`$BASENAME "$dir"`
        echo "Checking $dir"
        location=`$FIND /media/My\ Video/ -mindepth 2 -type d -name "$series"`

        #$location is only set if another directory has been found with the same name
        if [ -n "$location" ]; then
                echo "Filing for $location"
                #Use the hmt files to identify the episode base filename and strip off the suffix using sed
                for episodefile in `$FIND "$dir/" -name '*.hmt' | $SED 's/.hmt$//'`
                do
                        #Find and move all the files for this episode, if they are not in use
                        episode=`$BASENAME "$episodefile"`
                        inuse=`$LSOF | $GREP "$episode".ts`
                        if [ -n "$inuse" ]; then
                                echo "$episodefile is in use - skipping"
                        else
                            #Now go and move the file to the new location
                            echo "Moving $episode to $location"
                            $MV "$dir/$episode".* "$location"
                        fi
                done
                if [ "$(ls $dir)" ]; then
                        #There is probably another episode being recorded
                        #we don't want to interfere, so do nothing
                        echo "Series directory not empty"
                else
                        #.series is a hidden file, and the only hidden file that should exist
                        #if there are no other files in the directory, move .series as well
                        #and then remove the series directory at the top level - the filing is now done
                        #.series has to be moved last, in case the box powers down mid script
                        #in which case, re-running it should still cleanly finish the process
                        $MV $dir/.series $location
                        echo "Removing empty series directory"
                        $RMDIR "$dir"
                fi

        fi
done
 

MattC

Member
Once we've tested out the latest version of the script which does the title renaming up-front, how about we get this rolled into a package that everyone can install easily? Is it a relatively simple job?
 
OP
S

Sam Widges

Active Member
I think we should go for it. It shouldn't be too difficult either, although we'll need to pull in cron and add the crontab entry as well.
 
Top