Make your own Kodi add-on in Python

Configure the Kodi app

In this post we will learn how to develop a Kodi add-on in Python. You can run the add-on in OpenELEC or LibreELEC on the Orange Pi or Raspberry Pi, or as a matter of fact on any device that can run Kodi. The add-on will read data from various sources, such as an API for the current Bitcoin price, and show it on the main screen.

Most Kodi add-ons are plug-in type add-ons, which work with some sort of media content such as audio, video or pictures. Another type of add-on is a service add-on, which executes some code whenever Kodi starts. We will make an app that shows data on the home screen, so it will be a script add-on.

First we need to make a folder and put ‘script.’ in front of the name, e.g. ‘script.data_read’. Inside, we create a ‘addon.xml’ file, with the following structure:

So here, in the ‘addon’ tag, the ‘id’ attribute should be the folder name, the ‘name’ attribute will be the name we want to show for this app in Kodi. A full description of the ‘addon.xml’ file can be found on the official Kodi wiki, here. In the ‘library’ property of the ‘extension’ tag you need to type in the name of the main Python script that contains the logic of the app, which we will create now.

Kodi add-on Python script

Now we need to develop the main part of the application. Create a new file inside the ‘service.data_read’ and name it ‘service.py’. The script will add a label to the home screen in Kodi. Then it will change the text of the label to whatever you want and change it at regular intervals. To try it out, let’s write a random number inside the label to begin with.

First let’s import the needed modules:

import time
import xbmc
import xbmcgui
import random

Then we add a label control to the home page that changes every second, showing a random number:

if __name__ == '__main__':
#this is the home window
win = xbmcgui.Window(10000)
#get width and height of the screen
h=win.getHeight()
w=win.getWidth()
#put the label in the middle horizontally and 5% down from the top vertically
ctrl=xbmcgui.ControlLabel(w/2,int(.05*h), 125, 75, '0', 'font16')
win.addControl(ctrl)
win.setFocus(ctrl)
while True:
#every second set a random number inside the label
ctrl.setLabel(str(random.randint(0,10)))
time.sleep(1)

The label is in the middle of the screen horizontally and close to the top. I tested with Estuary and Confluence skins. In Estuary it looks like this:

To get some more interesting data we can use any API on the web. For example we can use the CoinDesk API to get the Bitcoin price, every minute. The API can be found here.

So we will write a script that sends a request to that API and parses the output. For this we need the ‘requests’ python module, that you can download from https://github.com/requests/requests.

To create the service script we make a new folder as before, call it ‘service.btc_read’ for example. Again, create a ‘addon.xml and ‘service.py’ file inside the folder. We need to make some slight modifications to the .xml file. We need of course to edit the id and name of the app, in the ‘addon’ tag:

id="service.btc_read"
name="btc reader"

Add this line between the ‘requires’ tags:

<import addon="script.module.requests" version="2.18.4"/>

As for the Python file, we have to import the required modules:

import requests
import json

And then in the while loop, we call the API every 60 seconds, we convert the json string into a Python object and we access the desired fields, in this case the USD rate for Bitcoin. You can of course explore the structure of the API output and change the currency.