ERepublik: Fix previous merge.
parent
3e030b965c
commit
2f90ce06bf
|
@ -54,5 +54,8 @@ ERepublik = conf.registerPlugin('ERepublik')
|
||||||
# conf.registerGlobalValue(ERepublik, 'someConfigVariableName',
|
# conf.registerGlobalValue(ERepublik, 'someConfigVariableName',
|
||||||
# registry.Boolean(False, _("""Help for someConfigVariableName.""")))
|
# registry.Boolean(False, _("""Help for someConfigVariableName.""")))
|
||||||
|
|
||||||
|
conf.registerGlobalValue(ERepublik, 'apikey',
|
||||||
|
registry.String('', _("""The API key."""), private=True))
|
||||||
|
|
||||||
|
|
||||||
# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79:
|
# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79:
|
||||||
|
|
|
@ -48,22 +48,6 @@ except:
|
||||||
_ = lambda x:x
|
_ = lambda x:x
|
||||||
internationalizeDocstring = lambda x:x
|
internationalizeDocstring = lambda x:x
|
||||||
|
|
||||||
def getCitizen(irc, name):
|
|
||||||
try:
|
|
||||||
if name.isdigit():
|
|
||||||
base = 'http://api.erpk.org/citizen/profile/%s.json?key=APIKEY'
|
|
||||||
data = json.load(utils.web.getUrlFd(base % name))
|
|
||||||
color = 3 if data['online'] else 4
|
|
||||||
data['name'] = '\x030%i%s\x03' % (color, data['name'])
|
|
||||||
return data
|
|
||||||
else:
|
|
||||||
base = 'http://api.erpk.org/citizen/search/%s/1.json?key=APIKEY'
|
|
||||||
data = json.load(utils.web.getUrlFd(base % name))
|
|
||||||
return getCitizen(irc, str(data[0]['id']))
|
|
||||||
except:
|
|
||||||
raise
|
|
||||||
irc.error(_('This citizen does not exist.'), Raise=True)
|
|
||||||
|
|
||||||
def flatten_subdicts(dicts):
|
def flatten_subdicts(dicts):
|
||||||
"""Change dict of dicts into a dict of strings/integers. Useful for
|
"""Change dict of dicts into a dict of strings/integers. Useful for
|
||||||
using in string formatting."""
|
using in string formatting."""
|
||||||
|
@ -90,11 +74,31 @@ def flatten_subdicts(dicts):
|
||||||
class ERepublik(callbacks.Plugin):
|
class ERepublik(callbacks.Plugin):
|
||||||
threaded = True
|
threaded = True
|
||||||
|
|
||||||
|
def _getCitizen(self, irc, name):
|
||||||
|
key = self.registryValue('apikey')
|
||||||
|
if not key:
|
||||||
|
irc.error(_('No API key set. Ask the owner to add one.'),
|
||||||
|
Raise=True)
|
||||||
|
try:
|
||||||
|
if name.isdigit():
|
||||||
|
base = 'http://api.erpk.org/citizen/profile/%s.json?key=%s'
|
||||||
|
data = json.load(utils.web.getUrlFd(base % (name, key)))
|
||||||
|
color = 3 if data['online'] else 4
|
||||||
|
data['name'] = '\x030%i%s\x0f' % (color, data['name'])
|
||||||
|
return data
|
||||||
|
else:
|
||||||
|
base = 'http://api.erpk.org/citizen/search/%s/1.json?key=%s'
|
||||||
|
data = json.load(utils.web.getUrlFd(base % (name, key)))
|
||||||
|
return self._getCitizen(irc, str(data[0]['id']))
|
||||||
|
except:
|
||||||
|
raise
|
||||||
|
irc.error(_('This citizen does not exist.'), Raise=True)
|
||||||
|
|
||||||
def _advinfo(self, irc, msg, args, format_, name):
|
def _advinfo(self, irc, msg, args, format_, name):
|
||||||
"""<format> <name|id>
|
"""<format> <name|id>
|
||||||
|
|
||||||
Returns informations about a citizen with advanced formating."""
|
Returns informations about a citizen with advanced formating."""
|
||||||
citizen = flatten_subdicts(getCitizen(irc, name))
|
citizen = flatten_subdicts(self._getCitizen(irc, name))
|
||||||
try:
|
try:
|
||||||
repl = lambda x:Template(x).safe_substitute(citizen)
|
repl = lambda x:Template(x).safe_substitute(citizen)
|
||||||
irc.replies(map(repl, format_.split('\\n')))
|
irc.replies(map(repl, format_.split('\\n')))
|
||||||
|
@ -137,7 +141,7 @@ class ERepublik(callbacks.Plugin):
|
||||||
"""<name|id>
|
"""<name|id>
|
||||||
|
|
||||||
Displays the citizen's medals."""
|
Displays the citizen's medals."""
|
||||||
citizen = getCitizen(irc, name)
|
citizen = self._getCitizen(irc, name)
|
||||||
medals = ['%s (%i)' % x for x in citizen['medals'].items() if x[1]]
|
medals = ['%s (%i)' % x for x in citizen['medals'].items() if x[1]]
|
||||||
irc.reply(_('%s has the following medal(s): %s') %
|
irc.reply(_('%s has the following medal(s): %s') %
|
||||||
(name, ', '.join(medals)))
|
(name, ', '.join(medals)))
|
||||||
|
|
Loading…
Reference in New Issue