Python Poker_API

For discussion of the Poker Mavens server module and other administration topics
Post Reply
xdev
Posts: 1
Joined: Fri Mar 29, 2013 1:24 am

Python Poker_API

Post by xdev »

Hey guys,

Just wanted to contribute this little snippet. It's the Python equivalent of what Kent has in http://www.briggsoft.com/docs/pmavens/A ... #poker_api for ASP / PHP, except it's written in Python. It has proven very useful in rapidly developing server-side scripts to be used by the site administrator, especially when we're tying in things like custom databases and other types of custom publicly accessible features that interact with the Mavens server one way or another.

Note that if assoc for the Poker_API object is set to True, it will return a dictionary (aka hash table). Otherwise, it will return a list of key=val strings. I believe this is similar behavior to how Brigg's Poker_API scripts handled that.

Here's the code:

Code: Select all

import urllib2

url = "http://192.168.1.100:8087/api"
pw = "my_api_password"

def Poker_API(url=url,params="",assoc=0):
    opener = urllib2.urlopen(url, params, 30)
    response = opener.read()

    if assoc:
        # if assoc is true, return a dictionary
        api = {}
        if response is None or response == "":
            api["Result"] = "Error"
            api["Error"] = "Connection failed"
        else:
            paramlist = response.split('\r\n')
            for param in paramlist:
                keyval = param.split('=')
                api[keyval[0]] = keyval[1]

    else:
        # if assoc is false, return a list
        api = []
        if response is None or response == "":
            api.append("Result=Error")
            api.append("Error=Connection failed")
        else:
            paramlist = response.split('\r\n')
            for param in paramlist:
                api.append(param)

    return api
Here's sample usage from a Python interactive command line interacting with a standard Mavens test install:

Code: Select all

>>> from pmserver_api import Poker_API
>>> test = Poker_API("http://127.0.0.1:8087/api","Password=mymavensapipwd&Command=SystemStats",True)
>>> test
{'UpTime': '0 days  0 hrs  14 mins', 'Logins': '0', 'FilledSeats': '0', 'Result': 'Ok', 'OccupiedTables': '0', 'UpSeconds': '870'}
>>> for key in test.keys():
...     print key+": "+test[key]
...    
UpTime: 0 days  0 hrs  14 mins
Logins: 0
FilledSeats: 0
Result: Ok
OccupiedTables: 0
UpSeconds: 870
Hopes this helps someone!

Keep up the great work K.Briggs!
Post Reply