예제 #1
0
    def test_parse_tibia_datetime_from_datetime(self):
        date = utils.parse_tibiadata_datetime(TIBIADATA_DATETIME_CET)
        self.assertIsInstance(date, datetime.datetime)

        date = utils.parse_tibiadata_datetime(TIBIADATA_DATETIME_CEST)
        self.assertIsInstance(date, datetime.datetime)

        date = utils.parse_tibiadata_datetime(TIBIADATA_DATETIME_PST)
        self.assertIsNone(date)

        # noinspection PyTypeChecker
        # Purposely providing wrong type.
        date = utils.parse_tibiadata_datetime(TIBIA_DATE)
        self.assertIsNone(date)
예제 #2
0
 def _parse_deaths_tibiadata(self, deaths):
     for death in deaths:
         level = death["level"]
         death_time = parse_tibiadata_datetime(death["date"])
         m = death_reason.search(death["reason"])
         _death = Death(self.name, level, time=death_time)
         killers_str = []
         assists_str = []
         involved = [i["name"] for i in death["involved"]]
         if m and m.group("killers"):
             killers_str = [
                 k.strip() for k in split_list(m.group("killers").strip())
             ]
         if m and m.group("assists"):
             assists_str = [
                 a.strip() for a in split_list(m.group("assists").strip())
             ]
         for killer in killers_str:
             summoner = next((i for i in involved if "of %s" % i in killer),
                             None)
             summon = None
             if summoner:
                 summon = killer.replace(" of %s" % summoner, "")
                 killer = summoner
             _death.killers.append(
                 Killer(killer, killer in involved, summon=summon))
         for assist in assists_str:
             _death.assists.append(Killer(assist, assist in involved))
         self.deaths.append(_death)
예제 #3
0
파일: world.py 프로젝트: jpars26/BotDiscord
    def from_tibiadata(cls, content):
        """Parses a TibiaData.com response into a :class:`World`

        Parameters
        ----------
        content: :class:`str`
            The raw JSON content from TibiaData

        Returns
        -------
        :class:`World`
            The World described in the page, or ``None``.

        Raises
        ------
        InvalidContent
            If the provided content is not a TibiaData world response.
        """
        json_data = parse_json(content)
        try:
            world_data = json_data["world"]
            world_info = world_data["world_information"]
            world = cls(world_info["name"])
            if "location" not in world_info:
                return None
            world.online_count = world_info["players_online"]
            world.status = "Online" if world.online_count > 0 else "Offline"
            world.record_count = world_info["online_record"]["players"]
            world.record_date = parse_tibiadata_datetime(
                world_info["online_record"]["date"])
            world.creation_date = world_info["creation_date"]
            world.location = try_enum(WorldLocation, world_info["location"])
            world.pvp_type = try_enum(PvpType, world_info["pvp_type"])
            world.transfer_type = try_enum(TransferType,
                                           world_info.get("transfer_type"),
                                           TransferType.REGULAR)
            world.premium_only = "premium_type" in world_info
            world.world_quest_titles = world_info.get("world_quest_titles", [])
            world._parse_battleye_status(world_info.get("battleye_status", ""))
            world.experimental = world_info.get("Game World Type:",
                                                "Regular") != "Regular"
            for player in world_data.get("players_online", []):
                world.online_players.append(
                    OnlineCharacter(player["name"], world.name,
                                    player["level"], player["vocation"]))
            return world
        except KeyError:
            raise InvalidContent(
                "content is not a world json response from TibiaData")
예제 #4
0
    def from_tibiadata(cls, content):
        """Builds a character object from a TibiaData character response.

        Parameters
        ----------
        content: :class:`str`
            The JSON content of the response.

        Returns
        -------
        :class:`Character`
            The character contained in the page, or None if the character doesn't exist

        Raises
        ------
        InvalidContent
            If content is not a JSON string of the Character response."""
        json_content = parse_json(content)
        char = cls()
        try:
            character = json_content["characters"]
            if "error" in character:
                return None
            character_data = character["data"]
            char.name = character_data["name"]
            char.world = character_data["world"]
            char.level = character_data["level"]
            char.achievement_points = character_data["achievement_points"]
            char.sex = try_enum(Sex, character_data["sex"])
            char.vocation = try_enum(Vocation, character_data["vocation"])
            char.residence = character_data["residence"]
            char.account_status = try_enum(AccountStatus,
                                           character_data["account_status"])
        except KeyError:
            raise InvalidContent(
                "content does not match a character json from TibiaData.")
        char.former_names = character_data.get("former_names", [])
        if "deleted" in character_data:
            char.deletion_date = parse_tibiadata_datetime(
                character_data["deleted"])
        char.married_to = character_data.get("married_to")
        char.former_world = character_data.get("former_world")
        char.position = character_data.get("Position:")
        if "guild" in character_data:
            char.guild_membership = GuildMembership(
                character_data["guild"]["name"],
                character_data["guild"]["rank"])
        if "house" in character_data:
            house = character_data["house"]
            paid_until_date = parse_tibiadata_date(house["paid"])
            char.houses.append(
                CharacterHouse(house["houseid"], house["name"], char.world,
                               house["town"], char.name, paid_until_date))
        char.comment = character_data.get("comment")
        if len(character_data["last_login"]) > 0:
            char.last_login = parse_tibiadata_datetime(
                character_data["last_login"][0])
        for achievement in character["achievements"]:
            char.achievements.append(
                Achievement(achievement["name"], achievement["stars"]))

        char._parse_deaths_tibiadata(character.get("deaths", []))

        for other_char in character["other_characters"]:
            char.other_characters.append(
                OtherCharacter(other_char["name"], other_char["world"],
                               other_char["status"] == "online",
                               other_char["status"] == "deleted"))

        if character["account_information"]:
            acc_info = character["account_information"]
            created = parse_tibiadata_datetime(acc_info.get("created"))
            loyalty_title = None if acc_info[
                "loyalty_title"] == "(no title)" else acc_info["loyalty_title"]
            position = acc_info.get("position")

            char.account_information = AccountInformation(
                created, loyalty_title, position)

        return char