Home » Kodi » Kodi Addon Development – sys.argv Explained, Build Menu

Kodi Addon Development – sys.argv Explained, Build Menu

Last updated on August 6th, 2017 at 11:58 am EST

Are you looking to develop a Kodi addon? Understanding sys.argv is the first step towards Kodi addon development and we thought we will take some time to write about it. In this post we are going to teach you how to build menu, in Kodi and also explain sys.argv. We are assuming you know about the basic structure of a Kodi addon, if not you can read about it here.


how to build menu kodi

Kodi Addon Development – sys.argv Explained, Build Menu

Pre-Requisites

Hit the link below to learn how to create a Kodi addon, repository and setup python.

How to create Kodi addon, Repository and Setup Python

The source for the Kodi addon used in this article can be found on github here.

sys.argv Explained : Kodi Addon Development

sys.argv arguments

Kodi uses three sys.argv arguments as mentioned below.

0The base URL of your add-on, e.g. 'plugin://plugin.video.demo1/'
1The process handle for this add-on, as a numeric string
2The query string passed to your add-on, e.g. '?foo=bar&baz=quux'

These three can be a bit confusing to understand, let’s see if we can make life easier for you. An example is the best way to understand this. Let’s go ahead and open the default.py file for our addon (plugin.video.demo1) and go through the code line by line.


import sys
import xbmcgui
import xbmcplugin
import urllib
import urlparse

The import statements are just importing some libraries which we need for the addon to run.

base_url = sys.argv[0]

Here we are setting the varibale base_url with sys.argv[0]. In our case it’s value will be plugin://plugin.video.demo1/

addon_handle = int(sys.argv[1])

Here we are setting the variable addon_handle as sys.argv[1] and converting it into an integer (as sys.argv[1] by default is a numeric string)

args = urlparse.parse_qs(sys.argv[2][1:])

we are setting variable args to sys.argv[2][1:] and converting it to a specific format using the function parse_qs in library urlparse. So, say sys.argv[2] had value as ?foo=bar&baz=quux’ specifying the [1:] we are ignoring the ?, if we had said [5:] we would be ignoring ?fo and starting at the first =.

so sys.argv[2][5:] will be bar&baz=quux

by using the function parse_qs we are converting it into a list.

so in this case args will be set as {‘foo’: [‘bar’], ‘baz’ : [‘quux’]}

Now, if sys.argv[2] was ?foo=bar&foo=bars&quux=spam

then

args would be set to

{‘foo’: [‘bar’, ‘bars’], ‘quux’: [‘spam’]}.

xbmcplugin.setContent(addon_handle, 'movies')

Here we are telling Kodi that our addon content type is movies. Other value are files, songs, artists, albums, movies, tvshows, episodes, musicvideos.

def build_url(query):
return base_url + '?' + urllib.urlencode(query)

def is a keyword used to define a function. In this case function name is build_url which takes an input and adds the base_url (plugin://plugin.video.demo1/) and a ‘?’ before the input. urllid.urlencode will encode the input into url structure. To know more about url encoding click here. So, if input to build_url was say Folder One, it will return below value

plugin://plugin.video.demo1/?Folder+One

mode = args.get('mode', None)

get method (inbuilt Python method) will get the value of word mode in args, if mode does not exist it will return None. This value will be assigned to variable mode. This will be more clear when we look at the values when the addon is executed.

How to Build Menu : Kodi Addon Development

if mode is None:
url = build_url({'mode': 'folder', 'foldername': 'Folder One'})
li = xbmcgui.ListItem('Folder One', iconImage='DefaultFolder.png')
xbmcplugin.addDirectoryItem(handle=addon_handle, url=url,
listitem=li, isFolder=True)
url = build_url({'mode': 'folder', 'foldername': 'Folder Two'})
li = xbmcgui.ListItem('Folder Two', iconImage='DefaultFolder.png')
xbmcplugin.addDirectoryItem(handle=addon_handle, url=url,
listitem=li, isFolder=True)
xbmcplugin.endOfDirectory(addon_handle)

Here we are checking the value of mode. If it is none, then the statements below will be executed, else they will be skipped.

    url = build_url({'mode': 'folder', 'foldername': 'Folder One'})

Here we are calling the function build_url, passing value in parenthesis to it, and the returned value is assigned to variable url.

In this case url will be below value, after appending base_url (plugin://plugin.video.demo1/) and ? before it.

plugin://plugin.video.demo1/?mode=folder&foldername=Folder+One

    li = xbmcgui.ListItem('Folder One', iconImage='DefaultFolder.png')

Here we are using ListItem method from xbmcgui to create a list item.

    xbmcplugin.addDirectoryItem(handle=addon_handle, url=url,
listitem=li, isFolder=True)

Here we are telling Kodi (xbmc) to add the list item added above to our directory (aka main folder). The isFolder=True tells Kodi that this is a folder and not playable media.

The next few lines create another folder called Folder Two.

    xbmcplugin.endOfDirectory(addon_handle)

Tells Kodi to end the Directory (aka folder listing). This is what we just built.

Kodi Addon development

 

elif mode[0] == 'folder':
foldername = args['foldername'][0]
url = 'http://www.vidsplay.com/wp-content/uploads/2017/04/alligator.mp4'
li = xbmcgui.ListItem(foldername + ' Video', iconImage='DefaultVideo.png')
xbmcplugin.addDirectoryItem(handle=addon_handle, url=url, listitem=li)
xbmcplugin.endOfDirectory(addon_handle)

if the value of mode[0] is set to folder, then code below it is executed. By mode[0] we mean the first value in the list mode. Read about python lists here.

foldername = args['foldername'][0]

Here we look for all text names foldername in args, and assign first occurrence to variable foldername. Again this will be more clear when we look at real time values.

The next few lines of code are again creating a list item, and a directory listing of videos this time. This will result in both Folder One and Folder Two having the same video link but with different names.

Folder One video

 

Kodi Addon Development : sys.argv Values in Real Time

Let’s get down to the real thing and see how these sys.argv[0], sys.argv[1], sys.argv[2] change value over the execution of a Kodi addon.

Start the Addon

When you start the addon for the first time, file default.py is executed and you will get below screen.

Addon first run

Values during runtime

base_url = sys.argv[0] = plugin://plugin.video.demo1/
addon_handle = int(sys.argv[1]) = 1
args = urlparse.parse_qs(sys.argv[2][1:]) = [] (empty list)

mode = args.get('mode', None) = None (as args has nothing)

Now since mode is None, code will go into first if loop.

if mode is None:
url = build_url({'mode': 'folder', 'foldername': 'Folder One'})

will make

url = plugin://plugin.video.demo1/?mode=folder&foldername=Folder+One

li = xbmcgui.ListItem('Folder One', iconImage='DefaultFolder.png')

will create a list item named Folder One, and

xbmcplugin.addDirectoryItem(handle=addon_handle, url=url,
 listitem=li, isFolder=True)

will display it on Kodi interface.

Similarly for Folder 2 code,

url will be

plugin://plugin.video.demo1/?mode=folder&foldername=Folder+Two

Select Folder One

Say you select Folder One, default.py will be executed again and you will get below screen.

Folder One Video

Values during runtime

base_url = sys.argv[0] = plugin://plugin.video.demo1/
addon_handle = int(sys.argv[1]) = 2
args = urlparse.parse_qs(sys.argv[2][1:]) = 
         {'mode': ['folder'], 'foldername': ['Folder One']}

Remember we clicked on Folder 1, which had url as

plugin://plugin.video.demo1/?mode=folder&foldername=Folder+One

sys.argv[2] starts from ?, but we are starting ignoring the ? by using sys.argv[2][1:] and parse_qs breaks the url into a list for python.

mode = args.get('mode', None) = folder (as args has mode folder)

As mode is folder, first if loop will be skipped and second if loop will be executed.

elif mode[0] == 'folder':

Here first statement is

foldername = args['foldername'][0]

Remember args is a list with below content

{'mode': ['folder'], 'foldername': ['Folder One']}

so

foldername = args['foldername'][0] = Folder One

if args was say

{'mode': ['folder'], 'foldername': ['Folder One','Folder Three']}

then

args['foldername'][0] = Folder One

but

args['foldername'][1] = Folder Three

if you tried

args['mode][1]

you would get an error as it does not exist.

If you click on Folder One Video, the video we linked would play.

Click on Double Dots After Playing Video

Say you are done watching the video, and click on the double dots above Folder one. You will go back to the main menu.

Kodi Addon development

Values during runtime

base_url = sys.argv[0] = plugin://plugin.video.demo1/
addon_handle = int(sys.argv[1]) = 3
args = urlparse.parse_qs(sys.argv[2][1:]) = [] (empty list)

mode = args.get('mode', None) = None (as args has nothing)

Now since mode is None, code will go into first if loop.

if mode is None:
url = build_url({'mode': 'folder', 'foldername': 'Folder One'})

will make

url = plugin://plugin.video.demo1/?mode=folder&foldername=Folder+One

li = xbmcgui.ListItem('Folder One', iconImage='DefaultFolder.png')

will create a list item named Folder One, and

xbmcplugin.addDirectoryItem(handle=addon_handle, url=url,
 listitem=li, isFolder=True)

will display it on Kodi interface.

Similarly for Folder 2 code,

url will be

plugin://plugin.video.demo1/?mode=folder&foldername=Folder+Two

Select Folder Two

Instead of selecting Folder One we select Folder two, you will see below screen.

folder two video

Values during runtime

base_url = sys.argv[0] = plugin://plugin.video.demo1/
addon_handle = int(sys.argv[1]) = 4
args = urlparse.parse_qs(sys.argv[2][1:]) = 
         {'mode': ['folder'], 'foldername': ['Folder Two']}

Remember we clicked on Folder 2, which had url as

plugin://plugin.video.demo1/?mode=folder&foldername=Folder+Two

sys.argv[2] starts from ?, but we are starting ignoring the ? by using sys.argv[2][1:] and parse_qs breaks the url into a list for python.

mode = args.get('mode', None) = folder (as args has mode folder)

As mode is folder, first if loop will be skipped and second if loop will be executed.

elif mode[0] == 'folder':

Here first statement is

foldername = args['foldername'][0]

Remember args is a list with below content

{'mode': ['folder'], 'foldername': ['Folder Two']}

so

foldername = args['foldername'][0] = Folder Two

If you click on Folder One Video, the video we linked would play.

That’s it peeps. Hope this helps upcoming Kodi addon developers.

What do you think of our first tutorial for Kodi addon development? Let us know via comments below.

References

http://kodi.wiki/view/audio-video_add-on_tutorial

Do you STREAM on Kodi WITHOUT a VPN?

We STRONGLY suggest you use a VPN Service like IPVanish.

Why VPN? 1. Stay Secure and Anonymous Online 2. Stream Content Anonymously 3. Access Geo Locked Content 4. Hide Your Activity from your ISP

Why IPVanish? 1. They Keep No Logs = No Tracking 2. Native apps for Android, Android TV, iOS, Mac, Linux 3. No Speed Slowdown 4. Highly Recommended 5. 7 Days Money Back Guarantee

Sign-Up for IPVanish

Follow Us

Subscribe to Our Youtube Channel