Kodi Community Forum
How to search via API? - Printable Version

+- Kodi Community Forum (https://forum.kodi.tv)
+-- Forum: Support (https://forum.kodi.tv/forumdisplay.php?fid=33)
+--- Forum: General Support (https://forum.kodi.tv/forumdisplay.php?fid=111)
+---- Forum: OS independent / Other (https://forum.kodi.tv/forumdisplay.php?fid=228)
+---- Thread: How to search via API? (/showthread.php?tid=353455)



How to search via API? - danielquinn - 2020-04-13

I'm working on extension for Mycroft (voice-activated AI) and I'd like for it to do something that sounds simple:
  • Search through my (remote) Kodi db for <keyword>
  • Return any data that db has for the results (ideally the URL for the file, whether it's been watched or not, etc.)
Ideally, I'd like to do something like this for example:
  • Search for "Burn Notice"
  • Have the API return the list of episodes WHERE watched = 'f' ORDER BY episode_order ...or some such thing
The thing is, all I've been able to find is this machine-friendly and human-hostile machine generated documentation and this rather old and poorly documented Python module.  As best I can tell, there's a way to get information about a video based on the library id, but I can't figure out how I might search the whole library for a best match for an arbitrary string -- something I figure must be available since the standard interface has a search button rather prominently placed.  If search is indeed not possible, then a simple export/import of specific metadata could work, though that's not exactly ideal.

If Kodi were running on the same machine, I'd just open up Sqlite directly, but since it's running on an Android box in another room, using an API seems like the responsible choice.  I just can't for the life of me figure out how to do this and why it seems so very difficult.

Any pointers (or Python code samples!) would be really appreciated.  The result of this project will be Freely licensed, so you can even play with the results when I'm ready to show & tell :-)


RE: How to search via API? - danielquinn - 2020-04-13

Well I appear to have figured it out by way of discovering this StackOverflow question that pointed me to a rough idea of what the syntax looked like, along with a whole lot of trial & error while looking at the machine-generated documentation.  If someone comes looking for answers one day and finds this, I hope it helps.

The answer for me was in two separate calls: one to search for the show based on my query, and another to get a list of episodes for that show, filtering for "playcount":

python:

from kodijson import Kodi
kodi = Kodi("http://<the android box's IP>:8080/jsonrpc")

# This returns a complex Python dict, but all I needed her was the `tvshowid`
tv_show = kodi.VideoLibrary.GetTVShows(
    filter={"field": "title", "operator": "contains", "value": "adventure"}
)
tv_show_id = tv_show["result"]["tvshows"][0]["tvshowid"]

# Now that I have the tv show id, I can get a list of episodes, filtering for `playcount < 1`
# The filtration syntax is very powerful, but not at all obvious from the machine-generated docs.
# I figured it out by looking at code samples for an old version of the API and hoped things
# hadn't changed too much:
# https://www.programcreek.com/python/example/79076/xbmc.executeJSONRPC
kodi.VideoLibrary.GetEpisodes(
    tvshowid=tv_show_id,
    properties=[
        "season",
        "episode",
        "playcount",
        "file"
    ],
    filter={
        "field": "playcount",
        "operator": "lessthan",
        "value": "1"
    }
)