Deleting XBMC shows from disk based on Watched status – part 2

(continue from  Part 1)

Playing with our data

So now we’ve got a nice list of our files with some usage data along with it, let’s abuse some linux commands to tweak it. For me, at least, I don’t want to just blindly delete anything I’ve already watched. There are some shows I do want to return to for nostalgia, but I’ve also found it helpful to have a few previous episodes reserved just in case I forget what the hell is going on (a symptom of watching too many shows?)

The horror of linux sort

My first thought was, “I’ll just pipe this through sort, it’ll be easy!”. Well yah, but I forgot how insane the sort syntax can be.

Here is what we need to do to be able to sort on the last field of data, with a date syntax of “YYYY-MM-DD HH:MM:SS”

# sort -t ‘|’ -k 4.1,4.4 -k 4.6,4.7 -k 4.9,4.10 -nr /tmp/watched.log
/video/Videos/TV/Some Public Domain Show/Season 01/Episode – 1×05.mkv|Some Public Domain Show – 1×05.mkv|1|2013-12-28 14:22:25
/video/Videos/TV/Some Public Domain Show/Season 01/Episode – 1×04.mkv|Some Public Domain Show – 1×04.mkv|1|2013-12-28 13:49:21
/video/Videos/TV/Some Public Domain Show/Season 01/Episode – 1×03.mkv|Some Public Domain Show – 1×03.mkv|1|2013-12-28 13:04:03
/video/Videos/TV/Some Public Domain Show/Season 01/Episode – 1×02.mkv|Some Public Domain Show – 1×02.mkv|1|2013-12-28 12:39:19

Basically, we’re telling sort that the field separator is “|”, and to sort based on field 4: characters 1-4 for year, chars 6-7 for month, and then chars 9-10 for day. Finally, we’re saying to sort numerically, and then reverse the order so that newest is displayed first.

Not convoluted at all, eh?

Reduce it with grep

Ok, we’re not performing rocket science here, but after the nastiness of sort it’s nice to return to the simplicity of grep. For my purposes, and since I’m not ready to fully automate this yet, I’m going to simply reduce my results a bit.

For example, I’d like to have a listing of everything that I watched in January. And I want to output the filenames to another tmp file that I can manually edit (to ensure I’m not eliminating something I’m not ready to part with yet) before I begin deleting it.

# sort -t ‘|’ -k 4.1,4.4 -k 4.6,4.7 -k 4.9,4.10 -nr /tmp/watched.log |grep 2014-01|egrep -v “Some|Favorites|I|want” |awk -F”|” ‘{print $1}’> > /tmp/january.txt

Let’s loop it

So now we’ve ensured that we aren’t nuking any of our favorites, but we cannot really TRUST the paths that our database is giving us to be shell-safe. There are likely a lot of parentheses, apostrophes, and even bangs (those are the worst!) in there. Luckily, quoting these looks like it’s going to be enough to get the job done.

# while read -r line ; do rm -f “$line”; done < /tmp/january.txt

Note: I tend to replace the rm -f portion with something safer the first few times I run it, typically an “ls -al”

Clean up the XBMC Library

Now that you’ve deleted some of your old data, you’re going to need to tell XBMC to rescan your drive and make note of the changes. Luckily, due to their awesome JSON API this is a breeze! If you have read my post on updating your library via cron, then this will be pretty familiar for you. You just need to send XBMC the “VideoLibrary.Clean” command:

# curl -H “Accept: application/json” -H “Content-type: application/json” -X POST -d ‘{“jsonrpc”: “2.0″, “method”: “VideoLibrary.Clean”, “id”: 1}’ http://IP-OF-XBMC:8080/jsonrpc

After thoughts

So, that worked out pretty well.. and it’s definitely better than manually looking for a bunch of files and trying to remember how far into the season you’ve gotten. But, it’s still a bit clunky. My immediate thoughts are that this would be better represented with a simple python or php backed webpage that shows you pretty thumbnails for the show in question and lets you multi-select before sending a batch into a delete queue.

How about you? What would you do to simplify this?

Leave a Reply

Your email address will not be published. Required fields are marked *