Guest - Testers are needed for the reworked CDateTime core component. See... https://forum.kodi.tv/showthread.php?tid=378981 (September 29) x
  • 1(current)
  • 2
  • 3
  • 4
  • 5
  • 22
Release Execute user tasks for Kodi events (callbacks ver2)
#1
Hello,

Pilulli originally wrote a service addon doing this. Long ago, I forked this and have apologetically hi-jacked his thread with my fork.
I am starting this new thread with the release of a completely rewritten version of my own code.

In short, users can create their own tasks that will be run when specific events occur in Kodi.
If you are familiar with the smartphone app IFTTT, it works something like that.
Task types include running scripts, running python scripts in the Kodi namespace, running Kodi builtin functions and sending http response objects.
There are currently 28 different events that can be responded to.
Lastly, new task types can be created and added into the program to share the fruits of your labors, if you wish.

Please see the wiki for this addon at: Add-on:Kodi_Callbacks (wiki)
I put a lot of effort into writing the wiki. I won't duplicate the details here.

As of 22-4-2016 the addon can be installed from the OFFICIAL KODI REPO (Isengard or higher).

Alternatively, the addon can be downloaded from Github at: https://github.com/KenV99/script.service...astero.zip
PLEASE RENAME THE DIRECTORY AFTER INSTALLING TO: 'script.service.kodi.callbacks'. (i.e. remove '-master').


Please post-back with problems or questions. Please post a link to an uploaded log if you are encountering errors. Also NEW click 'Write settings to log' on the Settings-General page so that your settings are in the log.

Update 6-16-16:
New version 1,0.0 in the official repo.
Several bug fixes for window monitoring, unicode support and shutdown events.

Update 2-27-16:
MAJOR update!
Unfortunately, this one will require you to hit the default button and re-enter your settings. Sorry!
This implements a workaround for a Kodi settings issue that prevented me from properly submitting for inclusion in the official repo.
You will see several new settings and the way that the settings are implemented.
Check out the Update tools too! If you experience an issue downloading from GitHub, just try again. It seems to fail sometimes on the first time around.

Update 2-17-16:
Added schedule publisher. Using 24H time, tasks can run at a set time. Alternatively a task can run at a set interval.
Added update tools to settings. Beta feature. Hopefully can streamline installing new versions.

Update 2-3-16:
Added a file watchdog routine to detect file system changes between runs of Kodi - during startup if files/directories in a watched file have changed, the event fires.
Requires a normal shutdown when Kodi exits.
Added some exception catching for Android.

Update 1-31-16:
Changed name to script.service.kodi.callbacks to be c/w official repo naming rules.
Please note that this update will require you to input your settings again. Apologies for that.

Update 1-29-16:
Fixed bug in localization routine for strings with newlines.
Made localization files Singleton to decrease disk I/O.

Update 1-25-16:
I localized all the strings that I could except the ones with the selector bug and pushed an update. Please let me know if you see any bugs in the Settings or get strange or empty messages on the screen or in the log.
This won't directly affect anyone since there are no translations, but wanted to make you aware.
Added on Resume After Idle event.

Update 1-22-16:
Several bugs were fixed including the builtin (kudos dfns) and an occasional issue on windows with scripts generating an access denied error.
I have added an Event for file system changes inspired by takoi's service.watchdog and the dependency mentioned below has been removed.
The syntax for the parameters can be found (loosely) at: https://pythonhosted.org/watchdog/api.ht...entHandler
I also added to the general settings a button to test that the four native tasks are working properly on your system to help with troubleshooting.
Note: this uses Kodi's JSON server and assumes that it is not configured with a username and password. If your's is, some tests will fail.
Reply
#2
Good work, do you want the other thread closed?
If I have helped you or increased your knowledge, click the 'thumbs up' button to give thanks :) (People with less than 20 posts won't see the "thumbs up" button.)
Reply
#3
(2016-01-13, 10:37)nickr Wrote: Good work, do you want the other thread closed?

Since it's actually pilluli's thread, I think it should be up to him.
By closing it, it remains view-able but won't take new posts?
Reply
#4
Nice going Ken. I will try it out on my test Jarvis machine as soon as I can. Your existing addon works great, so will be interesting to see just what changed.

One question. I see there is a onPlaybackStarted and a onPlaybackResumed. I will have to see if this Resume affects anything as my current Python script when it senses a onPlaybackStopped , when watching a TV Show, and gives me the option of Resume, Delete, or Play another show. If I press Resume, then it resumes play. So wondering if that will not trigger the onPlaybackStarted as the existing addon does. I guess I will find out and then figure out how to deal with it .

Keep up the good work. This is one of the most useful addons I have come across since I went to XBMC/Kodi about 2 years ago.
Reply
#5
(2016-01-13, 20:04)ReplayHarry Wrote: Nice going Ken. I will try it out on my test Jarvis machine as soon as I can. Your existing addon works great, so will be interesting to see just what changed.

One question. I see there is a onPlaybackStarted and a onPlaybackResumed. I will have to see if this Resume affects anything as my current Python script when it senses a onPlaybackStopped , when watching a TV Show, and gives me the option of Resume, Delete, or Play another show. If I press Resume, then it resumes play. So wondering if that will not trigger the onPlaybackStarted as the existing addon does. I guess I will find out and then figure out how to deal with it .

Keep up the good work. This is one of the most useful addons I have come across since I went to XBMC/Kodi about 2 years ago.

Thank you for the support.

I think from the situation that you describe, it will indeed trigger another onPlaybackStarted event.
The simplest solution that comes to mind is to have your script create a file when you 'press Resume', just as a means of passing that info along that you have resumed from your script.
Then, in whatever code is responding to the onPlaybackStarted event, detect the presence of the file and if it exists, do not execute whatever you usually are doing for a true start AND have it delete the file.
However, then you will also then need code that deletes that file during the onPlaybackEnded event to avoid having the next fresh start think that you are still resuming.
Reply
#6
(2016-01-13, 13:49)KenV99 Wrote: By closing it, it remains view-able but won't take new posts?

Exactly right. I'll talk to piluli.
If I have helped you or increased your knowledge, click the 'thumbs up' button to give thanks :) (People with less than 20 posts won't see the "thumbs up" button.)
Reply
#7
hi,


tried your addon but it fails with "ImportError: No module named taskABC" when i tried to to run a builtin command "StartPVRManager"

any hints? is it correct t just use StartPVRManager or should i use xbmx.StartPVRManager ?

Code:
14:48:52 T:139863512831744   DEBUG: CPythonInvoker(7, /home/kodi/.kodi/addons/service.kodi.callbacks/default.py): the source file to load is "/home/kodi/.kodi/addons/service.kodi.callbacks/default.py"
14:48:52 T:139863512831744   DEBUG: CPythonInvoker(7, /home/kodi/.kodi/addons/service.kodi.callbacks/default.py): setting the Python path to /home/kodi/.kodi/addons/service.kodi.callbacks:/home/kodi/.kodi/addons/script.module.requests/lib:/usr/lib64/python27.zip:/usr/lib64/python2.7:/usr/lib64/python2.7/plat-linux2:/usr/lib64/python2.7/lib-tk:/usr/lib64/python2.7/lib-old:/usr/lib64/python2.7/lib-dynload:/usr/lib64/python2.7/site-packages:/usr/lib64/python2.7/site-packages/gtk-2.0
14:48:52 T:139863512831744   DEBUG: CPythonInvoker(7, /home/kodi/.kodi/addons/service.kodi.callbacks/default.py): entering source directory /home/kodi/.kodi/addons/service.kodi.callbacks
14:48:52 T:139863512831744   DEBUG: CPythonInvoker(7, /home/kodi/.kodi/addons/service.kodi.callbacks/default.py): instantiating addon using automatically obtained id of "service.kodi.callbacks" dependent on version 2.19.0 of the xbmc.python api
14:48:52 T:139863512831744   ERROR: EXCEPTION Thrown (PythonToCppException) : -->Python callback/script returned the following error<--
                                             - NOTE: IGNORING THIS CAN LEAD TO MEMORY LEAKS!
                                            Error Type: <type 'exceptions.ImportError'>
                                            Error Contents: No module named taskABC
                                            Traceback (most recent call last):
                                              File "/home/kodi/.kodi/addons/service.kodi.callbacks/default.py", line 24, in <module>
                                                import resources.lib.pubsub as PubSub_Threaded
                                              File "/home/kodi/.kodi/addons/service.kodi.callbacks/resources/lib/__init__.py", line 20, in <module>
                                                from resources.lib.taskABC import AbstractTask
                                            ImportError: No module named taskABC
                                            -->End of Python script error report<--
14:48:52 T:139863512831744    INFO: Python script stopped
14:48:52 T:139863512831744   DEBUG: Thread LanguageInvoker 139863512831744 terminating
Reply
#8
Sorry, I have been having issues with my git to github routine not reflecting capitalization changes.
Anyway, it should be fixed. Please download again and report back.

Thanks.
Reply
#9
ok i'll test it, btw: is the a "return from idle" event?
Reply
#10
ok, I got it installed. Prior to installing, I also updated to Jarvis Beta5.

Here is my big question: How do I get my parameters in there now? I setup a task (Python script) and setup the events I needed (onPalybackStarted and onPlaybackEnded).
In the old version there was a parameters screen where I asked for event id, media type, file path plus filename, and title. How do I get those parameters passed to my Python script now?

update 1:30pm

I guess I should have noticed the %ti, %fn, etc, but the format of what shows up in those parameters seems to have changed a lot.
Will have to mess with a bit and see if I can get it to work.

Thanks again.
Reply
#11
ok, tried latest and now get a
Code:
service.kodi.callbacks" dependent on version 2.19.0 of the xbmc.python api
20:18:08 T:140636648875776   ERROR: EXCEPTION Thrown (PythonToCppException) : -->Python callback/script returned the following error<--
                                             - NOTE: IGNORING THIS CAN LEAD TO MEMORY LEAKS!
                                            Error Type: <type 'exceptions.ImportError'>
                                            Error Contents: No module named pydevd
                                            Traceback (most recent call last):
                                              File "/home/kodi/.kodi/addons/service.kodi.callbacks-master/default.py", line 52, in <module>
                                                import pydevd
                                            ImportError: No module named pydevd
                                            -->End of Python script error report<--
20:18:08 T:140636648875776    INFO: Python script stopped

Huh
Reply
#12
Sorry dfns - forgot turn off the debug flag. Should be fixed now!

The latest version has service.watchdog as a new dependency and supports watching files and or directories. Will update wiki soon.
Will ask takoi for permission to copy over some of his code and remove the dependency.

ReplyHarry - this should give a lot more flexibility in formatting the output just as you want it.
Reply
#13
Thank you for a really handy script!

However, I do have some issues. First off, I get an exception when testing my HTTP task. Following the test I tried to re-generate the settings file which also threw an exception. See log below. Lastly I noticed that my task started when I toggled the script disable/enable but it doesnt seem to work with urls that requrie a login. The Event returns "Accesss denied" just as if I tried to access the url without login credentials. The url I tried had the following format: 'http://Johan:[email protected]:8888/Shutdown.html' and I added the args 'action=Restart'

Code:
20:57:02 T:5508  NOTICE: $$$ [kodi.callbacks] Staring kodi.callbacks ver: 0.9.1
20:57:03 T:5508  NOTICE: $$$ [kodi.callbacks] - Settings read
20:57:03 T:5508  NOTICE: $$$ [kodi.callbacks] - Dispatcher initialized
20:57:03 T:5508  NOTICE: $$$ [kodi.callbacks] - Subscriber for event: onStartup, task: T1 created
20:57:03 T:5508  NOTICE: $$$ [kodi.callbacks] - Dispatcher started
20:57:03 T:5508  NOTICE: $$$ [kodi.callbacks] - Publisher(s) started
20:57:03 T:5508  NOTICE: $$$ [kodi.callbacks] - Entering wait loop
20:57:03 T:16124  NOTICE: $$$ [kodi.callbacks] - Task starting for onStartup
20:57:03 T:16124  NOTICE: $$$ [kodi.callbacks] - Command for Task T1, Event onStartup completed succesfully!
                                            The following message was returned: Access denied.
20:57:03 T:16124  NOTICE: $$$ [kodi.callbacks] - Task finalized for onStartup
20:57:23 T:20220  NOTICE: $$$ [kodi.callbacks] - Settings for test read
20:57:23 T:20220  NOTICE: $$$ [kodi.callbacks] - Creating subscriber for test
20:57:23 T:20220  NOTICE: $$$ [kodi.callbacks] - Test subscriber created successfully
20:57:23 T:20220   ERROR: EXCEPTION Thrown (PythonToCppException) : -->Python callback/script returned the following error<--
                                             - NOTE: IGNORING THIS CAN LEAD TO MEMORY LEAKS!
                                            Error Type: <type 'exceptions.KeyError'>
                                            Error Contents: ('expArgs',)
                                            Traceback (most recent call last):
                                              File "C:\Users\HTPC User\AppData\Roaming\Kodi\addons\service.kodi.callbacks-master\default.py", line 289, in <module>
                                                test(eventId)
                                              File "C:\Users\HTPC User\AppData\Roaming\Kodi\addons\service.kodi.callbacks-master\default.py", line 247, in test
                                                kwargs = events[evtsettings['type']]['expArgs']
                                            KeyError: ('expArgs',)
                                            -->End of Python script error report<--
20:58:49 T:6228   ERROR: EXCEPTION Thrown (PythonToCppException) : -->Python callback/script returned the following error<--
                                             - NOTE: IGNORING THIS CAN LEAD TO MEMORY LEAKS!
                                            Error Type: <type 'exceptions.IOError'>
                                            Error Contents: [Errno 2] No such file or directory: 'C:\\Users\\HTPC User\\AppData\\Roaming\\Kodi\\addons\\service.kodi.callbacks\\resources\\settings.xml'
                                            Traceback (most recent call last):
                                              File "C:\Users\HTPC User\AppData\Roaming\Kodi\addons\service.kodi.callbacks-master\default.py", line 286, in <module>
                                                generate_settingsxml()
                                              File "C:\Users\HTPC User\AppData\Roaming\Kodi\addons\service.kodi.callbacks-master\resources\lib\xml_gen.py", line 113, in generate_settingsxml
                                                with open(fn, mode='w') as f:
                                            IOError: [Errno 2] No such file or directory: 'C:\\Users\\HTPC User\\AppData\\Roaming\\Kodi\\addons\\service.kodi.callbacks\\resources\\settings.xml'
                                            -->End of Python script error report<--
Reply
#14
(2016-01-16, 22:21)LuffarJoh Wrote: Thank you for a really handy script!

However, I do have some issues. First off, I get an exception when testing my HTTP task. Following the test I tried to re-generate the settings file which also threw an exception. See log below. Lastly I noticed that my task started when I toggled the script disable/enable but it doesnt seem to work with urls that requrie a login. The Event returns "Accesss denied" just as if I tried to access the url without login credentials. The url I tried had the following format: 'http://Johan:[email protected]:8888/Shutdown.html' and I added the args 'action=Restart'

Code:
20:57:02 T:5508  NOTICE: $$$ [kodi.callbacks] Staring kodi.callbacks ver: 0.9.1
20:57:03 T:5508  NOTICE: $$$ [kodi.callbacks] - Settings read
20:57:03 T:5508  NOTICE: $$$ [kodi.callbacks] - Dispatcher initialized
20:57:03 T:5508  NOTICE: $$$ [kodi.callbacks] - Subscriber for event: onStartup, task: T1 created
20:57:03 T:5508  NOTICE: $$$ [kodi.callbacks] - Dispatcher started
20:57:03 T:5508  NOTICE: $$$ [kodi.callbacks] - Publisher(s) started
20:57:03 T:5508  NOTICE: $$$ [kodi.callbacks] - Entering wait loop
20:57:03 T:16124  NOTICE: $$$ [kodi.callbacks] - Task starting for onStartup
20:57:03 T:16124  NOTICE: $$$ [kodi.callbacks] - Command for Task T1, Event onStartup completed succesfully!
                                            The following message was returned: Access denied.
20:57:03 T:16124  NOTICE: $$$ [kodi.callbacks] - Task finalized for onStartup
20:57:23 T:20220  NOTICE: $$$ [kodi.callbacks] - Settings for test read
20:57:23 T:20220  NOTICE: $$$ [kodi.callbacks] - Creating subscriber for test
20:57:23 T:20220  NOTICE: $$$ [kodi.callbacks] - Test subscriber created successfully
20:57:23 T:20220   ERROR: EXCEPTION Thrown (PythonToCppException) : -->Python callback/script returned the following error<--
                                             - NOTE: IGNORING THIS CAN LEAD TO MEMORY LEAKS!
                                            Error Type: <type 'exceptions.KeyError'>
                                            Error Contents: ('expArgs',)
                                            Traceback (most recent call last):
                                              File "C:\Users\HTPC User\AppData\Roaming\Kodi\addons\service.kodi.callbacks-master\default.py", line 289, in <module>
                                                test(eventId)
                                              File "C:\Users\HTPC User\AppData\Roaming\Kodi\addons\service.kodi.callbacks-master\default.py", line 247, in test
                                                kwargs = events[evtsettings['type']]['expArgs']
                                            KeyError: ('expArgs',)
                                            -->End of Python script error report<--
20:58:49 T:6228   ERROR: EXCEPTION Thrown (PythonToCppException) : -->Python callback/script returned the following error<--
                                             - NOTE: IGNORING THIS CAN LEAD TO MEMORY LEAKS!
                                            Error Type: <type 'exceptions.IOError'>
                                            Error Contents: [Errno 2] No such file or directory: 'C:\\Users\\HTPC User\\AppData\\Roaming\\Kodi\\addons\\service.kodi.callbacks\\resources\\settings.xml'
                                            Traceback (most recent call last):
                                              File "C:\Users\HTPC User\AppData\Roaming\Kodi\addons\service.kodi.callbacks-master\default.py", line 286, in <module>
                                                generate_settingsxml()
                                              File "C:\Users\HTPC User\AppData\Roaming\Kodi\addons\service.kodi.callbacks-master\resources\lib\xml_gen.py", line 113, in generate_settingsxml
                                                with open(fn, mode='w') as f:
                                            IOError: [Errno 2] No such file or directory: 'C:\\Users\\HTPC User\\AppData\\Roaming\\Kodi\\addons\\service.kodi.callbacks\\resources\\settings.xml'
                                            -->End of Python script error report<--
The first error should be fixed, please download latest version.

The second error occurred because you have not renamed the directory from service.kodi.callbacks-master to service.kodi.callbacks (see http://kodi.wiki/view/Add-on:Kodi_Callbacks#Details)

However, you do not need to regenerate the xml file unless you have created your own custom task class and placed it in the resources.lib.tasks.folder. (see http://kodi.wiki/view/Add-on:Kodi_Callba...ustom_task)

If this is indeed what you have done, then just rename the folder and try again.

If you require an interactive login, then no, this will not work. If you can provide the login information directly such as: 'http://username:[email protected]" that may work. But it's untested.
Reply
#15
(2016-01-16, 22:57)KenV99 Wrote:
(2016-01-16, 22:21)LuffarJoh Wrote: Thank you for a really handy script!

However, I do have some issues. First off, I get an exception when testing my HTTP task. Following the test I tried to re-generate the settings file which also threw an exception. See log below. Lastly I noticed that my task started when I toggled the script disable/enable but it doesnt seem to work with urls that requrie a login. The Event returns "Accesss denied" just as if I tried to access the url without login credentials. The url I tried had the following format: 'http://Johan:[email protected]:8888/Shutdown.html' and I added the args 'action=Restart'

Code:
20:57:02 T:5508  NOTICE: $$$ [kodi.callbacks] Staring kodi.callbacks ver: 0.9.1
20:57:03 T:5508  NOTICE: $$$ [kodi.callbacks] - Settings read
20:57:03 T:5508  NOTICE: $$$ [kodi.callbacks] - Dispatcher initialized
20:57:03 T:5508  NOTICE: $$$ [kodi.callbacks] - Subscriber for event: onStartup, task: T1 created
20:57:03 T:5508  NOTICE: $$$ [kodi.callbacks] - Dispatcher started
20:57:03 T:5508  NOTICE: $$$ [kodi.callbacks] - Publisher(s) started
20:57:03 T:5508  NOTICE: $$$ [kodi.callbacks] - Entering wait loop
20:57:03 T:16124  NOTICE: $$$ [kodi.callbacks] - Task starting for onStartup
20:57:03 T:16124  NOTICE: $$$ [kodi.callbacks] - Command for Task T1, Event onStartup completed succesfully!
                                            The following message was returned: Access denied.
20:57:03 T:16124  NOTICE: $$$ [kodi.callbacks] - Task finalized for onStartup
20:57:23 T:20220  NOTICE: $$$ [kodi.callbacks] - Settings for test read
20:57:23 T:20220  NOTICE: $$$ [kodi.callbacks] - Creating subscriber for test
20:57:23 T:20220  NOTICE: $$$ [kodi.callbacks] - Test subscriber created successfully
20:57:23 T:20220   ERROR: EXCEPTION Thrown (PythonToCppException) : -->Python callback/script returned the following error<--
                                             - NOTE: IGNORING THIS CAN LEAD TO MEMORY LEAKS!
                                            Error Type: <type 'exceptions.KeyError'>
                                            Error Contents: ('expArgs',)
                                            Traceback (most recent call last):
                                              File "C:\Users\HTPC User\AppData\Roaming\Kodi\addons\service.kodi.callbacks-master\default.py", line 289, in <module>
                                                test(eventId)
                                              File "C:\Users\HTPC User\AppData\Roaming\Kodi\addons\service.kodi.callbacks-master\default.py", line 247, in test
                                                kwargs = events[evtsettings['type']]['expArgs']
                                            KeyError: ('expArgs',)
                                            -->End of Python script error report<--
20:58:49 T:6228   ERROR: EXCEPTION Thrown (PythonToCppException) : -->Python callback/script returned the following error<--
                                             - NOTE: IGNORING THIS CAN LEAD TO MEMORY LEAKS!
                                            Error Type: <type 'exceptions.IOError'>
                                            Error Contents: [Errno 2] No such file or directory: 'C:\\Users\\HTPC User\\AppData\\Roaming\\Kodi\\addons\\service.kodi.callbacks\\resources\\settings.xml'
                                            Traceback (most recent call last):
                                              File "C:\Users\HTPC User\AppData\Roaming\Kodi\addons\service.kodi.callbacks-master\default.py", line 286, in <module>
                                                generate_settingsxml()
                                              File "C:\Users\HTPC User\AppData\Roaming\Kodi\addons\service.kodi.callbacks-master\resources\lib\xml_gen.py", line 113, in generate_settingsxml
                                                with open(fn, mode='w') as f:
                                            IOError: [Errno 2] No such file or directory: 'C:\\Users\\HTPC User\\AppData\\Roaming\\Kodi\\addons\\service.kodi.callbacks\\resources\\settings.xml'
                                            -->End of Python script error report<--
The first error should be fixed, please download latest version.

The second error occurred because you have not renamed the directory from service.kodi.callbacks-master to service.kodi.callbacks (see http://kodi.wiki/view/Add-on:Kodi_Callbacks#Details)

However, you do not need to regenerate the xml file unless you have created your own custom task class and placed it in the resources.lib.tasks.folder. (see http://kodi.wiki/view/Add-on:Kodi_Callba...ustom_task)

If this is indeed what you have done, then just rename the folder and try again.

If you require an interactive login, then no, this will not work. If you can provide the login information directly such as: 'http://username:[email protected]" that may work. But it's untested.

Thank you. i should probably have looked in the wiki before posting that question Smile

regarding basic authentication found this in the docs of the library 'request' that you use: http://docs.python-requests.org/en/lates...entication

It looks like you have to add an argument to the get method call

Code:
>>> from requests.auth import HTTPBasicAuth
>>> requests.get('https://api.github.com/user', auth=HTTPBasicAuth('user', 'pass'))
<Response [200]>

I would make a pull request but my skills in python are not the best so maybe you can have a look at it? Either you could add a switch in Kodi for authentication and add a username and password field, or simply split the url in case of a @. Anyways, I hope you can find a solution for this since it would help me a lot Smile
Reply
  • 1(current)
  • 2
  • 3
  • 4
  • 5
  • 22

Logout Mark Read Team Forum Stats Members Help
Execute user tasks for Kodi events (callbacks ver2)3