kodi

Add directory to Kodi using the command line


Is there possible to add a directory to Kodi using the command line? I've been looking for this with no luck so far.

What I'm looking for is to automate the process of adding a directory manually using command line. For some reason this doesn't appear to be a popular question out there; am I missing something?


Solution

  • Crawling the Kodi/XBMC forums and wiki show a few options... Here's what I've gathered...

    Edit the Database Directly (not recommended)

    Kodi stores this information in a sqlite database, however this location would be pretty tricky to manipulate yourself as it would require both knowledge of the path of each sqlite database file as well as the relationship of each column/table in each database file (assuming it's a strictly relational database file, which most are).

    For example:

    sqlite3 <path_to_kodi_preferences>/userdata/Database/MyVideos119.db
    
    sqlite> .tables
    actor                   movie_view              studio_link           
    actor_link              movielinktvshow         tag                   
    art                     musicvideo              tag_link              
    bookmark                musicvideo_view         tvshow                
    country                 path                    tvshow_view           
    country_link            rating                  tvshowcounts          
    director_link           season_view             tvshowlinkpath        
    episode                 seasons                 tvshowlinkpath_minview
    episode_view            sets                    uniqueid              
    files                   settings                version               
    genre                   stacktimes              writer_link           
    genre_link              streamdetails         
    movie                   studio             
    

    Edit sources.xml

    The official wiki mentions <path_to_kodi_preferences>/userdata/sources.xml for this but it still assumes you know how to manipulate an XML file programmatically and the community warns that this is potentially "invasive" and that the official addons/plugins aren't allowed to use this technique.

    I dove into this and the XML seems like the way to go, for example, to add Videos:

        <video>
            <default pathversion="1"></default>
            <source>
                <name>Movies</name>
                <path pathversion="1">/home/ubuntu/Movies/</path>
                <allowsharing>true</allowsharing>
            </source>
            <source>
                <name>Video Playlists</name>
                <path pathversion="1">special://videoplaylists/</path>
                <allowsharing>true</allowsharing>
            </source>
    +       <source>
    +           <name>MyCustomDirectory</name>
    +           <path pathversion="1">/home/ubuntu/MyCustomDirectory/</path>
    +           <allowsharing>true</allowsharing>
    +       </source>
        </video>
    

    ... however comments suggest Kodi needs to be restarted and that this location still needs to be crawled/refreshed. There may be some "watchdog" add-ons that can do this for you.

    Use the Add-On API

    Another technique is to use the official Python API, such as through UpdateLibrary(database, path) however examples usually involve Python to call the API directly. Here's an example from the PlexKodiConnect GitHub project:

    # Make sure Kodi knows we wiped the databases
    xbmc.executebuiltin('UpdateLibrary(video)')
    if utils.settings('enableMusic') == 'true':
        xbmc.executebuiltin('UpdateLibrary(music)')
    

    Since the simplest solution is often the best, I would recommend working on a way to automate the settings.xml file. Modifying XML files is well-documented in nearly all programming languages (to that point, you could technically brute-force and just inject an XML string at the given place without an xml parser 😈) and then handle the restart and refresh operations once the XML is confirmed as being updated properly.