All pastes #1637658 Raw Edit

eRepublik.py

public python v1 · immutable
#1637658 ·published 2009-10-22 16:35 UTC
rendered paste body
##  Copyright (C) 2009 by Filip Brcic <brcha@gna.org>##  eRepublik eAPI python access library##  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 libxml2from math import ceilclass Citizen:    def loadByName(self, name):        try:            self.doc = libxml2.parseFile('http://api.erepublik.com/v1/feeds/citizens/' +                                         name +                                         '?by_username=true')        except:            return None        self.ctxt = self.doc.xpathNewContext()        return self    def loadById(self, id):        try:            self.doc = libxml2.parseFile('http://api.erepublik.com/v1/feeds/citizens/' + str(id))        except:            return None        self.ctxt = self.doc.xpathNewContext()        return self    def getName(self):        return self.ctxt.xpathEval('//citizen/name/text()')[0].getContent()    def getId(self):        return int(self.ctxt.xpathEval('//citizen/id/text()')[0].getContent())    def getDamage(self):        return int(self.ctxt.xpathEval('//citizen/damage/text()')[0].getContent())    def getStrength(self):        return float(self.ctxt.xpathEval('//citizen/strength/text()')[0].getContent())    def getRank(self):        try:            return self.ctxt.xpathEval('//citizen/military-rank/text()')[0].getContent()        except:            return "Private"    def getRankAsNumber(self):        rank = self.getRank().lower()        if rank == 'private':            return 1        elif rank == 'corporal':            return 2        elif rank == 'sergeant':            return 3        elif rank == 'lieutenant':            return 4        elif rank == 'captain':            return 5        elif rank == 'colonel':            return 6        elif rank == 'general':            return 7        else: # if rank == 'field marshall'            return 8        def getFights(self):        return int(self.ctxt.xpathEval('//citizen/fights/text()')[0].getContent())    def getWellness(self):        return float(self.ctxt.xpathEval('//citizen/wellness/text()')[0].getContent())    def getExperience(self):        return int(self.ctxt.xpathEval('//citizen/experience-points/text()')[0].getContent())    def getLevel(self):        return int(self.ctxt.xpathEval('//citizen/level/text()')[0].getContent())    def getCitizenship(self):        return self.ctxt.xpathEval('//citizen/citizenship/country/text()')[0].getContent()    def getCitizenshipId(self):        return int(self.ctxt.xpathEval('//citizen/citizenship/c-id/text()')[0].getContent())    def getEmployer(self):        expr = self.ctxt.xpathEval('//citizen/employer/text()')        if len(expr) == 0:            return 'N/A'        else:            return expr[0].getContent()    def getEmployerId(self):        return int(self.ctxt.xpathEval('//citizen/employer-id/text()')[0].getContent())    def isUnemployed(self):        return len(self.ctxt.xpathEval('//citizen/employer-id/text()')) == 0    def getManufacturing(self):        expr = self.ctxt.xpathEval('//citizen/skills/skill[domain="manufacturing"]/value/text()')        if len(expr) == 0:            return 0.0        return float(expr[0].getContent())    def getLand(self):        expr = self.ctxt.xpathEval('//citizen/skills/skill[domain="land"]/value/text()')        if len(expr) == 0:            return 0.0        return float(expr[0].getContent())    def getConstructions(self):        expr = self.ctxt.xpathEval('//citizen/skills/skill[domain="constructions"]/value/text()')        if len(expr) == 0:            return 0.0        return float(expr[0].getContent())    def getRegion(self):        return self.ctxt.xpathEval('//citizen/region/text()')[0].getContent()    def getRegionId(self):        return int(self.ctxt.xpathEval('//citizen/region-id/text()')[0].getContent())    def getCountry(self):        return self.ctxt.xpathEval('//citizen/country/text()')[0].getContent()    def getCountryId(self):        return int(self.ctxt.xpathEval('//citizen/country-id/text()')[0].getContent())    def toString(self):        return (            self.getName() + ': Location: ' +            self.getCountry() + ', ' + self.getRegion() +            '; Skills: manu=' + str(self.getManufacturing()) +            ', land=' + str(self.getLand()) +            ', cons=' + str(self.getConstructions()) +            '; Strength: ' + str(self.getStrength()) +            '; Rank: ' + self.getRank() +            ' (' + str(self.getDamage()) + ' dmg, ' +             str(self.getFights()) + ' fights)' +            '; Wellness: ' + str(self.getWellness()) +            '; Level: ' + str(self.getLevel()) +            ' (' + str(self.getExperience()) + ')' +            '; Employed at: ' + self.getEmployer() +            '; Citizenship: ' + self.getCitizenship()            )    def fightCalc(self, wellness):        dmg = (1 + self.getRankAsNumber() / 5.0) * self.getStrength() * 2        dmg = dmg * (1 + (wellness - 25)/100.0)        return map(lambda x: int(ceil(dmg * x)), (0.5, 1.2, 1.4, 1.6, 1.8, 2.0))    def fightCalcStr(self, wellness):        fc = self.fightCalc(wellness)        res = ''        for i in range(0,6):            res += 'Q%s: %s, ' % (str(i), str(fc[i]))        return res[:-2]class Region:    def __init__(self, id):        self.doc = libxml2.parseFile('http://api.erepublik.com/v1/feeds/regions/' + str(id))        self.ctxt = self.doc.xpathNewContext()    def getName(self):        return self.ctxt.xpathEval('//region/name/text()')[0].getContent()    def getId(self):        return int(self.ctxt.xpathEval('//region/id/text()')[0].getContent())    def getCitizens(self):        ret = []        for a in self.ctxt.xpathEval('//region/citizens/citizen/id/text()'):            ret = ret + [int(a.getContent())]        return ret