Linux My hackey workaround to reset an entire folder as unwatched
#1
I just spent like four hours trying to get this seemingly simple task to work and after much banging of the head, it works. I figure why not document it here for anybody who might run across the same problems I did and how I got around them. If for nothing else, it's a handy place for me to find it again when I invariably forget and need it.

I've used linux a long time but I would never consider myself an expert, so some of this is probably "hackey". That's fine with me. Maybe there's a better way, but this worked.

Background on the need: I wanted a way to reset the watched status of a specific TV show without needing to do it one by one on every episode (some shows have lots of episodes!). I also only use kodi in "file mode", not "library mode". Right or wrong, maybe this is easier with a library or mysql setup, but I like it this way. It seems kodi used to be able to do this by marking an entire folder as watched, but that ability is gone in Krypton, so basically this is just a goofy workaround.

I also wanted it to be editable from the interface itself so that I could fairly quickly add a new "reset" function for a new TV series I may add in the future. Also, it would have to be somewhat intuitive so my wife can reset the status if she chooses (as opposed to me SSHing in to run a script).

I found this wonderful thread in my journey that told me everything I needed to know:
https://forum.kodi.tv/showthread.php?tid=346358 

But the problem is, being a kodi novice, I really didn't know how to do any of the things it mentioned. That's what I'm trying to document here. Even simple things like when to use quotes and whatnot should all be captured here with exact syntax to make it work.

Things to know:
0. This is clunky, I know.
1. You'll need an add-on to "run" the command. "XBMC Commands" works perfectly.
2. You'll need a bash script to do the database "reset".
3. You'll need a python script to call the bash script. The reason for this is we need to pass it a parameter and you can't do that with system.exec and a bash script, so we need to call the python script with the parameter, which will then call the bash script with the parameter.
4. This is probably vulnerable to SQL injection because it happily passes the parameter onto the bash script. If your kodi is open to the world, this could be a very bad idea unless you sanitize the input first.
5. The library reset is admittedly a lazy hack. All it does is blast the records out of the path table where it finds a match to your parameter, it does NOT touch the file records. But because it'll get a new path record when you start watching the series again, the primary key will be new and all those file records will be "orphaned" and new ones created. This could be improved by joining path and files in the bash script for the delete, but I'm lazy.

On to the guts:

STEP 1: 
Install the XBMC Commands add-on


STEP 2:
Create the bash script:

nano ~/.kodi/reset-library.sh

In the file, paste the following and CTRL-X,Y,Enter to save:
sqlite3 ~/.kodi/userdata/Database/MyVideos107.db "delete from path where strPath like \"%$1%\";"

I don't honestly remember if I needed to make it executable or not, so maybe for good measure:
chmod +x reset-library.sh


STEP 3:
Create the python script:
nano ~/.kodi/reset-library.py

In the file, paste the following and CTRL-X,Y,Enter to save:

#!/usr/bin/python
import os
import sys
os.system("~/.kodi/reset-library.sh '" + sys.argv[1]+"'")


STEP 4:
Now we just have to tell the scripts which TV show you want to clear the status for. I'm going to use the made-up "My Fab Show", you should edit this as necessary. NOTE - it will reset the status for all shows that have "My Fab Show" in the file PATH.

Back in the kodi interface, go into the XBMC Commands add-on:

1. Add command
2. Choose "Enter command"
3. Give it a title like "Reset My Fab Show"
4. Now for the command, this is where I had the most trouble of all, this needs to be exact AND what I found is that you can NOT use ~/.kodi. You MUST do /storage/.kodi! This is the exact syntax:

runscript(/storage/.kodi/reset-library.py, "My Fab Show")

Hit OK to save it and test it out, it should work!
Reply
#2
Why don't you use Mark as Unwatched from the Context Menu?
My Signature
Links to : Official:Forum rules (wiki) | Official:Forum rules/Banned add-ons (wiki) | Debug Log (wiki)
Links to : HOW-TO:Create Music Library (wiki) | HOW-TO:Create_Video_Library (wiki)  ||  Artwork (wiki) | Basic controls (wiki) | Import-export library (wiki) | Movie sets (wiki) | Movie universe (wiki) | NFO files (wiki) | Quick start guide (wiki)
Reply
#3
I'm not able to edit my post but I wanted to add that I indeed need to do the better join. In some situations, blasting the path wasn't enough (though sometimes it weirdly was). This should fix the issue of the watched status "coming back" unintentionally. Replace what I have in reset-library.sh above and use this instead:

sqlite3 ~/.kodi/userdata/Database/MyVideos107.db "DELETE FROM files WHERE idPath IN (SELECT idPath FROM path WHERE strPath LIKE \"%$1%\");"
Reply
#4
(2020-05-09, 05:06)MorkZaDork Wrote: I'm not able to edit my post

That's correct. For newcomers to this forum, certain anti-spam measures are in effect. Those will vanish over time and with a sufficient post count.

(2020-05-09, 05:06)MorkZaDork Wrote: sqlite3 ~/.kodi/userdata/Database/MyVideos107.db "DELETE FROM files WHERE idPath IN (SELECT idPath FROM path WHERE strPath LIKE \"%$1%\");"

Hmm, so now you're deleting whole file entries?!? It sure is one way to get rid of watched statuses... :-|

Watched statuses are stored in the playCount field and the accompanying lastPlayed field.
Setting those two to NULL will wipe the watched status.

And there is still no reply to the question: why not do all this watched status stuff from Kodi's GUI ?
Reply

Logout Mark Read Team Forum Stats Members Help
My hackey workaround to reset an entire folder as unwatched0