Skip to content

Commit

Permalink
Fixed worlds parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
Galarzaa90 committed Jan 26, 2021
1 parent 67d3ddb commit 4e4b104
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 24 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ Changelog
Due to this library relying on external content, older versions are not guaranteed to work.
Try to always use the latest version.

.. v3.6.4:
3.6.4 (2021-01-26)
==================

- Fixed world list parsing breaking due to the cookie consent dialog.

.. v3.6.3:
3.6.3 (2021-01-14)
Expand Down
8 changes: 4 additions & 4 deletions tests/resources/world/tibiacom_list_online.txt

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions tests/tests_world.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,14 @@ def test_world_overview_from_content(self):
self.assertGreater(world_overview.total_online, 0)
self.assertIsNotNone(world_overview.record_date)
self.assertIsNotNone(world_overview.record_count)
self.assertEqual(len(world_overview.regular_worlds), 65)
self.assertEqual(len(world_overview.tournament_worlds), 6)
self.assertEqual(82, len(world_overview.regular_worlds))
self.assertEqual(6, len(world_overview.tournament_worlds))

worlds = ListedWorld.list_from_content(content)
self.assertEqual(len(world_overview.worlds), len(worlds))

def test_world_overview_from_content_offline(self):
# TODO: Enable when we have a sample again
def _test_world_overview_from_content_offline(self):
"""Testing parsing world overview with offline worlds"""
content = self.load_resource(FILE_WORLD_LIST_OFFLINE)
world_overview = WorldOverview.from_content(content)
Expand Down
2 changes: 1 addition & 1 deletion tibiapy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = '3.6.3'
__version__ = '3.6.4'
__author__ = 'Allan Galarza'

import logging
Expand Down
28 changes: 12 additions & 16 deletions tibiapy/world.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,40 +432,37 @@ def from_content(cls, content):
InvalidContent
If the provided content is not the HTML content of the worlds section in Tibia.com
"""
parsed_content = parse_tibiacom_content(content, html_class="TableContentAndRightShadow")
parsed_content = parse_tibiacom_content(content)
world_overview = WorldOverview()
try:
record_row, *rows = parsed_content.find_all("tr")
m = record_regexp.search(record_row.text)
record_table, worlds_header_table, worlds_table, *tournament_tables \
= parsed_content.find_all("table", {"class": "TableContent"})
m = record_regexp.search(record_table.text)
world_overview.record_count = parse_integer(m.group("count"))
world_overview.record_date = parse_tibia_datetime(m.group("date"))
world_rows = rows
world_overview._parse_worlds(world_rows)
regular_world_rows = worlds_table.find_all("tr", attrs={"class": ["Odd", "Even"]})
world_overview._parse_worlds(regular_world_rows)
if tournament_tables:
tournament_world_rows = tournament_tables[1].find_all("tr", attrs={"class": ["Odd", "Even"]})
world_overview._parse_worlds(tournament_world_rows, True)
return world_overview
except (AttributeError, KeyError, ValueError):
raise InvalidContent("content does not belong to the World Overview section in Tibia.com")

def _parse_worlds(self, world_rows):
def _parse_worlds(self, world_rows, tournament=False):
"""Parses the world columns and adds the results to :py:attr:`worlds`.
Parameters
----------
world_rows: :class:`list` of :class:`bs4.Tag`
A list containing the rows of each world.
tournament: :class:`bool`
Whether these are tournament worlds or not.
"""
tournament = False
for world_row in world_rows:
cols = world_row.find_all("td")
name = cols[0].text.strip()
status = "Online"
if len(cols) == 1 and name == "Tournament Worlds":
tournament = True
continue
elif len(cols) == 1 and name == "Regular Worlds":
tournament = False
continue
elif name == "World":
continue
online = parse_integer(cols[1].text.strip(), None)
if online is None:
online = 0
Expand All @@ -481,7 +478,6 @@ def _parse_worlds(self, world_rows):
m = battleye_regexp.search(battleye_icon["onmouseover"])
if m:
world.battleye_date = parse_tibia_full_date(m.group(1))

additional_info = cols[5].text.strip()
world._parse_additional_info(additional_info, tournament)
self.worlds.append(world)

0 comments on commit 4e4b104

Please sign in to comment.