Created
October 27, 2016 19:58
-
-
Save s4w3d0ff/330cb72328d1a14b2767846a66fd7e07 to your computer and use it in GitHub Desktop.
Simple bf4stats.com api wrapper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin python | |
import requests | |
commands = ['playerInfo', 'playerRankings', 'onlinePlayers'] | |
platforms = ['pc','xbox','ps3','xone','ps4'] | |
posout = ['json', 'jsonp', 'js', 'lines'] | |
playeroptions = [ | |
'imagePaths', | |
'details', | |
'names', | |
'extra', | |
'stats', | |
'weapons', | |
'weaponUnlocks', | |
'weaponCategory', | |
'vehicles', | |
'vehicleCategory', | |
'vehicleUnlocks', | |
'kititems', | |
'awards', | |
'dogtags', | |
'assignments', | |
'upcomingUnlocks', | |
'urls' | |
] | |
def get(command, name=False, platform=False, options=False, out=False): | |
if not command in commands: | |
raise ValueError('Invalid command!: %s' % command) | |
if command == 'onlinePlayers': | |
if out in posout: | |
r = requests.post( | |
'http://api.bf4stats.com/api/'+command, params={'out': out} | |
) | |
else: | |
r = requests.post('http://api.bf4stats.com/api/'+command) | |
print(r.url) | |
return r.text | |
if not name: | |
raise ValueError('A name is required for "%s"' % command) | |
if not platform or platform not in platforms: | |
raise ValueError('Invalid platform!: %s' % str(platform)) | |
payload = {'plat': platform, 'name': name} | |
if command == 'playerInfo' and options: | |
if not isinstance(options, list): | |
raise ValueError('PlayerInfo options must be a list') | |
payload['opt'] = options | |
if out in posout: | |
payload['out'] = out | |
r = requests.post('http://api.bf4stats.com/api/'+command, params=payload) | |
print(r.url) | |
return r.text | |
if __name__ == '__main__': | |
import argparse | |
parser = argparse.ArgumentParser(description='BF4stats.com api wrapper!') | |
parser.add_argument( | |
'command', | |
choices=commands, | |
) | |
parser.add_argument( | |
'-n', | |
'--name', | |
nargs='?', | |
const=False, | |
default=False, | |
help='name of bf4 account') | |
parser.add_argument( | |
'-p', | |
'--platform', | |
nargs='?', | |
const=False, | |
default=False, | |
choices=platforms, | |
help='platform to query') | |
parser.add_argument( | |
'-o', | |
'--options', | |
nargs='+', | |
default=False, | |
help=str(playeroptions).strip('[').strip(']')) | |
parser.add_argument( | |
'-out', | |
'--out', | |
nargs='?', | |
const=False, | |
default=False, | |
choices=posout, | |
help='output format (not working... only json)') | |
args = parser.parse_args() | |
print(args) | |
print(get(args.command, args.name, args.platform, args.options, args.out)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment