2014-09-10, 20:09
Hi,
I wanted to share an insight I had with everyone, regarding communication with a running script/plugin.
Problem: You have a script that launches a custom UI and stays running until you specifically exit it. At the same time, you need to run callable instances of the script that do not launch a UI, and only make use of "setResolvedURL" so that the Player can get a time-limited URL from a server. The problem is that the instances of the plugin that run to get the current content URL don't have access to the variables or objects that are in the instance of the script that is currently running the GUI.
I recently realized there is a very easy way to make the functions and variables of the main instance of the script available to other instances of the script. There is an HTTP server built into Python, and by running an instance of this server in the main instance of the script, then you have the ability to get and put data to and from the main script instance from any other instance.
An example: I'm building a script with a full UI for a streaming music service. The main script is launched and the GUI stays running until you quit. The music streams that the service uses are time-limited URLs that also require a temporary login token to be able to fetch.
Using the webserver that runs with the main instance of the script, now the plugin instances that run with sole purpose of getting a streaming URL and sending it back to the player, the script instance can hit a URL that my little http server responds to, such as http://localhost:9001/tra.456789, and the main running instance can do the processing I need to have done, and then return the freshly generated content URL to the plugin instance. This lets the plugin code be very small, and it doesn't have to duplicate a bunch of code (such as the API calls themselves, and all the logic to deal with errors, session management, password management, etc) that is already running in the main instance of the script.
It looks like you can roll your own server pretty easily, or use a mini-server framework python module such as 'bottle' (which is what I'm using) to be able to parse the URLs that are sent to the webserver and then run whatever functions and return whatever data you need.
Here's a god little tutorial on how to roll your own (which is really all you need if you only need to process a couple different URLs):
http://www.acmesystems.it/python_httpserver
And here's the 'bottle' python module, which is contained in one file and can be very easily integrated into your script:
http://bottlepy.org/docs/dev/index.html
Basically what you're doing here is making your script have its own little http-based API that can be used by any other program. The possibilities are really endless. You could implement your own http-based remote control for your script, for example. You could implement JSON-RPC-style APIs for your plugin that follow the style of the XBMC ones. You can use it to make your plugin respond to external home automation events.
Anyway, just wanted to share this concept in case people are looking to solve this particular problem. It was a head scratcher for me until I realized how easy it was to make a http server in python.
I wanted to share an insight I had with everyone, regarding communication with a running script/plugin.
Problem: You have a script that launches a custom UI and stays running until you specifically exit it. At the same time, you need to run callable instances of the script that do not launch a UI, and only make use of "setResolvedURL" so that the Player can get a time-limited URL from a server. The problem is that the instances of the plugin that run to get the current content URL don't have access to the variables or objects that are in the instance of the script that is currently running the GUI.
I recently realized there is a very easy way to make the functions and variables of the main instance of the script available to other instances of the script. There is an HTTP server built into Python, and by running an instance of this server in the main instance of the script, then you have the ability to get and put data to and from the main script instance from any other instance.
An example: I'm building a script with a full UI for a streaming music service. The main script is launched and the GUI stays running until you quit. The music streams that the service uses are time-limited URLs that also require a temporary login token to be able to fetch.
Using the webserver that runs with the main instance of the script, now the plugin instances that run with sole purpose of getting a streaming URL and sending it back to the player, the script instance can hit a URL that my little http server responds to, such as http://localhost:9001/tra.456789, and the main running instance can do the processing I need to have done, and then return the freshly generated content URL to the plugin instance. This lets the plugin code be very small, and it doesn't have to duplicate a bunch of code (such as the API calls themselves, and all the logic to deal with errors, session management, password management, etc) that is already running in the main instance of the script.
It looks like you can roll your own server pretty easily, or use a mini-server framework python module such as 'bottle' (which is what I'm using) to be able to parse the URLs that are sent to the webserver and then run whatever functions and return whatever data you need.
Here's a god little tutorial on how to roll your own (which is really all you need if you only need to process a couple different URLs):
http://www.acmesystems.it/python_httpserver
And here's the 'bottle' python module, which is contained in one file and can be very easily integrated into your script:
http://bottlepy.org/docs/dev/index.html
Basically what you're doing here is making your script have its own little http-based API that can be used by any other program. The possibilities are really endless. You could implement your own http-based remote control for your script, for example. You could implement JSON-RPC-style APIs for your plugin that follow the style of the XBMC ones. You can use it to make your plugin respond to external home automation events.
Anyway, just wanted to share this concept in case people are looking to solve this particular problem. It was a head scratcher for me until I realized how easy it was to make a http server in python.