def test_character_from_tibiadata_deleted(self): """Testing parsing a deleted character""" content = self._load_resource(FILE_CHARACTER_TIBIADATA_DELETED) char = Character.from_tibiadata(content) self.assertEqual(char.url_tibiadata, Character.get_url_tibiadata(char.name)) self.assertIsInstance(char, Character) self.assertTrue(char.deleted) self.assertIsInstance(char.deletion_date, datetime.datetime) self.assertIsNone(char.guild_name) self.assertIsNone(char.last_login)
async def get_character(bot, name, *, tries=5) -> Optional[NabChar]: """Fetches a character from TibiaData, parses and returns a Character object The character object contains all the information available on Tibia.com Information from the user's database is also added, like owner and highscores. If the character can't be fetch due to a network error, an NetworkError exception is raised If the character doesn't exist, None is returned. """ if tries == 0: raise errors.NetworkError(f"get_character({name})") try: url = Character.get_url_tibiadata(name) except UnicodeEncodeError: return None if invalid_name.search(name): return None # Fetch website try: character = CACHE_CHARACTERS[name.lower()] except KeyError: try: async with bot.session.get(url) as resp: content = await resp.text(encoding='ISO-8859-1') character = NabChar.from_tibiadata(content) except (aiohttp.ClientError, asyncio.TimeoutError, tibiapy.TibiapyException): await asyncio.sleep(config.network_retry_delay) return await get_character(bot, name, tries=tries - 1) CACHE_CHARACTERS[name.lower()] = character if character is None: return None if character.house: house_id = get_house_id(character.house.name) if house_id: character.house.id = house_id # If the character exists in the online list use data from there where possible try: for c in online_characters[character.world]: if c == character: character.level = c.level character.vocation = c.vocation break except KeyError: pass await bind_database_character(bot, character) return character
async def get_character(bot, name, *, tries=5) -> Optional[NabChar]: """Fetches a character from TibiaData, parses and returns a Character object The character object contains all the information available on Tibia.com Information from the user's database is also added, like owner and highscores. If the character can't be fetch due to a network error, an NetworkError exception is raised If the character doesn't exist, None is returned. """ if tries == 0: raise errors.NetworkError(f"get_character({name})") try: url = Character.get_url_tibiadata(name) except UnicodeEncodeError: return None if invalid_name.search(name): return None # Fetch website try: character = CACHE_CHARACTERS[name.lower()] except KeyError: try: async with bot.session.get(url) as resp: content = await resp.text(encoding='ISO-8859-1') character = NabChar.from_tibiadata(content) except (aiohttp.ClientError, asyncio.TimeoutError, tibiapy.TibiapyException): await asyncio.sleep(config.network_retry_delay) return await get_character(bot, name, tries=tries - 1) CACHE_CHARACTERS[name.lower()] = character if character is None: return None if character.house: house_id = get_house_id(character.house.name) if house_id: character.house.id = house_id # If the character exists in the online list use data from there where possible try: for c in online_characters[character.world]: if c == character: character.level = c.level character.vocation = c.vocation break except KeyError: pass await bind_database_character(bot, character) return character
def test_character_from_tibiadata(self): """Testing parsing TibiaData content""" content = self._load_resource(FILE_CHARACTER_TIBIADATA) char = Character.from_tibiadata(content) self.assertEqual(char.url_tibiadata, Character.get_url_tibiadata(char.name)) self.assertIsInstance(char, Character) self.assertIsNotNone(char.guild_name) self.assertIsInstance(char.last_login, datetime.datetime) self.assertIsInstance(char.house, CharacterHouse) self.assertEqual(char.house.owner, char.name) self.assertEqual(char.house.town, "Ankrahmun") self.assertEqual(char.house.world, char.world) self.assertIsInstance(char.house.paid_until_date, datetime.date) self.assertTrue(char.deaths[3].by_player)