Search code examples
pythonkodi

kodi addon - print result on home page


First thing first, this is a very basic addon, as well as my first addon ever for kodi.

I've simply cloned the hello world example and changed the addon.py file to this:

import xbmcaddon
import xbmcgui
import subprocess
import urllib
import requests
import json

addon       = xbmcaddon.Addon()
addonname   = addon.getAddonInfo('name')


def check_location():
    send_url = 'http://freegeoip.net/json'
    r = requests.get(send_url)
    j = json.loads(r.text)
    city = j['city']
    return "Currently connected from: " + city;

def check_ip():
    public_ip = subprocess.check_output(["ifconfig `ip route get 8.8.8.8 | grep 8.8.8.8 | cut -d' ' -f5` | grep \'inet \' | awk -F'[: ]+' '{ print $4 }'"], shell=True);
    result = ("Your IP is : %s " % public_ip);
    return result;

xbmcgui.Dialog().ok("Check IP and Location", check_ip(), check_location())

This simple script, get the external Ip and the location and prints them in a modal when runs.

What I'd like to do, is to have this results printed on the home page (I am using titan skin if this help in any way) but I have no idea where to start, as google doesn't really help either.

I don't care if I have to create two new menu entries with that as a result, or I have I to change some skin files, but the idea is to keep the IP and the Location always visible on the home page, as I need to check instantly if my VPN drops the connection so that I can reconnect (or at least I know i'm not connected anymore)

Thanks in advance for any help


Solution

  • Probably, it may be done by manipulating skin XML files, but I have zero knowledge of Kodi skinning.

    xbmcgui.Window class can be used to attach various xbmcgui controls to existing Kodi windows. Below is a modified code that I use in one of my addons:

    window = xbmcgui.Window(10000)
    label = xbmcgui.ControlLabel(10, 10, 1000, 50, 'Some label text')
    window.addControl(label)
    

    10000 is a numeric ID of the Home window. The list of window IDs can be found in Kodi Wiki.

    Additional notes:

    • Controls are placed in the window using pixel coordinates starting from top left corner. However, different skins have different pixel resolutions so visual placement of your controls may vary in different skins.
    • Although a Control is attached to a window while Kodi is running, you need to use some kind of a long running loop to update your information on the screen (see setLabel method). For that you might want to organize your addon as a service addon.