How to create a KODI entry to run a script?

I would like to integrate a video update script (bash) into Kodi such that it appears somewhere on the menu and I can start it from there. For example in MainMenu/Programs.
Is this possible? If so how?
The script updates a set of strm files to the latest available on the server and right now I have to ssh into the OSMC box to start it, which is a bit inconvenient.
The script itself just townloads a tar file using curl and then expands it into a “streams” folder on my media drive.

You would need to wrap it in a python script

Too bad, I have never written python and when I tried modifying python code I realized I have to go the python for dummies course first…

What I want to do is execute this system call from a Kodi menu item for example named “UpdateStreams”:
/home/osmc/bin/getstreamlist R
The argument R clears the target streams directory before downloading the tgz file from the server and then expanding it into the streams folder.
I do not need any feedback from the call just that it was executed.

Hya!

It’s not that they wanted to to rewrite your script, just a means to activate it from the menu. This should work:

import subprocess

subprocess.Popen(["/home/osmc/bin/getstreamlist", "R"])

that’s all that needs to be there :slight_smile:

I tested it in SSH (not Kodi) like this:

#!/usr/bin/env bash

echo "Hello world, from ${1}"

and then used

python
import subprocess
subprocess.Popen(["/tmp/bashexample.sh", "testscript"])

to call and execute

osmc@osmc:~$ python
Python 2.7.16 (default, Oct 10 2019, 22:02:15)
[GCC 8.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import subprocess
>>> subprocess.Popen(["/tmp/bashexample.sh", "testscript"])
<subprocess.Popen object at 0xf7133c70>
>>> Hello world, from testscript

which works identical in Python 3:

osmc@osmc:~$ python3
Python 3.7.3 (default, Jan 22 2021, 20:04:44)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import subprocess
>>> subprocess.Popen(["/tmp/bashexample.sh", "testscript"])
<subprocess.Popen object at 0xf743bc70>
>>> Hello world, from testscript

The end result could be something like this:

file /tmp/pythonwrapper.py has

#!/usr/bin/env python3

import subprocess
subprocess.run(["/tmp/bashexample.sh", "RunFromPythonScript"])

and should then be called from Kodi using

XBMC.RunScript(/tmp/pythonwrapper.py)

which would trigger the bash script, which would result in

Hello world, from RunFromPythonScript

Note: seeing that Kodi is limited to Python, I’m not sure if the python shebang should be in the .py file.

Thanks for the explanations.
Being a non-python person I cannot follow the above…
Could you please say which of the above snippets are supposed to be typed on the command line and which are going into files, including the names of those files?
I checked the existence of python on my OSMC box:

$ which python
/usr/bin/python
$ python --version
Python 2.7.16

But the first command you list (import) does not seem to exist:

$ which import
$

Or is it supposed to be inside a file, if so what is the proper name of such file?

Regarding “called from Kodi”:
How do you enter such a command into Kodi???
I was hoping for a GUI item below Programs named something like “UpdateStreams”, which I could click inside Kodi.
The execution of my script just takes a second or so, there is no need to create a user feedback dialogue.

Okay, so this is threefold:

  • Your bash script, already in place /home/osmc/bin/getstreamlist
  • The python script, create /home/osmc/bin/getstreamlistwrapper.py
#!/usr/bin/env python3

import subprocess
subprocess.run(["/home/osmc/bin/getstreamlist", "R"])

Ive done something similar, using the osmc skin you can just add an entry to the sub menu of one of the main menu items…eg, Settings.
From there you can select ‘Change action’, then ‘Custom item’, then enter:

XBMC.RunScript(/home/osmc/bin/getstreamlistwrapper.py)

LATER… (but posts were crossing)
I did the following:

  1. Created a file ~/test/getstreams.py with this content
#!/usr/bin/env python

import subprocess
subprocess.run(["/home/osmc/bin/getstreamlist", "R"])
  1. Set it executable

  2. Tried running it:

$ ./getstreams.py
Traceback (most recent call last):
  File "./getstreams.py", line 4, in <module>
    subprocess.run(["/home/osmc/bin/getstreamlist", "R"])
AttributeError: 'module' object has no attribute 'run'

So I am missing something…

Hi,

The code is for python3, where as you are calling python2. If you are running the latest version of osmc change the getstreams.py to:

#!/usr/bin/env python3

import subprocess
subprocess.run(["/home/osmc/bin/getstreamlist", "R"])

Should work.

But if your still happy to have this appear under MainMenu/Programs, you may get better on with this addon. You’ll need to install from zip file.

Here is the code so that you can check for your self that it’s save to run and for future reference.

Thanks Tom.

1 Like

Thanks, by adding back the 3 after python the script ran as it should.
Since the script actually prints a lot of output, which I guess will not be shown in Kodi, maybe the output should be redirected to /dev/null?
How can this be done in the call string from python?

1 Like

I think this should help you with that Redirecting stdout to "nothing" in python - Stack Overflow

You should use that in the .py file, where the print in the example is your subprocess.run entry

Thanks for the zipfile!
Problem when installing from zip (after enabling zip installation) was shown on screen for a short time:

Failed to install Add-on from zip file
The dependency on xbmc.python.version 3.0.0 could not be satisfied.

The OSMC version is:

OSMC 2020.11-1 (kernel: Linux 4.19.122-2-osmc)
OSMC running Kodi 18.9

What can I do now?

Any reason that you have not updated to the latest stable OSMC version? Python3 comes with the lastes OSMC/Kodi version

1 Like

Well, before I moved out to my summer home I updated my OSMC boxes sometime in the spring of this year, I think.
When was the latest release done?

Stable in August

So then I will have to set aside some time when the TV is not in use to do the update…
It takes a very long time to do it in the summer home due to the use of mobile broadband Internet out here…

1 Like

While you also want to check your sources.list as you seem to have a strange combination of Kernel and Kodi version.

I tested to update my secondary OSMC box since it is not hooked to our main TV. This turns out to have been fortunate because that update got me Kodi 19.1 but at the same time disabled many or all of my add-ons!
Here is a list of the add-ons that are no longer available:

The following add-ons are incompatible with this version of Kodi and have been automatically disabled:
Backup
CBSN News
DPlay
ESA Videos
Keymap Editor
Nasa
NPR,
Retrospect
Skin Helper Service
Skin Widgets
Sveriges Radio
SVT Play
TED Talks
TV3 Play
Viaplay
YouTube

Amazing!
So add-ons like YouTube, SVT Play, Nasa, TED Talks etc are now incompatible? Why is this so?
And is this the reason for the disclaimer during installation to go to Blog - OSMC to check before proceeding?
I tried that on my phone but did not find anything alarming.

Where can I check this?
And what should I look for?

Check cat /etc/apt/sources.list should include deb http://apt.osmc.tv buster main and not deb http://apt.osmc.tv buster-devel main

It contains these 4 entries:

deb http://ftp.debian.org/debian buster main contrib non-free
deb http://ftp.debian.org/debian/ buster-updates main contrib non-free
deb http://security.debian.org/ buster/updates main contrib non-free
deb http://apt.osmc.tv buster main