async def fetch_highscores_page(self, world, category=Category.EXPERIENCE, vocation=VocationFilter.ALL, page=1): """Fetches a single highscores page from Tibia.com Parameters ---------- world: :class:`str` The world to search the highscores in. category: :class:`Category` The highscores category to search, by default Experience. vocation: :class:`VocationFilter` The vocation filter to use. No filter used by default. page: :class:`int` The page to fetch, by default the first page is fetched. Returns ------- :class:`Highscores` The highscores information or ``None`` if not found. Raises ------ Forbidden If a 403 Forbidden error was returned. This usually means that Tibia.com is rate-limiting the client because of too many requests. NetworkError If there's any connection errors during the request. """ content = await self._get( Highscores.get_url(world, category, vocation, page)) highscores = Highscores.from_content(content) return highscores
def test_highscores_from_content(self): """Testing parsing Highscores""" content = self.load_resource(FILE_HIGHSCORES_FULL) highscores = Highscores.from_content(content) self.assertEqual("Estela", highscores.world) self.assertEqual(VocationFilter.KNIGHTS, highscores.vocation) self.assertEqual(Category.MAGIC_LEVEL, highscores.category) self.assertEqual(BattlEyeHighscoresFilter.ANY_WORLD, highscores.battleye_filter) self.assertEqual(1983, highscores.results_count) self.assertEqual(38, highscores.from_rank) self.assertEqual(38, highscores.to_rank) self.assertEqual(4, highscores.page) self.assertEqual(40, highscores.total_pages) self.assertIsNotNone(highscores.get_page_url(1)) self.assertIsNotNone(highscores.url) self.assertIsNotNone(highscores.next_page_url) self.assertIsNotNone(highscores.previous_page_url) self.assertEqual(datetime.timedelta(minutes=6), highscores.last_updated) for entry in highscores.entries: self.assertIsInstance(entry, HighscoresEntry) self.assertIsInstance(entry.name, str) self.assertIsInstance(entry.vocation, Vocation) self.assertIsInstance(entry.rank, int) self.assertIsInstance(entry.value, int) self.assertIsInstance(entry.level, int) self.assertEqual("Estela", entry.world)
def test_highscores_from_content_battleye_and_pvp_filters(self): """Testing parsing Highscores""" content = self.load_resource(FILE_HIGHSCORES_BATTLEYE_PVP_FILTER) highscores = Highscores.from_content(content) self.assertEqual(None, highscores.world) self.assertEqual(VocationFilter.ALL, highscores.vocation) self.assertEqual(Category.EXPERIENCE, highscores.category) self.assertEqual(BattlEyeHighscoresFilter.INITIALLY_PROTECTED, highscores.battleye_filter) self.assertEqual(1000, highscores.results_count) self.assertEqual(1, highscores.from_rank) self.assertEqual(50, highscores.to_rank) self.assertEqual(1, highscores.page) self.assertEqual(20, highscores.total_pages) self.assertEqual(3, len(highscores.pvp_types_filter)) self.assertIsNotNone(highscores.url) self.assertEqual(datetime.timedelta(minutes=27), highscores.last_updated) for entry in highscores.entries: self.assertIsInstance(entry, HighscoresEntry) self.assertIsInstance(entry.name, str) self.assertIsInstance(entry.vocation, Vocation) self.assertIsInstance(entry.rank, int) self.assertIsInstance(entry.value, int) self.assertIsInstance(entry.level, int)
async def get_highscores(world, category=Category.EXPERIENCE, vocation=VocationFilter.ALL, *, tries=5) \ -> Optional[Highscores]: """Gets all the highscores entries of a world, category and vocation.""" # TODO: Add caching if tries == 0: raise errors.NetworkError(f"get_highscores_tibiadata({world},{category},{vocation})") try: async with aiohttp.ClientSession() as session: async with session.get(Highscores.get_url_tibiadata(world, category, vocation)) as resp: content = await resp.text() highscores = Highscores.from_tibiadata(content, vocation) except (aiohttp.ClientError, asyncio.TimeoutError, tibiapy.TibiapyException): await asyncio.sleep(config.network_retry_delay) return await get_highscores(world, category, vocation, tries=tries - 1) return highscores
def _test_highscores_from_content_no_results(self): """Testing parsing highscores with no results (first day of a new world)""" content = self.load_resource(FILE_HIGHSCORES_NO_RESULTS) highscores = Highscores.from_content(content) self.assertIsInstance(highscores, Highscores) self.assertEqual(highscores.world, "Unica") self.assertEqual(highscores.category, Category.EXPERIENCE) self.assertEqual(highscores.vocation, VocationFilter.ALL) self.assertEqual(highscores.total_pages, 0) self.assertEqual(len(highscores.entries), 0)
def test_highscores_from_tibiadata(self): """Testing parsing highscores from TibiaData""" content = self._load_resource(FILE_HIGHSCORES_TIBIADATA_FULL) highscores = Highscores.from_tibiadata(content) self.assertEqual(highscores.world, "Antica") self.assertEqual(highscores.vocation, VocationFilter.ALL) self.assertEqual(highscores.category, Category.AXE_FIGHTING) self.assertEqual(highscores.results_count, 300) self.assertEqual( highscores.url_tibiadata, Highscores.get_url_tibiadata(highscores.world, highscores.category, highscores.vocation)) for entry in highscores.entries: self.assertIsInstance(entry, HighscoresEntry) self.assertIsInstance(entry.name, str) self.assertIsInstance(entry.vocation, Vocation) self.assertIsInstance(entry.rank, int) self.assertIsInstance(entry.value, int)
async def get_highscores(world, category=Category.EXPERIENCE, vocation=VocationFilter.ALL, *, tries=5) \ -> Optional[Highscores]: """Gets all the highscores entries of a world, category and vocation.""" # TODO: Add caching if tries == 0: raise errors.NetworkError( f"get_highscores_tibiadata({world},{category},{vocation})") try: async with aiohttp.ClientSession() as session: async with session.get( Highscores.get_url_tibiadata(world, category, vocation)) as resp: content = await resp.text() highscores = Highscores.from_tibiadata(content, vocation) except (aiohttp.ClientError, asyncio.TimeoutError, tibiapy.TibiapyException): await asyncio.sleep(config.network_retry_delay) return await get_highscores(world, category, vocation, tries=tries - 1) return highscores
async def test_client_fetch_highscores_page(self, mock): """Testing fetching a highscores page""" world = "Estela" category = Category.MAGIC_LEVEL vocations = VocationFilter.KNIGHTS content = self.load_resource(FILE_HIGHSCORES_FULL) mock.get(Highscores.get_url(world, category, vocations), status=200, body=content) highscores = await self.client.fetch_highscores_page( world, category, vocations) self.assertIsInstance(highscores.data, Highscores)
def test_highscore_changes(self): content = self.load_resource( "website_changes/1_community_highscores.txt") highscores = Highscores.from_content(content) self.assertIsNone(highscores.world) self.assertEqual(Category.EXPERIENCE, highscores.category) self.assertEqual(VocationFilter.ALL, highscores.vocation) self.assertEqual(1000, highscores.results_count) self.assertEqual(1, highscores.page) self.assertEqual(20, highscores.total_pages) first_entry = highscores.entries[0] self.assertEqual("Goraca", first_entry.name) self.assertEqual(Vocation.MASTER_SORCERER, first_entry.vocation) self.assertEqual(1904, first_entry.level) self.assertEqual(114_797_203_877, first_entry.value)
def test_highscores_from_tibiadata_experience(self): """Testing parsing experience highscores""" content = self._load_resource(FILE_HIGHSCORES_TIBIADATA_EXPERIENCE) highscores = Highscores.from_tibiadata(content) self.assertEqual(highscores.world, "Luminera") self.assertEqual(highscores.vocation, VocationFilter.ALL) self.assertEqual(highscores.category, Category.EXPERIENCE) self.assertEqual(highscores.results_count, 300) for entry in highscores.entries: self.assertIsInstance(entry, ExpHighscoresEntry) self.assertIsInstance(entry.name, str) self.assertIsInstance(entry.vocation, Vocation) self.assertIsInstance(entry.rank, int) self.assertIsInstance(entry.value, int) self.assertIsInstance(entry.level, int)
def test_highscores_from_tibiadata_loyalty(self): """Testing parsing loyalty highscores""" content = self._load_resource(FILE_HIGHSCORES_TIBIADATA_LOYALTY) highscores = Highscores.from_tibiadata(content) self.assertEqual(highscores.world, "Zunera") self.assertEqual(highscores.vocation, VocationFilter.ALL) self.assertEqual(highscores.category, Category.LOYALTY_POINTS) self.assertEqual(highscores.results_count, 57) for entry in highscores.entries: self.assertIsInstance(entry, LoyaltyHighscoresEntry) self.assertIsInstance(entry.name, str) self.assertIsInstance(entry.vocation, Vocation) self.assertIsInstance(entry.rank, int) self.assertIsInstance(entry.value, int) self.assertIsInstance(entry.title, str)
def test_highscores_from_content_loyalty(self): """Testing parsing experience loyalty""" content = self._load_resource(FILE_HIGHSCORES_LOYALTY) highscores = Highscores.from_content(content) self.assertEqual(highscores.world, "Calmera") self.assertEqual(highscores.vocation, VocationFilter.PALADINS) self.assertEqual(highscores.category, Category.LOYALTY_POINTS) self.assertEqual(highscores.results_count, 65) self.assertEqual(highscores.total_pages, 3) for entry in highscores.entries: self.assertIsInstance(entry, LoyaltyHighscoresEntry) self.assertIsInstance(entry.name, str) self.assertIsInstance(entry.vocation, Vocation) self.assertIsInstance(entry.rank, int) self.assertIsInstance(entry.value, int) self.assertIsInstance(entry.title, str)
def test_highscores_from_content_highscores(self): """Testing parsing experience highscores""" content = self._load_resource(FILE_HIGHSCORES_EXPERIENCE) highscores = Highscores.from_content(content) self.assertEqual(highscores.world, "Gladera") self.assertEqual(highscores.vocation, VocationFilter.PALADINS) self.assertEqual(highscores.category, Category.EXPERIENCE) self.assertEqual(highscores.results_count, 300) self.assertEqual(highscores.total_pages, 12) for entry in highscores.entries: self.assertIsInstance(entry, ExpHighscoresEntry) self.assertIsInstance(entry.name, str) self.assertIsInstance(entry.vocation, Vocation) self.assertIsInstance(entry.rank, int) self.assertIsInstance(entry.value, int) self.assertIsInstance(entry.level, int)
def test_highscores_from_content(self): """Testing parsing Highscores""" content = self._load_resource(FILE_HIGHSCORES_FULL) highscores = Highscores.from_content(content) self.assertEqual(highscores.world, "Estela") self.assertEqual(highscores.vocation, VocationFilter.KNIGHTS) self.assertEqual(highscores.category, Category.MAGIC_LEVEL) self.assertEqual(highscores.results_count, 300) self.assertEqual(highscores.from_rank, 76) self.assertEqual(highscores.to_rank, 100) self.assertEqual(highscores.page, 4) self.assertEqual(highscores.total_pages, 12) self.assertIsNotNone(highscores.url) for entry in highscores.entries: self.assertIsInstance(entry, HighscoresEntry) self.assertIsInstance(entry.name, str) self.assertIsInstance(entry.vocation, Vocation) self.assertIsInstance(entry.rank, int) self.assertIsInstance(entry.value, int)
def test_highscores_from_content(self): """Testing parsing Highscores""" content = self.load_resource(FILE_HIGHSCORES_FULL) highscores = Highscores.from_content(content) self.assertEqual("Estela", highscores.world) self.assertEqual(VocationFilter.KNIGHTS, highscores.vocation) self.assertEqual(Category.MAGIC_LEVEL, highscores.category) self.assertEqual(1932, highscores.results_count) self.assertEqual(31, highscores.from_rank) self.assertEqual(31, highscores.to_rank) self.assertEqual(4, highscores.page) self.assertEqual(39, highscores.total_pages) self.assertIsNotNone(highscores.url) self.assertEqual(datetime.timedelta(minutes=2), highscores.last_updated) for entry in highscores.entries: self.assertIsInstance(entry, HighscoresEntry) self.assertIsInstance(entry.name, str) self.assertIsInstance(entry.vocation, Vocation) self.assertIsInstance(entry.rank, int) self.assertIsInstance(entry.value, int) self.assertEqual("Estela", entry.world)
def test_highscores_from_content_unrelated_section(self): """Testing parsing an unrelated section""" content = self.load_resource(self.FILE_UNRELATED_SECTION) with self.assertRaises(InvalidContent): Highscores.from_content(content)
def test_highscores_from_content_empty(self): """Testing parsing highscores when empty (world doesn't exist)""" content = self.load_resource(FILE_HIGHSCORES_EMPTY) highscores = Highscores.from_content(content) self.assertIsNone(highscores)
def test_highscores_from_tibiadata_empty(self): """Testing parsing empty highscores""" content = self._load_resource(FILE_HIGHSCORES_TIBIADATA_EMPTY) highscores = Highscores.from_tibiadata(content) self.assertIsNone(highscores)
def test_highscores_from_content_empty(self): """Testing parsing highscores empty highscores""" content = self._load_resource(FILE_HIGHSCORES_EMPTY) highscores = Highscores.from_content(content) self.assertIsNone(highscores)
def test_highscores_from_tibiadata_unrelated_section(self): """Testing parsing an unrelated section""" with self.assertRaises(InvalidContent): Highscores.from_tibiadata( self._load_resource( tests.tests_character.FILE_CHARACTER_TIBIADATA))