Skip to content

Instantly share code, notes, and snippets.

@joealcorn
Last active October 8, 2015 14:18
Show Gist options
  • Save joealcorn/3344408 to your computer and use it in GitHub Desktop.
Save joealcorn/3344408 to your computer and use it in GitHub Desktop.
wrapper for MCBouncer API
import requests
try:
import simplejson as json
except ImportError:
import json
class MCBouncer(object):
"""
MCBouncer object allowing you to interact with the API.
Example usage: mcb = MCBouncer(api_key)
"""
def __init__(self, api_key):
self.api_key = api_key
def sendRequest(self, endpoint, *args):
url = 'http://mcbouncer.com/api/{0}/{1}/{2}'.format(
endpoint, self.api_key, '/'.join(str(a) for a in args))
try:
r = requests.get(url, timeout=5)
except requests.exceptions.Timeout:
return {u'success': False, u'error': u'timeout'}
except requests.exceptions.ConnectionError:
return {u'success': False, u'error': u'connection error'}
else:
return json.loads(r.text)
def addBan(self, admin, player, reason='Rule violation.'):
"""
Ban a player.
Example usage: mcb.addban(admin, player, reason)
"""
return self.sendRequest('addBan', admin, player, reason)
def removeBan(self, player):
"""
Unban a player.
Example usage: mcb.removeBan(player)
"""
return self.sendRequest('removeBan', player)
def addIPBan(self, admin, ip, reason='Rule violation.'):
"""
Ban an IP.
Example usage: mcb.addIPBan(admin, ip, reason)
"""
return self.sendRequest('addIPBan', admin, ip, reason)
def removeIPBan(self, ip):
"""
Unban an IP.
Example usage: mcb.removeIPBan(ip)
"""
return self.sendRequest('removeIPBan', ip)
def addNote(self, admin, player, note):
"""
Add a note to a player.
Example usage: mcb.addnote(admin, player, note)
"""
return self.sendRequest('addNote', admin, player, note)
def removeNote(self, note_id):
"""
Remove a note from a player.
Example usage: mcb.removeNote(note_id)
"""
return self.sendRequest('removeNote', note_id)
def getBans(self, player, page=0, per_page=10):
"""
Get a paginated list of bans for a player.
Example usage: mcb.getBans(player, page, per_page)
"""
return self.sendRequest('getBans', player, page, per_page)
def getIPBans(self, ip, page=0, per_page=10):
"""
Get a paginated list of bans for an IP.
Example usage: mcb.getIPBans(ip, page, per_page)
"""
return self.sendRequest('getIPBans', ip, page, per_page)
def getNotes(self, player, page=0, per_page=10):
"""
Get a paginated list of notes for a player.
Example usage: mcb.getNotes(player, page, per_page)
"""
return self.sendRequest('getNotes', player, page, per_page)
def getBanCount(self, player):
"""
Get the number of bans for a player.
Example usage: mcb.getBanCount(player)
"""
return self.sendRequest('getBanCount', player)
def getIPBanCount(self, ip):
"""
Get the number of bans for an IP.
Example usage: mcb.getIPBanCount(ip)
"""
return self.sendRequest('getIPBanCount', ip)
def getNoteCount(self, player):
"""
Get the number of notes for a player.
Example usage: mcb.getNoteCount(player)
"""
return self.sendRequest('getNoteCount', player)
def getBanReason(self, player):
"""
Gets the reason a player is banned from your server
Example usage: mcb.getBanReason(player)
"""
return self.sendRequest('getBanReason', player)
def getBanDetails(self, player):
"""
Gets the details of a ban on your server
Example usage: mcb.getBanDetails(player)
"""
return self.sendRequest('getBanDetails', player)
# def updateUser(self, player, ip_address):
# ''' updateUser(player, ip_address) '''
# return self.sendRequest('updateUser', player, ip_address)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment