python json filter optimization?
#1
Hi gurus,

Is there a way to make a filter work a bit smarter than the below code snippet?
Perhaps there is a way of using a list instead of single values?

Thanks in advance!

Code:
elif filtergenre == "hard":
    # HARD:
    f = '"filter": { "or":['
    f = f + '{"field": "genre", "operator": "is", "value": "action"}, '
    f = f + '{"field": "genre", "operator": "is", "value": "horror"}, '
    f = f + '{"field": "genre", "operator": "is", "value": "crime"}, '
    f = f + '{"field": "genre", "operator": "contains", "value": "sci"}, '
    f = f + '{"field": "genre", "operator": "is", "value": "thriller"}'
    f = f + ']}, "filter": {"and":['
    f = f + '{"field": "genre", "operator": "isnot", "value": "family"}, '
    f = f + '{"field": "genre", "operator": "isnot", "value": "romance"}, '
    f = f + '{"field": "genre", "operator": "isnot", "value": "fantasy"}, '
    f = f + '{"field": "genre", "operator": "isnot", "value": "comedy"}, '
    f = f + '{"field": "genre", "operator": "isnot", "value": "adventure"}, '
    f = f + '{"field": "genre", "operator": "isnot", "value": "documentary"}, '
    f = f + '{"field": "genre", "operator": "isnot", "value": "western"}, '
    f = f + '{"field": "genre", "operator": "isnot", "value": "music"}, '
    f = f + '{"field": "year", "operator": "greaterthan", "value": "' + filteryearfrom + '"}, '
    f = f + '{"field": "year", "operator": "lessthan", "value": "' + filteryearto + '"}'
    f = f + ']}, '
<
Reply
#2
What is this exactly? I you are using some third-party query library, then you should provide more info. "There are no psychics" © "The Mentalist".
Reply
#3
My bad! It is the filter bit of the json query. Goes into libmovies. I just did it using string addition for code clarity.

Most of the inspiration originates from the wiki:
http://kodi.wiki/view/JSON-RPC_API/Examples
Reply
#4
I see now. In fact, Python has build-in json module for serialization/deserialization of json objects, so you don't have to deal with raw strings.
This should get you started: https://pymotw.com/2/json/
Reply
#5
Hi Roman, and all.

Would you care to dumb it down a notch?

What is the way to avoid including all those identical filters and instead have something beautiful?

From this:
Code:
filter on genre this, filter on genre that, filter on genre foo, filter on genre bar

To this:
Code:
filter on genres this, that, foo, bar

Thanks
Reply
#6
I'm not really looking for a masters in json. Just a way of making Kodi work..
Reply
#7
Now I understand what you mean. But this has nothing to do with JSON in general, this is how Kodi JSON-RPC API is implemented. If it allows grouping queries in some way, then you can implement what you want, if not, you are out of luck.
Reply
#8
Good. Now to sit back and wait for someone who knows if it does :-) after all this isnt a json forum.
Reply
#9
For what its worth. The info needed to determine if a filter can be applied smarter is probably here: http://kodi.wiki/view/JSON-RPC_API/ and I dont think it is possible?
Reply
#10
Looking through the introspect from v6 then I'd say no its not possible. However, you really really should use a proper json serializer, makes it looks so much easier to follow. So please provide the full json you send to Kodi and if you have a suggestion on how you'd prefer it to look then suggest that too, in a full example. Your code is really hard to follow and afaict its not even a correct query.

The following is how I parse it, which is a bad json (double filter property)

Code:
var params = {
  'filter': {
    'or': [
      { 'field': 'genre', 'operator': 'is', 'value': 'action' },
      { 'field': 'genre', 'operator': 'is', 'value': 'horror' },
      { 'field': 'genre', 'operator': 'is', 'contains': 'sci' },
      { 'field': 'genre', 'operator': 'is', 'value': 'thriller' },
    ] },
  'filter': {
    'and': [
      { 'field': 'genre', 'operator': 'isnot', 'value': 'family' },
      { 'field': 'genre', 'operator': 'isnot', 'value': 'romance' },
      { 'field': 'genre', 'operator': 'isnot', 'value': 'fantasy' },
      { 'field': 'genre', 'operator': 'isnot', 'value': 'comedy' },
      { 'field': 'genre', 'operator': 'isnot', 'value': 'adventure' },
      { 'field': 'genre', 'operator': 'isnot', 'value': 'documentary' },
      { 'field': 'genre', 'operator': 'isnot', 'value': 'western' },
      { 'field': 'genre', 'operator': 'isnot', 'value': 'music' },
      { 'field': 'year', 'operator': 'greaterthan', 'value': filteryearfrom },
      { 'field': 'year', 'operator': 'lessthan', 'value': filteryearto },
    ]
  }
}
If you have problems please read this before posting

Always read the XBMC online-manual, FAQ and search the forum before posting.
Do not e-mail XBMC-Team members directly asking for support. Read/follow the forum rules.
For troubleshooting and bug reporting please make sure you read this first.

Image

"Well Im gonna download the code and look at it a bit but I'm certainly not a really good C/C++ programer but I'd help as much as I can, I mostly write in C#."
Reply
#11
Thanks Topfs2. You are likely correct in assuming it is wrong. I think it works, but it probably ignores one of the filters? the extra filter bit should probably go. Can it be written like you do in python?

It would be nice if the filter could handle a list with values. No biggie here but for the music there are really a lot of similar genres that needs to be joined.

Edit: bloody smart phone. .
Reply
#12
Here is the full JSONRPC (pardon for not formatting nicely):
Code:
q = xbmc.executeJSONRPC('{"jsonrpc": "2.0", "method": "VideoLibrary.GetMovies", "params": {  "filter": { "or":[{"field": "genre", "operator": "is", "value": "action"}, {"field": "genre", "operator": "is", "value": "horror"}, {"field": "genre", "operator": "is", "value": "crime"}, {"field": "genre", "operator": "contains", "value": "sci"}, {"field": "genre", "operator": "is", "value": "thriller"}]}, "filter": {"and":[{"field": "genre", "operator": "isnot", "value": "family"}, {"field": "genre", "operator": "isnot", "value": "romance"}, {"field": "genre", "operator": "isnot", "value": "fantasy"}, {"field": "genre", "operator": "isnot", "value": "comedy"}, {"field": "genre", "operator": "isnot", "value": "adventure"}, {"field": "genre", "operator": "isnot", "value": "documentary"}, {"field": "genre", "operator": "isnot", "value": "music"}, {"field": "genre", "operator": "isnot", "value": "western"}, {"field": "year", "operator": "greaterthan", "value": "1989"}, {"field": "year", "operator": "lessthan", "value": "2000"}]},  "properties": ["title", "year", "thumbnail", "playcount"], "sort": { "order": "ascending", "method": "label", "ignorearticle": true }}, "id": "libMovies"}')

Again, this is not half bad compared to having to filter audio genres..
So I was hoping for something along the lines of having a list for the values for each filter. (pseudo and probably wrong in other ways):
Code:
"filter":
    {
    "or": [
        {"field": "genre", "operator": "is", "value": ["action", "horror", "crime", "thriller"]},
        {"field": "genre", "operator": "contains", "value": ["sci"]} ],
     "and": [
         {"field": "genre", "operator": "isnot", "value": ["family", "romance", "fantasy", "comedy","adventure","documentary","western","music"]} ],
        {"field": "year", "operator": "greaterthan", "value": "1989"},
        {"field": "year", "operator": "lessthan", "value": "2000"}    ]
    }

Topfs2, you were correct about the filter being ignored. Thanks again.
Reply

Logout Mark Read Team Forum Stats Members Help
python json filter optimization?0