def test_guild_from_content(self): """Testing parsing a guild""" content = self._load_resource(FILE_GUILD_FULL) guild = Guild.from_content(content) self.assertIsInstance(guild, Guild, "Guild should be a Guild object.") self.assertEqual(guild.url, Guild.get_url(guild.name)) self.assertEqual(guild.url_tibiadata, Guild.get_url_tibiadata(guild.name)) self.assertTrue(guild.active, "Guild should be active") self.assertIsInstance( guild.founded, datetime.date, "Guild founded date should be an instance of datetime.date") self.assertTrue(guild.open_applications, "Guild applications should be open") self.assertIsNotNone(guild.description, "Guild should have a description") self.assertTrue(guild.members, "Guild should have members") self.assertEqual(guild.member_count, len(guild.members)) self.assertTrue(guild.invites, "Guild should have invites") self.assertIsInstance(guild.online_members, list, "Guild online members should be a list.") self.assertEqual( len(guild.online_members), guild.online_count, "Length of online_members should be equal " "to online_count") self.assertTrue(guild.ranks, "Guild ranks should not be empty.") for member in guild.members: self.assertIsInstance(member.level, int, "Member level should be an integer.") self.assertIsInstance( member.joined, datetime.date, "Member's joined date should be datetime.date.") for invited in guild.invites: self.assertIsNotNone( invited.name, "Invited character's name should not be None.") self.assertIsInstance( invited.date, datetime.date, "Invited character's date should be datetime.date.") self.assertIsInstance(guild.guildhall, GuildHouse) self.assertEqual(guild.guildhall.owner, guild.members[0].name) self.assertEqual(guild.guildhall.world, guild.world) self.assertIsInstance(guild.guildhall.paid_until_date, datetime.date)
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