Basic python alias helper script
#1
Copy and paste the below I am using it for my needs but it is easily customizable. Thank you all as I have trolled around these forums to gather some of this information

It could use cleaned up and if people find it useful I will release a future version with some better error handling and documentation. I am also casually adding more alias's as I need.

Feel free to update, recycle and reuse however you see fit.

Output of help:
ALIAS'S AVAILABLE:
totalunwatched
setcurrentplaylist
totalrecords
totalwatched
clearplaylist
gotomusic
getcurrentlyplaying
togglewatched
recentlyseen
cleanlibrary
updatevideolibrary
playfile
addtoplaylist
recentlyadded
getplaylistcontents

Simply run script eg scriptname.py gotomusic

You may run any command that the httpapi supports as a parameter eg "command=ExecBuiltIn&parameter=XBMC.UpdateLibrary(video)"
Not that the " and " are very important

Code:
#!/usr/bin/env python
# written by eric soukenka
# contact [email protected] for any questions
import sys,pycurl,re
from cStringIO import StringIO

class scraper(object):
    def __init__(self):
        self.stringbuf = StringIO()
        self.c = pycurl.Curl()
        self.c.setopt(pycurl.VERBOSE, 0) # show request info
        self.c.setopt(pycurl.COOKIEFILE, '') # enable automatic cookie handling
        self.c.setopt(pycurl.ENCODING, 'gzip,deflate')
        self.c.setopt(pycurl.USERAGENT, USER_AGENT)
        self.c.setopt(pycurl.CONNECTTIMEOUT, 3)
        self.c.setopt(pycurl.TIMEOUT, 5)


    def __curl_callback(self, buf):
        self.stringbuf.write(buf)

    def gethtml(self, url):
        self.c.setopt(pycurl.URL , url)
        self.c.setopt(pycurl.WRITEFUNCTION, self.__curl_callback)
        self.c.setopt(pycurl.HEADERFUNCTION, self.__curl_callback)
        self.c.perform()
        self.c.close
        return self.stringbuf.getvalue()

def cleanOutput(data):
    insideHTML=data.split('<html>');
    insideHTML=insideHTML[1].split('</html>');
    output=insideHTML[0];

    p = re.compile(r'\s+')
    output=p.sub(' ', output)

    p = re.compile(r'<record>')
    output=p.sub('\n',output)

    p = re.compile(r'<.*?>')
    return p.sub('', output)

def detectError(data):
    p = re.compile(r'Error:')
    if(p.search(data)): return "Error"

def handle_default():
    return sys.argv[1]


#Modify this freely remember to add , at end of line if not last line
alias = {
    'gotomusic': lambda: 'command=ExecBuiltIn&parameter=XBMC.ActivateWindow(10005)',
    'getcurrentlyplaying': lambda: 'command=getcurrentlyplaying',
    'togglewatched': lambda: 'command=Action(200)',
    'updatevideolibrary': lambda: 'command=ExecBuiltIn&parameter=XBMC.UpdateLibrary(video)',
    'clearplaylist': lambda: 'command=ClearPlayList(1)',
    'setcurrentplaylist': lambda: 'command=SetCurrentPlaylist(1)',
    'getplaylistcontents': lambda: 'command=GetPlaylistContents(1)',
    'addtoplaylist': lambda: 'command=AddToPlayList('+sys.argv[2]+')',
    'cleanlibrary': lambda: 'command=ExecBuiltIn&parameter=CleanLibrary',
    #'recentlyadded': lambda: 'command=queryvideodatabase(select%20path.strpath%20from%20path)',
    'recentlyadded': lambda: 'command=queryvideodatabase(SELECT%20p.strPath,f.strFilename%20FROM%20path%20p,%20files%20f%20WHERE%20p.idPath%20=%20f.idPath%20ORDER%20BY%20f.idFile%20DESC%20limit%20'+sys.argv[2]+';)',
    'recentlyseen': lambda: 'command=queryvideodatabase(SELECT%20strPath,strFilename%20FROM%20path,files%20WHERE%20path.idPath%20=%20files.idPath%20AND%20playCount%20NOT%20NULL%20ORDER%20BY%20lastPlayed%20DESC%20limit%20'+sys.argv[2]+';)',
    'totalwatched': lambda: 'command=queryvideodatabase(SELECT%20count(strPath)%20FROM%20path,files%20WHERE%20path.idPath%20=%20files.idPath%20AND%20playCount%20NOT%20NULL;)',
    'totalunwatched': lambda: 'command=queryvideodatabase(SELECT%20count(strPath)%20FROM%20path,files%20WHERE%20path.idPath%20=%20files.idPath%20AND%20playCount%20IS%20NULL;)',
    'totalrecords': lambda: 'command=queryvideodatabase(SELECT%20count(p.strPath)%20FROM%20path%20p,files%20f%20WHERE%20p.idPath=f.idPath;)',
    'playfile': lambda: 'command=PlayFile('+sys.argv[2]+')'
}
if(sys.argv[1]=='-h' or sys.argv[1]=='--help'):
    print "ALIAS'S AVAILABLE:";
    for val in alias:
        print "     "+val;
    print "";
    print "Simply run script eg scriptname.py gotomusic";
    print "";
    print "You may run any command that the httpapi supports as a parameter eg \"command=ExecBuiltIn&parameter=XBMC.UpdateLibrary(video)\""
    print "Not that the \" and \" are very important";
    print "--------------------------------------------------------";
    sys.exit();
parameters = alias.get(sys.argv[1], handle_default)

USER_AGENT = 'Mozilla/5.0 (X11; U; Linux i686; en-GB; rv:1.9.0.5) Gecko/2008121622 Ubuntu/8.10 (intrepid) Firefox/3.0.5'

s = scraper()

#change host name if you need
url='http://localhost:8080/xbmcCmds/xbmcHttp?';

try:
    sys.stderr = output = s.gethtml(url+parameters());
except pycurl.error,err:
    print "XBMC was not found"
    sys.exit()

    
print cleanOutput(output).strip();
Reply
#2
There are long lists with all commands on the wiki Smile
Reply
#3
blittan Wrote:There are long lists with all commands on the wiki Smile

I know that it what I have used for what I got I just have not added them all. I do also have some useful SQL queries and things like that which just make running easier.

Output is also stripped of html which can make it look better when running from command line. Not to mention easier then curl %20\(function\) and other issues that arose when typing long way.

Its only intended to make things easier eg
xbmccontrol.py recentlyseen 5

does just that Smile
Reply
#4
thanks! love it! Playfile is like Airplay for XBMC.

Bugfix: HTML Encoding of the URL
Bugfix: Error when starting without parameter

Code:
#!/usr/bin/env python
# written by eric soukenka
# contact [email protected] for any questions
import sys,pycurl,re
from cStringIO import StringIO
import urllib

class scraper(object):
    def __init__(self):
        self.stringbuf = StringIO()
        self.c = pycurl.Curl()
        self.c.setopt(pycurl.VERBOSE, 0) # show request info
        self.c.setopt(pycurl.COOKIEFILE, '') # enable automatic cookie handling
        self.c.setopt(pycurl.ENCODING, 'gzip,deflate')
        self.c.setopt(pycurl.USERAGENT, USER_AGENT)
        self.c.setopt(pycurl.CONNECTTIMEOUT, 3)
        self.c.setopt(pycurl.TIMEOUT, 5)


    def __curl_callback(self, buf):
        self.stringbuf.write(buf)

    def gethtml(self, url):
        self.c.setopt(pycurl.URL , url)
        self.c.setopt(pycurl.WRITEFUNCTION, self.__curl_callback)
        self.c.setopt(pycurl.HEADERFUNCTION, self.__curl_callback)
        self.c.perform()
        self.c.close
        return self.stringbuf.getvalue()

def cleanOutput(data):
    insideHTML=data.split('<html>');
    insideHTML=insideHTML[1].split('</html>');
    output=insideHTML[0];

    p = re.compile(r'\s+')
    output=p.sub(' ', output)

    p = re.compile(r'<record>')
    output=p.sub('\n',output)

    p = re.compile(r'<.*?>')
    return p.sub('', output)

def detectError(data):
    p = re.compile(r'Error:')
    if(p.search(data)): return "Error"

def handle_default():
    return sys.argv[1]

#Modify this freely remember to add , at end of line if not last line
alias = {
    'gotomusic': lambda: 'command=ExecBuiltIn&parameter=XBMC.ActivateWindow(10005)',
    'getcurrentlyplaying': lambda: 'command=getcurrentlyplaying',
    'togglewatched': lambda: 'command=Action(200)',
    'updatevideolibrary': lambda: 'command=ExecBuiltIn&parameter=XBMC.UpdateLibrary(video)',
    'clearplaylist': lambda: 'command=ClearPlayList(1)',
    'setcurrentplaylist': lambda: 'command=SetCurrentPlaylist(1)',
    'getplaylistcontents': lambda: 'command=GetPlaylistContents(1)',
    'addtoplaylist': lambda: 'command=AddToPlayList('+sys.argv[2]+')',
    'cleanlibrary': lambda: 'command=ExecBuiltIn&parameter=CleanLibrary',
    #'recentlyadded': lambda: 'command=queryvideodatabase(select%20path.strpath%20from%20path)',
    'recentlyadded': lambda: 'command=queryvideodatabase(SELECT%20p.strPath,f.strFilename%20FROM%20path%20p,%20files%20f%20WHERE%20p.idPath%20=%20f.idPath%20ORDER%20BY%20f.idFile%20DESC%20limit%20'+sys.argv[2]+';)',
    'recentlyseen': lambda: 'command=queryvideodatabase(SELECT%20strPath,strFilename%20FROM%20path,files%20WHERE%20path.idPath%20=%20files.idPath%20AND%20playCount%20NOT%20NULL%20ORDER%20BY%20lastPlayed%20DESC%20limit%20'+sys.argv[2]+';)',
    'totalwatched': lambda: 'command=queryvideodatabase(SELECT%20count(strPath)%20FROM%20path,files%20WHERE%20path.idPath%20=%20files.idPath%20AND%20playCount%20NOT%20NULL;)',
    'totalunwatched': lambda: 'command=queryvideodatabase(SELECT%20count(strPath)%20FROM%20path,files%20WHERE%20path.idPath%20=%20files.idPath%20AND%20playCount%20IS%20NULL;)',
    'totalrecords': lambda: 'command=queryvideodatabase(SELECT%20count(p.strPath)%20FROM%20path%20p,files%20f%20WHERE%20p.idPath=f.idPath;)',
    'playfile': lambda: 'command=PlayFile('+urllib.quote(sys.argv[2])+')'
}
if(len(sys.argv) == 1):
    print "ALIAS'S AVAILABLE:";
    for val in alias:
        print "     "+val;
    print "";
    print "Simply run script eg scriptname.py gotomusic";
    print "";
    print "You may run any command that the httpapi supports as a parameter eg \"command=ExecBuiltIn&parameter=XBMC.UpdateLibrary(video)\""
    print "Not that the \" and \" are very important";
    print "--------------------------------------------------------";
    sys.exit();
parameters = alias.get(sys.argv[1], handle_default)

USER_AGENT = 'Mozilla/5.0 (X11; U; Linux i686; en-GB; rv:1.9.0.5) Gecko/2008121622 Ubuntu/8.10 (intrepid) Firefox/3.0.5'

s = scraper()

#change host name if you need
url='http://localhost:8080/xbmcCmds/xbmcHttp?';

try:
    sys.stderr = output = s.gethtml(url+parameters());
except pycurl.error,err:
    print "XBMC was not found"
    sys.exit()

print cleanOutput(output).strip();
Reply

Logout Mark Read Team Forum Stats Members Help
Basic python alias helper script0