Skip to content

Commit 4e4b104

Browse files
committed
Fixed worlds parsing
1 parent 67d3ddb commit 4e4b104

File tree

5 files changed

+28
-24
lines changed

5 files changed

+28
-24
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+
.. v3.6.4:
10+
11+
3.6.4 (2021-01-26)
12+
==================
13+
14+
- Fixed world list parsing breaking due to the cookie consent dialog.
15+
916
.. v3.6.3:
1017
1118
3.6.3 (2021-01-14)

tests/resources/world/tibiacom_list_online.txt

Lines changed: 4 additions & 4 deletions
Large diffs are not rendered by default.

tests/tests_world.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,14 @@ def test_world_overview_from_content(self):
109109
self.assertGreater(world_overview.total_online, 0)
110110
self.assertIsNotNone(world_overview.record_date)
111111
self.assertIsNotNone(world_overview.record_count)
112-
self.assertEqual(len(world_overview.regular_worlds), 65)
113-
self.assertEqual(len(world_overview.tournament_worlds), 6)
112+
self.assertEqual(82, len(world_overview.regular_worlds))
113+
self.assertEqual(6, len(world_overview.tournament_worlds))
114114

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

118-
def test_world_overview_from_content_offline(self):
118+
# TODO: Enable when we have a sample again
119+
def _test_world_overview_from_content_offline(self):
119120
"""Testing parsing world overview with offline worlds"""
120121
content = self.load_resource(FILE_WORLD_LIST_OFFLINE)
121122
world_overview = WorldOverview.from_content(content)

tibiapy/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__version__ = '3.6.3'
1+
__version__ = '3.6.4'
22
__author__ = 'Allan Galarza'
33

44
import logging

tibiapy/world.py

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -432,40 +432,37 @@ def from_content(cls, content):
432432
InvalidContent
433433
If the provided content is not the HTML content of the worlds section in Tibia.com
434434
"""
435-
parsed_content = parse_tibiacom_content(content, html_class="TableContentAndRightShadow")
435+
parsed_content = parse_tibiacom_content(content)
436436
world_overview = WorldOverview()
437437
try:
438-
record_row, *rows = parsed_content.find_all("tr")
439-
m = record_regexp.search(record_row.text)
438+
record_table, worlds_header_table, worlds_table, *tournament_tables \
439+
= parsed_content.find_all("table", {"class": "TableContent"})
440+
m = record_regexp.search(record_table.text)
440441
world_overview.record_count = parse_integer(m.group("count"))
441442
world_overview.record_date = parse_tibia_datetime(m.group("date"))
442-
world_rows = rows
443-
world_overview._parse_worlds(world_rows)
443+
regular_world_rows = worlds_table.find_all("tr", attrs={"class": ["Odd", "Even"]})
444+
world_overview._parse_worlds(regular_world_rows)
445+
if tournament_tables:
446+
tournament_world_rows = tournament_tables[1].find_all("tr", attrs={"class": ["Odd", "Even"]})
447+
world_overview._parse_worlds(tournament_world_rows, True)
444448
return world_overview
445449
except (AttributeError, KeyError, ValueError):
446450
raise InvalidContent("content does not belong to the World Overview section in Tibia.com")
447451

448-
def _parse_worlds(self, world_rows):
452+
def _parse_worlds(self, world_rows, tournament=False):
449453
"""Parses the world columns and adds the results to :py:attr:`worlds`.
450454
451455
Parameters
452456
----------
453457
world_rows: :class:`list` of :class:`bs4.Tag`
454458
A list containing the rows of each world.
459+
tournament: :class:`bool`
460+
Whether these are tournament worlds or not.
455461
"""
456-
tournament = False
457462
for world_row in world_rows:
458463
cols = world_row.find_all("td")
459464
name = cols[0].text.strip()
460465
status = "Online"
461-
if len(cols) == 1 and name == "Tournament Worlds":
462-
tournament = True
463-
continue
464-
elif len(cols) == 1 and name == "Regular Worlds":
465-
tournament = False
466-
continue
467-
elif name == "World":
468-
continue
469466
online = parse_integer(cols[1].text.strip(), None)
470467
if online is None:
471468
online = 0
@@ -481,7 +478,6 @@ def _parse_worlds(self, world_rows):
481478
m = battleye_regexp.search(battleye_icon["onmouseover"])
482479
if m:
483480
world.battleye_date = parse_tibia_full_date(m.group(1))
484-
485481
additional_info = cols[5].text.strip()
486482
world._parse_additional_info(additional_info, tournament)
487483
self.worlds.append(world)

0 commit comments

Comments
 (0)