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:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<addon id="service.data_read"
       name="Data reader"
       version="1.0.0"
       provider-name="">
  <requires>
    <import addon="xbmc.python" version="2.1.0"/>
  </requires>
  <extension point="xbmc.service" library="service.py" start="login" />
  <extension point="xbmc.addon.metadata">
    <platform>all</platform>
    <summary lang="en"></summary>
  </extension>
</addon>

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.

    
while True:
    r=requests.get('https://api.coindesk.com/v1/bpi/currentprice.json')
    data=json.loads(r.text)
    price=data['bpi']['USD']['rate']
    ctrl.setLabel(price)
    time.sleep(60)

To install this add-on, zip the folder and put it somewhere where Kodi can see it. Then in Kodi go to Add-ons -> Install from .zip file and navigate to the location of the archive.

The label should look like this:

You can customize the size and format of the label as you wish. You can find the full code of the add-on here:https://github.com/ionutpi/kodi-bitcoin-addon.

Face recognition on the Orange Pi with OpenCV and Python

Leave a Reply

Your email address will not be published / Required fields are marked *