Skip to content

Commit fdd4ea2

Browse files
committed
fix: read currency templates in mount prices
1 parent d0a4f11 commit fdd4ea2

2 files changed

Lines changed: 51 additions & 3 deletions

File tree

‎tests/parsers/test_mount.py‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,33 @@ def test_pricecurrency_wins_over_tournament(self):
126126
))
127127

128128
self.assertEqual("Event Points", mount.price_currency)
129+
130+
def _priced_in(self, currency: str) -> Mount:
131+
return self._parse("Doombringer", self._edited("| price = 780\n",
132+
f"| price = 780\n| pricecurrency = {currency}\n"))
133+
134+
def test_price_currency_cleans_links(self):
135+
mount = self._priced_in("[[Silver Token]]s")
136+
137+
self.assertEqual("Silver Tokens", mount.price_currency)
138+
139+
def test_price_currency_tibia_coins_template(self):
140+
mount = self._priced_in("{{TC}}")
141+
142+
self.assertEqual("Tibia Coins", mount.price_currency)
143+
144+
def test_price_currency_tournament_coins_template(self):
145+
mount = self._priced_in("{{TC3}}")
146+
147+
self.assertEqual("Tournament Coins", mount.price_currency)
148+
149+
def test_price_currency_template_first_letter_any_case(self):
150+
mount = self._priced_in("{{tC}}")
151+
152+
self.assertEqual("Tibia Coins", mount.price_currency)
153+
154+
def test_price_currency_kept_for_zero_price(self):
155+
mount = self._parse("Doombringer", self._edited("| price = 780\n", "| price = 0\n"))
156+
157+
self.assertEqual(0, mount.price)
158+
self.assertEqual("Tibia Coins", mount.price_currency)

‎tibiawikisql/parsers/mount.py‎

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Changed by tibia.sh in 2026. See "About this copy" in README.md.
2+
import re
23
from typing import Any, ClassVar
34

45
import tibiawikisql.schema
@@ -8,6 +9,15 @@
89
from tibiawikisql.parsers import BaseParser
910
from tibiawikisql.utils import clean_links, client_color_to_rgb, parse_boolean, parse_client_id, parse_integer
1011

12+
TIBIA_COINS = "Tibia Coins"
13+
TOURNAMENT_COINS = "Tournament Coins"
14+
15+
CURRENCY_TEMPLATES = {"TC": TIBIA_COINS, "TC3": TOURNAMENT_COINS}
16+
"""The currency templates that `Template:Infobox Mount` prints, by name."""
17+
18+
CURRENCY_TEMPLATE_PATTERN = re.compile(r"\{\{([Tt]C3?)\}\}")
19+
"""A value that is only `{{TC}}` or `{{TC3}}`. As in MediaWiki, only the first letter of the name ignores case."""
20+
1121

1222
def remove_mount(name: str) -> str:
1323
"""Remove "(Mount)" from the name, if found.
@@ -47,11 +57,19 @@ def parse_attributes(cls, article: Article) -> dict[str, Any]:
4757
raw_attributes = row["_raw_attributes"]
4858
row["price_currency"] = None
4959
if row["price"] is not None:
50-
currency = clean_links(raw_attributes.get("pricecurrency", ""))
60+
currency = cls._parse_currency(raw_attributes.get("pricecurrency", ""))
5161
if currency:
5262
row["price_currency"] = currency
5363
elif parse_boolean(raw_attributes.get("tournament", "")):
54-
row["price_currency"] = "Tournament Coins"
64+
row["price_currency"] = TOURNAMENT_COINS
5565
else:
56-
row["price_currency"] = "Tibia Coins"
66+
row["price_currency"] = TIBIA_COINS
5767
return row
68+
69+
@staticmethod
70+
def _parse_currency(value: str) -> str:
71+
"""Read `pricecurrency`: the currency that `{{TC}}` or `{{TC3}}` prints, otherwise the text without links."""
72+
template = CURRENCY_TEMPLATE_PATTERN.fullmatch(value.strip())
73+
if template:
74+
return CURRENCY_TEMPLATES[template.group(1).upper()]
75+
return clean_links(value)

0 commit comments

Comments
 (0)