Python script to fix messed up “Date Added” in XBMC / Kodi

Filed under Programming, Uncategorized
Tagged as , , ,

I recently switched from MediaPortal to XBMC on my living room HTPC. I was using MediaPortal for a long time. But unfortunately it got more and more unstable from release to release. So I gave XBMC a try and so far I am pretty satisfied with it. I really like how fast it is.

When I add movies to the library, XBMC sets “Date Added” to the file creation date. That is exactly how I like it. I am using 3rd party tools to add movies to the library which are not automatically recognized by XBMC itself (e.g. XBNE). I just added a lot of movies with XBNE. Worked great. Unfortunately XBNE added them with the current date in “Date Added”.

So I wrote a simple python script which goes through all movies in the library’s database and changes “Date Added” to the file’s last modification date.

Notes:

  • This is not a XBMC AddOn. It’s just a simple python script that manipulates the sqlite video database directly. So handle with care, and back up your video database.
  • Also exit XBMC before you run this script.
  • This script was tested with XBMC 13.0 up to Kodi 14.2
  • Change the location of your video database in the script accordingly. If you don’t know where it is, check this XBMC Wiki Entry about Userdata location.
  • If the script does not find file names with special characters, you might change the encoding from ‘cp1252’ to whatever you have. cp1252 is the standard encoding in windows for Western Europe.
  • This only changes movie files. Not TV series.

Download:

Download from Github: https://github.com/nharrer/kodi-update-movie-dateadded

Update 2015-07-17:

Update 2015-12-06:

  • Updated script to work with a database change in Kode database Version 93

11 Comments

  1. PeteG says:

    Great script. Connects to my video DB nicely but unfortunately my file paths are on a Samba share (on my NAS) which the os.path lib doesn't like smb:////. ... You get the picture. I'll have to do some digging as to how Python handles this and maybe do a little pattern matching to replace the path with something Python can handle.

    Any hints? Java and Perl are my speciality ๐Ÿ™‚

    Can't wait to get my library in order.

    • norbert says:

      Hi Pete.

      I just made a small change to the script on Github. Please download it again. I basically only cut away the "smb:"-Part. The rest might work. I can't test this however, because my database does not contain any movies on shares at the moment.

      Please let me know if this works for you or if you still have problems.

      • PeterG says:

        Sorry Norbert

        No go...

        I've been messing with it for a couple of hours now trying different things... Last 2 lines of output are below which were formally prefixed with smb: (that you've now stripped out).

        File //MEDIASERVER/Movies/Taken 3 (2014)/Taken 3.avi does not exist!
        Processed 1282 Rows.

        I've done some reading on the issue and it wouldn't seem to be the spaces. I've even tested it on a path/file with no spaces and it gives the same error. os.stat or exists couldn't see it either.

        Are you using windows? It might be an OS or FS incompatibility.
        Although the kodi media player works a treat so they are definitely accessible.

        Thanks for the quick reply!

        I'll keep on it over the next few days and let you know if I have a win.

        Cheers!

        • norbert says:

          Oh, I see. Yes I am on Windows. What system are you using?

          On a Linux style machine, you could just mount the remote path to any local path and then replace the smb-path to your local path in the script. Kind of quick and dirty hack at least.

          • PeteG says:

            I'm running a synology NAS with a SMB server. (With a RO guest user) I've been trying to mount it as you suggested but there must be something weird going on with openelec as it doesn't seem to get the right permissions. My WD live mounts it fine. I think I'll keep trying with the mount as that's probably the best bet.

            It might be a hack but, once it's done, that's it till the next library clean.

          • PeterG says:

            Got it!

            Embarrassingly, I had the wrong path in my mount command so it worked fine once I got my head screwed on.

            However, the permanent mount in the Kodi (GUI) file source is still a mystery to me. Anyway, I manually mounted my NAS and your script ripped through in 30secs and my recent movies are now showing up correctly!!

            Your script is probably the only thing that does this as I read a lot about the advancedsettings.xml 'dateadded' entries that weren't worth my time even reading.

            Rebuilt the library several times over the past couple of days and with 1200+ movies, it takes a while. *sigh*

            Anyway all good and have a great NY!
            Thanks for your help and time.

          • norbert says:

            Excellent. Great work.

            Yes, the setting in advancedsettings.xml only works for newly added Movies. If the dates in the database are already screwed up, then there does not seem to be a native way in Kodi to fix this.

            And like you, I can't stand it if the movie order is botched. And having to rebuild a carefully maintained movie database is a nightmare.

            BTW. From time to time I do "Export Video Library" in Kodi (it adds a .nfo file with all the database data for each movie to the movies directory). That way, if the database is wrecked, it's pretty easy to get it all back. It also helps when moving many movies to a different directory (just a clean and rescan will restore the metadata).

            I also wish you a great new year 2016.

        • norbert says:

          I googled a bit about python and samba. And it seems to be a mess. I also guess it would be easier to solve your mount problem then to integrate smb-support into the script. Have you seen this: http://wiki.openelec.tv/index.php/Mounting_network_shares

          I might look into smb-support at some time in the future. Probably by using the pysmb module. But for now I didn't find an easy and quick way to implement smb-support.

          • PeterG says:

            Ha! that's exactly the link I just found as well. Seems to be an issue with type cifs that makes you configure it manually. Might do that later and the inevitable library clean, rebuild and name fixes... maybe later it's working right now ๐Ÿ™‚

  2. PeterG says:

    Hi Norton,

    Simple change to the original query to do TV show episodes. Note, I am running v90 in my video DB so it may be a different view for later versions as is the case for movies.


    viewname = "episodeview"

    with closing(conn.cursor()) as cursor:
    cursor.execute('SELECT idEpisode, idFile, strFileName, strPath, dateAdded FROM {0} ORDER BY idFile'.format(viewname))

  3. PeterG says:

    Sorry, I meant Norbert...

Post a Reply to norbert

Your email is never published nor shared. Required fields are marked *

*
*