Author Topic: Python & VoxCommando Season Call Function  (Read 2645 times)

0 Members and 1 Guest are viewing this topic.

Mace

  • $upporter
  • Contributor
  • *****
  • Posts: 77
  • Karma: 1
    • View Profile
Python & VoxCommando Season Call Function
« on: May 21, 2013, 07:33:52 AM »
A work around for selecting seasons. While this is achievable with the current commands, this work around lets you call a season without saying the full phrase again.

Step 1:
We will need a python file with this code: (Notepad++ works well)
Code: [Select]
def tvseason(com = dict):
    if com.has_key("Container.FolderPath"):
        return com.get("Container.FolderPath")

Save this file as seasoncall.py in the PY folder in your VoXCommando directory. (remember the .py on the end.

Step2:
Then use this command to load the python file at startup:

Code: [Select]
<command id="1436" name="load python startup code" enabled="true" alwaysOn="False" confirm="False" requiredConfidence="0" loop="False" loopDelay="0" loopMax="0" description="">
  <action>
    <cmdType>PY.ExecFile</cmdType>
    <cmdString>PY\seasoncall.py</cmdString>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <phrase>load python startup code, load python startup script</phrase>
  <event>python ready</event>
</command>

Change seasoncall.py to what ever your python script is called, but once again remember the .py on the end.

The basics of the script is turn the JSON query into a dictionary (fortunately JSON return format from query's is already set as python dictionary. Then if the key is present in the query return the value for the key.

Step3:
You'll then need the commands to run the script:

Code: [Select]
<command id="1501" name="Season Call" enabled="true" alwaysOn="False" confirm="False" requiredConfidence="0" loop="False" loopDelay="0" loopMax="0" description="">
  <action>
    <cmdType>XJson.Raw</cmdType>
    <cmdString>XBMC.GetInfoLabels&amp;&amp;"labels" : ["Container.FolderPath"]</cmdString>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <action>
    <cmdType>PY.ExecString</cmdType>
    <cmdString>result=tvfilter({LastResult})</cmdString>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <action>
    <cmdType>XJson.Raw</cmdType>
    <cmdString>GUI.ActivateWindow&amp;&amp;"window": "video", "parameters": [ "{LastResult}{1}/" ]</cmdString>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <phrase>show me season</phrase>
  <payloadRange>1,10</payloadRange>
</command>

Should look like this:


Keep in mind you must be looking at the season window for a tvshow in XBMC for this to work.

Something like this:


While this is a messy work around is does give everyone a bit of an idea what is possible with the python plugin.
The python script can be adjusted for pretty much any JSON query which is cool.

Script attached at the bottom. Feel free to download and copy to the PY folder.
 
« Last Edit: May 21, 2013, 09:47:46 AM by Mace »

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7714
  • Karma: 116
    • View Profile
    • VoxCommando
Re: Python & VoxCommando Season Call Function
« Reply #1 on: May 21, 2013, 08:55:10 AM »
Thanks Mace.  This is very cool.  In particular, the fact that the JSON response can be passed to a python variable and then treated like a Dict is very enlightening!

For completeness, I should mention that we don't actually need python to accomplish this task.  However, it is educational and I'm sure it could be a starting point for doing more complex and interesting things, so there is no doubt that it is very useful.

Instead of using the python method to extract the path from the JSON string, we can use the action XJson.ParseTokens

Code: [Select]
<action actiontype="XJson.ParseTokens" repeat="1" logic="False"><paramstring>{[0]}</paramstring></action>
so the complete command will look like this:
Code: [Select]
<?xml version="1.0" encoding="utf-16"?>
<command id="1501" name="Season Call No Python" enabled="true" alwaysOn="False" confirm="False" requiredConfidence="0" loop="False" loopDelay="0" loopMax="0" description="">
  <action>
    <cmdType>XJson.Raw</cmdType>
    <cmdString>XBMC.GetInfoLabels&amp;&amp;"labels" : ["Container.FolderPath"]</cmdString>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <action>
    <cmdType>XJson.ParseTokens</cmdType>
    <cmdString>{[0]}</cmdString>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <action>
    <cmdType>XJson.Raw</cmdType>
    <cmdString>GUI.ActivateWindow&amp;&amp;"window": "video", "parameters": [ "{LastResult}{1}/" ]</cmdString>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <phrase>show me season</phrase>
  <payloadRange>1,10</payloadRange>
</command>

Both methods suffer from the fact that we have basically no error checking.  If you are not in the correct place to begin with, the command won't work, and it may take you to a weird place... basically to a path that does not exist.  It makes me realise that the parsetokens action should probably be throwing an error if the token can't be found.

By the way, the param I am using in the parsetoken action is just
Code: [Select]
{[0]}which basically means return the 0th item.  -- In case you don't speak computer, 0th really means 1st!  :biglaugh
« Last Edit: May 21, 2013, 09:00:11 AM by jitterjames »

Mace

  • $upporter
  • Contributor
  • *****
  • Posts: 77
  • Karma: 1
    • View Profile
Re: Python & VoxCommando Season Call Function
« Reply #2 on: May 21, 2013, 10:06:15 AM »
wow XJson.ParseTokens just went straight over my head.
Some sleep might help.