Skip to content

Instantly share code, notes, and snippets.

@shizeeg
Created May 12, 2012 01:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save shizeeg/2663574 to your computer and use it in GitHub Desktop.
Save shizeeg/2663574 to your computer and use it in GitHub Desktop.
Godville.net player stats viewer.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Godville player stats viewer. For more info, see <http://godville.net/>
Copyright (C) 2012 sh!zeeg <shizeeg@ya.ru>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import sys
# set default encoding
if sys.version_info < (3, 0):
reload(sys)
sys.setdefaultencoding('utf-8')
# try simplejson first, then json from Python 2.6+
try: import simplejson as json
except ImportError:
try: import json; json.dumps
except (ImportError, AttributeError):
sys.stderr.write("Please install simplejson or Python 2.6 or higher.")
sys.exit(2)
# trying to play cross-version way.
try:
from urllib import request, error
from urllib.error import HTTPError
except ImportError:
import urllib2 as request
from urllib2 import HTTPError
# FIXME: change to your nickname!
player = 'Shizeeg Unadequatov'
if len(sys.argv) > 1:
player = ' '.join(sys.argv[1:])
print('Request stats for player: "%s"\n' % player)
try:
resp = request.urlopen('http://godville.net/gods/api/%s.json' % player)
except HTTPError:
sys.stderr.write("%s\n" % sys.exc_info()[1])
sys.exit(1)
json_data = json.loads(resp.read().decode('utf-8'))
# uncomment next line to print JSON dump. (DEBUG)
#print(json.dumps(json_data, indent=4))
for i in sorted(json_data.keys()):
d = json_data[i]
if type(d) == dict:
print('%s (%d):' % (i, len(d)))
for j in d:
print(' %s: %s "%s"' % (j, ' ' * (25 - len(j)), d[j]))
else:
print('%s: %s "%s"' % (i, ' ' * (29 - len(i)), d))
# vim: ts=4:st=4:et:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment