Solved Query the database for beginners
#16
well, well, well...

Too soon ofcourse Smile

Seems the window.property being set onload is not really set as passing it to the skin.string is not happening untill the next onload fires. Maybe there is a <onloadcomplete> tag or something, or maybe the new <onclick> for dynamic lists will be save me?
Reply
#17
The solution to getting the list of albums to update immediatly was to simply include the window.parameter directly in the content tag instead of passing it to a skin string first (I used that for testing stuff).

I have my working discography now, so at least I have that going for me, which is nice Smile
Reply
#18
And here I am again.. it took awhile to get the albums working and due to 100% dynamic lisis I migjt have broken some functionality so rhe simple ordeal of using kodi as a smart database is far from over... userfriendlymess.
Reply
#19
Nice work, i'm interested in this one as i'm currently writing some tutorials and "Querying the database" sounds like a good one.

Can you post your code to github? or pastebin if its simple?
Reply
#20
Hi Zag, thanks for the interest.

Here it is, please keep in mind this is my first Jason in py-thong hack:

Make the folder Kodi/addons/script.myscript/ and add these two files:
Note, to anyone else reading this, you should probably have a look at http://kodi.wiki/view/Add-on_structure#D..._structure for what an addon folder should contain should you decide to release it)

addon.xml
Code:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<addon id="script.myscript" name="MyScript" version="5.0.0" provider-name="Torben">
    <requires>
        <import addon="xbmc.python" version="2.20.0"/>
        <import addon="script.module.simplejson" version="3.3.0"/>
    </requires>
    <extension point="xbmc.python.script" library="mydefault.py">
        <provides>executable</provides>
    </extension>
    <extension point="xbmc.addon.metadata">
        <summary lang="en">Get Albumartist DBID from AlbumDBID</summary>
        <description lang="en">This script returns the ArtistDBID from a passed AlbumDBID</description>
        <language></language>
        <platform>all</platform>
        <license>GNU GENERAL PUBLIC LICENSE Version 2</license>
        <forum></forum>
        <website></website>
        <email></email>
        <source></source>
    </extension>
</addon>

mydefault.py
Code:
import xbmc, xbmcgui, xbmcaddon, sys
from operator import itemgetter
if sys.version_info >=  (2, 7):
    import json
else:
    import simplejson as json

# Define global variables
WINDOW = xbmcgui.Window( 10000 )

__addon__        = xbmcaddon.Addon()
__addonversion__ = __addon__.getAddonInfo('version')
__addonid__      = __addon__.getAddonInfo('id')
__addonname__    = __addon__.getAddonInfo('name')

# Get value passed from KODI
ALBUMDBID = sys.argv[1]

# Lookup Album details in the library to get at the artist id:
json_query = xbmc.executeJSONRPC('{ "jsonrpc": "2.0", "method": "AudioLibrary.GetAlbumDetails", "params": {"albumid": %d, "properties": ["artistid"]}, "id": 1 }' % int(ALBUMDBID))
json_query = unicode(json_query, 'utf-8', errors='ignore')
json_response = json.loads(json_query)

if json_response.has_key('result') and (json_response['result'] != None) and json_response['result'].has_key('albumdetails'):
    WINDOW.setProperty( 'dbLookParamFound' , str(json_response['result']['albumdetails']['artistid'][0]) )

Run the script and pass the Album DBID from Kodi. Here is an example that runs onclick in a panel with albums:
Code:
<onclick>RunScript(script.myscript, $INFO[listitem.dbid])</onclick>

Now you can access the artist DBID in the skin using window(Home).Property(dbLookParamFound), here is an example where it is used to fill a dynamic list with albums by artist:
Code:
<content sortby="year" sortorder="descending" target="music">musicdb://artists/$INFO[Window(Home).Property(dbLookParamFound)]/</content>

At least in the latest Jarvis Smile
Reply
#21
PHP Code:
__addon__        xbmcaddon.Addon()
__addonversion__ __addon__.getAddonInfo('version')
__addonid__      __addon__.getAddonInfo('id')
__addonname__    __addon__.getAddonInfo('name'
Everyone should STOP doing it like that! It's wrong. I know i've done it in the past as well but it has to end.

PHP Code:
addon        xbmcaddon.Addon()
addonversion addon.getAddonInfo('version')
addonid      addon.getAddonInfo('id')
addonname    addon.getAddonInfo('name'
Read/follow the forum rules.
For troubleshooting and bug reporting, read this first
Interested in seeing some YouTube videos about Kodi? Go here and subscribe
Reply
#22
Hi Martijn, thanks for your input!

I probably hacked away on one of your old scripts then Smile
Would you enlighten and tell what's so wrong with it, and what is the code good for to begin with?
Reply
#23
Haha!

He just means to get rid of the underscores in the python names. They were in the legacy tutorials (something I'm trying to, and will fix).

Hopefully if people follow the example Add-ons in the future, we can get "best practice" coding guidelines into them.

Currently I believe most people start by copying some old Add-on they find on github somewhere.
Reply
#24
I see. Well in my eyes python is odd enough as it is. So loosing the sore undersvores is welcomed. But do I even nwwd to set those variabkes?
Reply
#25
So, Zag, was it useful to you in any way?
Reply
#26
Yep, keep checking the wiki Add-on development pages in the future for new guides Wink
Reply
#27
Are there any reason NOT to simplify the first lines in the above py script. Perhaps there are some standards set by Team Kodi? I'd like to get it right the first time Smile

From this:
Code:
import xbmc, xbmcgui, xbmcaddon, sys
from operator import itemgetter
if sys.version_info >=  (2, 7):
    import json
else:
    import simplejson as json

# Define global variables
WINDOW = xbmcgui.Window( 10000 )

addon        = xbmcaddon.Addon()
addonversion = addon.getAddonInfo('version')
addonid      = addon.getAddonInfo('id')
addonname    = addon.getAddonInfo('name')

# Get value passed from KODI
ALBUMDBID = sys.argv[1]

...

To this:
Code:
import xbmc, xbmcgui
from operator import itemgetter
import json
# Define global variables
WINDOW = xbmcgui.Window( 10000 )

# Get value passed from KODI
ALBUMDBID = sys.argv[1]

...
Reply
#28
https://www.python.org/dev/peps/pep-0008/#imports
Read/follow the forum rules.
For troubleshooting and bug reporting, read this first
Interested in seeing some YouTube videos about Kodi? Go here and subscribe
Reply
#29
So there is no need for simplejson and all those addon variables as I suspected?
But imports should be on separate lines.
Reply
#30
(2015-10-25, 17:24)Torben Wrote: So there is no need for simplejson and all those addon variables? Thanks

uhm?
Well if you want to use them you will absolutely need to define them. Anything you don't use shouldn't be there. Nothing we as "team" dictate as that's just common sense.

The simplejson stuff is from our experience the best you can get as from python 2.7 json is faster than simplejson.
The addon variables are needed for whatever purpose you could think of. Again if you don't need them, don't define them
Read/follow the forum rules.
For troubleshooting and bug reporting, read this first
Interested in seeing some YouTube videos about Kodi? Go here and subscribe
Reply

Logout Mark Read Team Forum Stats Members Help
Query the database for beginners0