Skip to content

Commit 9383ef3

Browse files
committed
Fixed worlds not being parsed correctly again due to tournament worlds order changing. After this fix,
the order should not matter anymore
1 parent c494439 commit 9383ef3

File tree

2 files changed

+30
-16
lines changed

2 files changed

+30
-16
lines changed

CHANGELOG.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ Changelog
66
Due to this library relying on external content, older versions are not guaranteed to work.
77
Try to always use the latest version.
88

9+
.. v4.1.6
10+
11+
4.1.6 (2021-06-28)
12+
==================
13+
- Fixed worlds not being parsed correctly again due to tournament worlds order changing. After this fix,
14+
the order should not matter anymore.
15+
916
.. v4.1.5
1017
1118
4.1.5 (2021-06-25)

tibiapy/world.py

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -466,25 +466,14 @@ def from_content(cls, content):
466466
parsed_content = parse_tibiacom_content(content)
467467
world_overview = WorldOverview()
468468
try:
469-
record_table, worlds_header_table, *worlds_tables \
470-
= parsed_content.find_all("table", {"class": "TableContent"})
471-
m = record_regexp.search(record_table.text)
469+
records_table, *tables = parsed_content.find_all("table", {"class": "TableContent"})
470+
m = record_regexp.search(records_table.text)
472471
world_overview.record_count = parse_integer(m.group("count"))
473472
world_overview.record_date = parse_tibia_datetime(m.group("date"))
474-
if len(worlds_tables) > 1:
475-
tournament_tables = worlds_tables[0]
476-
worlds_table = worlds_tables[2]
477-
else:
478-
tournament_tables = None
479-
worlds_table = worlds_tables[0]
480-
regular_world_rows = worlds_table.find_all("tr", attrs={"class": ["Odd", "Even"]})
481-
world_overview._parse_worlds(regular_world_rows)
482-
if tournament_tables:
483-
tournament_world_rows = tournament_tables.find_all("tr", attrs={"class": ["Odd", "Even"]})
484-
world_overview._parse_worlds(tournament_world_rows, True)
473+
world_overview._parse_worlds_tables(tables)
485474
return world_overview
486-
except (AttributeError, KeyError, ValueError):
487-
raise InvalidContent("content does not belong to the World Overview section in Tibia.com")
475+
except (AttributeError, KeyError, ValueError) as e:
476+
raise InvalidContent("content does not belong to the World Overview section in Tibia.com", e)
488477

489478
def _parse_worlds(self, world_rows, tournament=False):
490479
"""Parses the world columns and adds the results to :py:attr:`worlds`.
@@ -518,3 +507,21 @@ def _parse_worlds(self, world_rows, tournament=False):
518507
additional_info = cols[5].text.strip()
519508
world._parse_additional_info(additional_info, tournament)
520509
self.worlds.append(world)
510+
511+
def _parse_worlds_tables(self, tables):
512+
"""Parses the world columns and adds the results to :py:attr:`worlds`.
513+
514+
Parameters
515+
----------
516+
world_rows: :class:`list` of :class:`bs4.Tag`
517+
A list containing the rows of each world.
518+
tournament: :class:`bool`
519+
Whether these are tournament worlds or not.
520+
"""
521+
for title_table, worlds_table in zip(tables, tables[1:]):
522+
title = title_table.text.lower()
523+
regular_world_rows = worlds_table.find_all("tr", attrs={"class": ["Odd", "Even"]})
524+
self._parse_worlds(regular_world_rows, "tournament" in title)
525+
526+
527+

0 commit comments

Comments
 (0)