def test_guild_from_tibiadata(self): """Testing parsing a guild from TibiaData""" content = self._load_resource(FILE_GUILD_TIBIADATA) guild = Guild.from_tibiadata(content) self.assertIsInstance(guild, Guild) self.assertTrue(guild.open_applications) self.assertIsNotNone(guild.guildhall) self.assertEqual(guild.founded, datetime.date(2002, 2, 18)) self.assertIsInstance(guild.guildhall, GuildHouse) self.assertEqual(guild.guildhall.world, guild.world) self.assertIsNotNone(guild.logo_url)
async def get_guild(name, title_case=True, *, tries=5) -> Optional[Guild]: """Fetches a guild from TibiaData, parses and returns a Guild object The Guild object contains all the information available on Tibia.com Guilds are case sensitive on tibia.com so guildstats.eu is checked for correct case. If the guild can't be fetched 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_guild({name})") # Fix casing using guildstats.eu if needed # Sorry guildstats.eu :D try: guild = CACHE_GUILDS[name.lower()] return guild except KeyError: pass if not title_case: guild_name = await get_guild_name_from_guildstats(name, tries=tries) name = guild_name if guild_name else name else: name = name.title() # Fetch website try: async with aiohttp.ClientSession() as session: async with session.get(Guild.get_url_tibiadata(name)) as resp: content = await resp.text(encoding='ISO-8859-1') guild = Guild.from_tibiadata(content) except (aiohttp.ClientError, asyncio.TimeoutError, tibiapy.TibiapyException): await asyncio.sleep(config.network_retry_delay) return await get_guild(name, title_case, tries=tries - 1) if guild is None: if title_case: return await get_guild(name, False) else: return None CACHE_GUILDS[name.lower()] = guild return guild
async def get_guild(name, title_case=True, *, tries=5) -> Optional[Guild]: """Fetches a guild from TibiaData, parses and returns a Guild object The Guild object contains all the information available on Tibia.com Guilds are case sensitive on tibia.com so guildstats.eu is checked for correct case. If the guild can't be fetched 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_guild({name})") # Fix casing using guildstats.eu if needed # Sorry guildstats.eu :D try: guild = CACHE_GUILDS[name.lower()] return guild except KeyError: pass if not title_case: guild_name = await get_guild_name_from_guildstats(name, tries=tries) name = guild_name if guild_name else name else: name = name.title() # Fetch website try: async with aiohttp.ClientSession() as session: async with session.get(Guild.get_url_tibiadata(name)) as resp: content = await resp.text(encoding='ISO-8859-1') guild = Guild.from_tibiadata(content) except (aiohttp.ClientError, asyncio.TimeoutError, tibiapy.TibiapyException): await asyncio.sleep(config.network_retry_delay) return await get_guild(name, title_case, tries=tries - 1) if guild is None: if title_case: return await get_guild(name, False) else: return None CACHE_GUILDS[name.lower()] = guild return guild
def test_guild_from_tibiadata_unrelated_section(self): """Testing parsing a different TibiaData json""" content = self._load_resource( tests.tests_character.FILE_CHARACTER_TIBIADATA) with self.assertRaises(InvalidContent): Guild.from_tibiadata(content)
def test_guild_from_tibiadata_invalid_json(self): """Testing parsing an invalid json""" with self.assertRaises(InvalidContent): Guild.from_tibiadata( "<html><p>definitely not a json string</p></html>")
def test_guild_from_tibiadata_with_invites(self): """Testing parsing a guild with invites""" content = self._load_resource(FILE_GUILD_TIBIADATA_INVITED) guild = Guild.from_tibiadata(content) self.assertTrue(len(guild.invites) > 0) self.assertIsInstance(guild.invites[0], GuildInvite)
def test_guild_from_tibiadata_disbanding(self): """Testing parsing a disbanding guild from TibiaData""" content = self._load_resource(FILE_GUILD_TIBIADATA_DISBANDING) guild = Guild.from_tibiadata(content) self.assertIsNotNone(guild.disband_condition) self.assertEqual(guild.disband_date, datetime.date(2018, 12, 26))
def test_guild_from_tibiadata_not_found(self): """Testing parsing a non existent guild""" content = self._load_resource(FILE_GUILD_TIBIADATA_NOT_FOUND) guild = Guild.from_tibiadata(content) self.assertIsNone(guild)