def get_character_details(api_token, character_name): payload = build_request(url_join("characters", quote(character_name)), api_token=api_token) name = payload["name"] deaths = payload["deaths"] age = payload["age"] avg_life_span = age / deaths level = payload["level"] gender = payload["gender"] race = payload["race"] profession = payload["profession"] equipment_details = get_equipment_details(payload["equipment"]) res = [] res.append("%s (%s):" % (name, level)) res.append(" Level %s %s %s" % (gender, race, profession)) res.append(" Average life-span: %ss" % avg_life_span) res.append(" Equipment:") for id, equip in equipment_details.iteritems(): slot = equip["slot"] name = equip.get("name", id) res.append(" %s: %s" % (slot, name)) return "\n".join(res)
def get_character_details(api_token, character_name): payload = build_request(url_join("characters", character_name.replace(" ", "%20")), api_token=api_token) name = payload["name"] deaths = payload["deaths"] age = payload["age"] avg_life_span = age / deaths level = payload["level"] gender = payload["gender"] race = payload["race"] profession = payload["profession"] equipment = payload["equipment"] equipment_details = get_item_details([node["id"] for node in equipment]) res = [] res.append("%s (%s):" % (name, level)) res.append(" Level %s %s %s" % (gender, race, profession)) res.append(" Average life-span: %ss" % avg_life_span) res.append(" Equipment:") for equip in equipment: id = equip["id"] slot = equip["slot"] name = equipment_details.get(id) if equipment_details.has_key(id): res.append(" %s: %s" % (slot, name)) else: res.append(" %s: %s" % (slot, id)) return "\n".join(res)
def build_request(endpoint, parameters=None, api_token=""): if parameters is None: parameters = {} request_url = url_join(API_URL, endpoint) if api_token: parameters["access_token"] = api_token if parameters: request_url += "?" + urlencode(parameters) resp = urlopen(request_url) return loads(resp.read())