Skip to content

Parsers

tibiawikisql.parsers.base

Classes:

Name Description
AttributeParser

Defines how to parser an attribute from a Wiki article into a python object.

ParserMeta

Metaclass for all parsers.

BaseParser

Base class that defines how to extract information from a Wiki template into a model.

AttributeParser

AttributeParser(
    func: Callable[[dict[str, str]], T], fallback: D = ...
)

Bases: Generic[T]

Defines how to parser an attribute from a Wiki article into a python object.

Parameters:

Name Type Description Default
func Callable[[dict[str, str]], T]

A callable that takes the template's attributes as a parameter and returns a value.

required
fallback D

Fallback value to set if the value is not found or the callable failed.

...

Methods:

Name Description
__call__

Perform parsing on the defined attribute.

required

Define a required attribute.

optional

Create optional attribute parser. Will fall back to None.

status

Create a parser for the commonly found "status" parameter.

version

Create a parser for the commonly found "implemented" parameter.

Source code in tibiawikisql/parsers/base.py
def __init__(self, func: Callable[[dict[str, str]], T], fallback: D = ...) -> None:
    """Create an instance of the class.

    Args:
        func: A callable that takes the template's attributes as a parameter and returns a value.
        fallback: Fallback value to set if the value is not found or the callable failed.

    """
    self.func = func
    self.fallback = fallback
__call__
__call__(attributes: dict[str, str]) -> T | D

Perform parsing on the defined attribute.

Parameters:

Name Type Description Default
attributes dict[str, str]

The template attributes.

required

Returns:

Type Description
T | D

The result of the parser's function or the fallback value if applicable.

Raises:

Type Description
AttributeParsingError

If the parser function fails and no fallback was provided.

Source code in tibiawikisql/parsers/base.py
def __call__(self, attributes: dict[str, str]) -> T | D:
    """Perform parsing on the defined attribute.

    Args:
        attributes: The template attributes.

    Returns:
        The result of the parser's function or the fallback value if applicable.

    Raises:
        AttributeParsingError: If the parser function fails and no fallback was provided.
    """
    try:
        return self.func(attributes)
    except Exception as e:
        if self.fallback is Ellipsis:
            raise AttributeParsingError(e) from e
        return self.fallback
required classmethod
required(
    field_name: str,
    post_process: Callable[[str], T] = strip,
) -> Self

Define a required attribute.

Parameters:

Name Type Description Default
field_name str

The name of the template attribute in the wiki.

required
post_process Callable[[str], T]

A function to call on the attribute's value.

strip

Returns:

Type Description
Self

An attribute parser expecting a required value.

Source code in tibiawikisql/parsers/base.py
@classmethod
def required(cls, field_name: str, post_process: Callable[[str], T] = str.strip) -> Self:
    """Define a required attribute.

    Args:
        field_name: The name of the template attribute in the wiki.
        post_process: A function to call on the attribute's value.

    Returns:
        An attribute parser expecting a required value.

    """
    return cls(lambda x: post_process(x[field_name]))
optional classmethod
optional(
    field_name: str,
    post_process: Callable[[str], T | None] = strip,
    default: T | None = None,
) -> Self

Create optional attribute parser. Will fall back to None.

Parameters:

Name Type Description Default
field_name str

The name of the template attribute in the wiki.

required
post_process Callable[[str], T | None]

A function to call on the attribute's value.

strip
default T | None
None

Returns:

Type Description
Self

An attribute parser for an optional value.

Source code in tibiawikisql/parsers/base.py
@classmethod
def optional(cls, field_name: str, post_process: Callable[[str], T | None] = str.strip, default: T | None = None) -> Self:
    """Create optional attribute parser. Will fall back to None.

    Args:
        field_name: The name of the template attribute in the wiki.
        post_process: A function to call on the attribute's value.
        default:

    Returns:
        An attribute parser for an optional value.

    """
    return cls(lambda x: post_process(x[field_name]), default)
status classmethod
status() -> Self

Create a parser for the commonly found "status" parameter.

Returns:

Type Description
Self

An attribute parser for the status parameter, falling back to "active" if not found.

Source code in tibiawikisql/parsers/base.py
@classmethod
def status(cls) -> Self:
    """Create a parser for the commonly found "status" parameter.

    Returns:
        An attribute parser for the status parameter, falling back to "active" if not found.

    """
    return cls(lambda x: x.get("status").lower(), "active")
version classmethod
version() -> Self

Create a parser for the commonly found "implemented" parameter.

Returns:

Type Description
Self

An attribute parser for the implemented parameter.

Source code in tibiawikisql/parsers/base.py
@classmethod
def version(cls) -> Self:
    """Create a parser for the commonly found "implemented" parameter.

    Returns:
        An attribute parser for the implemented parameter.

    """
    return cls(lambda x: x.get("implemented").lower())

ParserMeta

Bases: type

Metaclass for all parsers.

BaseParser

Base class that defines how to extract information from a Wiki template into a model.

Methods:

Name Description
parse_attributes

Parse the attributes of an article into a mapping.

from_article

Parse an article into a TibiaWiki model.

Attributes:

Name Type Description
template_name str

The name of the template that contains the information.

model type[RowModel]

The model to convert the data into.

table type[Table]

The SQL table where the data wil be stored.

attribute_map dict[str, AttributeParser]

A map defining how to process every template attribute.

template_name class-attribute
template_name: str = NotImplemented

The name of the template that contains the information.

model class-attribute

The model to convert the data into.

table class-attribute

The SQL table where the data wil be stored.

attribute_map class-attribute

A map defining how to process every template attribute.

parse_attributes classmethod
parse_attributes(article: Article) -> dict[str, Any]

Parse the attributes of an article into a mapping.

By default, it will apply the attribute map, but it can be overridden to parse attributes in more complex ways. It is called by parse_article.

Parameters:

Name Type Description Default
article Article

The article to extract the data from.

required

Returns:

Type Description
dict[str, Any]

A dictionary containing the parsed attribute values.

Raises:

Type Description
AttributeParsingError

If the required template is not found.

Source code in tibiawikisql/parsers/base.py
@classmethod
def parse_attributes(cls, article: Article) -> dict[str, Any]:
    """Parse the attributes of an article into a mapping.

    By default, it will apply the attribute map, but it can be overridden to parse attributes in more complex ways.
    It is called by `parse_article`.

    Args:
        article: The article to extract the data from.

    Returns:
        A dictionary containing the parsed attribute values.

    Raises:
        AttributeParsingError: If the required template is not found.

    """
    templates = parse_templatates_data(article.content)
    if cls.template_name not in templates:
        raise TemplateNotFoundError(article, cls)
    attributes = templates[cls.template_name]
    row = {
        "article_id": article.article_id,
        "timestamp": article.timestamp,
        "title": article.title,
        "_raw_attributes": attributes,
    }
    try:
        for field, parser in cls.attribute_map.items():
            row[field] = parser(attributes)
    except AttributeParsingError as e:
        raise ArticleParsingError(article, e) from e
    return row
from_article classmethod
from_article(article: Article) -> M

Parse an article into a TibiaWiki model.

Parameters:

Name Type Description Default
article Article

The article from where the model is parsed.

required

Returns:

Type Description
M

An inherited model object for the current article.

Source code in tibiawikisql/parsers/base.py
@classmethod
def from_article(cls, article: Article) -> M:
    """Parse an article into a TibiaWiki model.

    Args:
        article: The article from where the model is parsed.

    Returns:
        An inherited model object for the current article.

    """
    row = cls.parse_attributes(article)
    try:
        return cls.model.model_validate(row)
    except ValidationError as e:
        raise ArticleParsingError(article, cause=e) from e

tibiawikisql.parsers.achievement

Classes:

Name Description
AchievementParser

Parser for achievements.

AchievementParser

Bases: BaseParser

Parser for achievements.

Attributes:

Name Type Description
model

The model to convert the data into.

table

The SQL table where the data wil be stored.

template_name

The name of the template that contains the information.

attribute_map ClassVar

A map defining how to process every template attribute.

model class-attribute instance-attribute
model = Achievement

The model to convert the data into.

table class-attribute instance-attribute

The SQL table where the data wil be stored.

template_name class-attribute instance-attribute
template_name = 'Infobox_Achievement'

The name of the template that contains the information.

attribute_map class-attribute instance-attribute
attribute_map: ClassVar = {
    "name": AttributeParser(
        lambda x: get("actualname") or get("name")
    ),
    "grade": optional("grade", parse_integer),
    "points": optional("points", parse_integer),
    "is_premium": optional("premium", parse_boolean, False),
    "is_secret": optional("secret", parse_boolean, False),
    "description": required("description", clean_links),
    "spoiler": optional("spoiler", clean_links),
    "achievement_id": optional(
        "achievementid", parse_integer
    ),
    "version": optional("implemented"),
    "status": status(),
}

A map defining how to process every template attribute.

tibiawikisql.parsers.book

Classes:

Name Description
BookParser

Parser for book articles.

BookParser

Bases: BaseParser

Parser for book articles.

Attributes:

Name Type Description
model

The model to convert the data into.

table

The SQL table where the data wil be stored.

template_name

The name of the template that contains the information.

attribute_map ClassVar

A map defining how to process every template attribute.

model class-attribute instance-attribute
model = Book

The model to convert the data into.

table class-attribute instance-attribute
table = BookTable

The SQL table where the data wil be stored.

template_name class-attribute instance-attribute
template_name = 'Infobox_Book'

The name of the template that contains the information.

attribute_map class-attribute instance-attribute
attribute_map: ClassVar = {
    "name": required("title"),
    "book_type": optional("booktype", clean_links),
    "location": optional(
        "location", lambda x: clean_links(x, True)
    ),
    "blurb": optional(
        "blurb", lambda x: clean_links(x, True)
    ),
    "author": optional(
        "author", lambda x: clean_links(x, True)
    ),
    "prev_book": optional("prevbook"),
    "next_book": optional("nextbook"),
    "text": required("text", clean_links),
    "version": optional("implemented"),
    "status": status(),
}

A map defining how to process every template attribute.

tibiawikisql.parsers.charm

Classes:

Name Description
CharmParser

Parser for charms.

CharmParser

Bases: BaseParser

Parser for charms.

Attributes:

Name Type Description
model

The model to convert the data into.

table

The SQL table where the data wil be stored.

template_name

The name of the template that contains the information.

attribute_map ClassVar

A map defining how to process every template attribute.

model class-attribute instance-attribute
model = Charm

The model to convert the data into.

table class-attribute instance-attribute
table = CharmTable

The SQL table where the data wil be stored.

template_name class-attribute instance-attribute
template_name = 'Infobox_Charm'

The name of the template that contains the information.

attribute_map class-attribute instance-attribute
attribute_map: ClassVar = {
    "name": AttributeParser(
        lambda x: get("actualname") or get("name")
    ),
    "type": AttributeParser(lambda x: get("type")),
    "effect": AttributeParser(
        lambda x: clean_links(get("effect"))
    ),
    "cost": AttributeParser(
        lambda x: parse_integer(get("cost"))
    ),
    "version": AttributeParser(
        lambda x: get("implemented"), None
    ),
    "status": status(),
}

A map defining how to process every template attribute.

tibiawikisql.parsers.creature

Classes:

Name Description
CreatureParser

Parses creatures or monsters.

Functions:

Name Description
parse_maximum_damage

Parse the maximum damage template from TibiaWiki.

parse_maximum_integer

From a string, finds the highest integer found.

parse_loot

Get every item drop entry of a creature's drops.

parse_abilities

Parse the abilities of a creature.

parse_monster_walks

Match the values against a regex to filter typos or bad data on the wiki.

CreatureParser

Bases: BaseParser

Parses creatures or monsters.

Methods:

Name Description
parse_attributes

Parse the attributes of an article into a mapping.

Attributes:

Name Type Description
model

The model to convert the data into.

template_name

The name of the template that contains the information.

table

The SQL table where the data wil be stored.

attribute_map ClassVar

A map defining how to process every template attribute.

model class-attribute instance-attribute
model = Creature

The model to convert the data into.

template_name class-attribute instance-attribute
template_name = 'Infobox_Creature'

The name of the template that contains the information.

table class-attribute instance-attribute
table = CreatureTable

The SQL table where the data wil be stored.

attribute_map class-attribute instance-attribute
attribute_map: ClassVar = {
    "name": AttributeParser(
        lambda x: get("actualname") or get("name")
    ),
    "article": optional("article"),
    "plural": optional("plural"),
    "library_race": optional("bestiaryname"),
    "creature_class": optional("creatureclass"),
    "type_primary": optional("primarytype"),
    "type_secondary": optional("secondarytype"),
    "bestiary_class": optional("bestiaryclass"),
    "bestiary_level": optional("bestiarylevel"),
    "bestiary_occurrence": optional("occurrence"),
    "bosstiary_class": optional("bosstiaryclass"),
    "hitpoints": optional("hp", parse_integer),
    "experience": optional("exp", parse_integer),
    "armor": optional("armor", parse_integer),
    "mitigation": optional("mitigation", parse_integer),
    "speed": optional("speed", parse_integer),
    "runs_at": optional("runsat", parse_integer),
    "summon_cost": optional("summon", parse_integer),
    "convince_cost": optional("convince", parse_integer),
    "illusionable": optional("illusionable", parse_boolean),
    "pushable": optional("pushable", parse_boolean),
    "push_objects": optional("pushobjects", parse_boolean),
    "sees_invisible": optional("senseinvis", parse_boolean),
    "paralysable": optional("paraimmune", parse_boolean),
    "spawn_type": optional("spawntype"),
    "is_boss": optional("isboss", parse_boolean, False),
    "cooldown": optional("cooldown", parse_float),
    "modifier_physical": optional(
        "physicalDmgMod", parse_integer
    ),
    "modifier_earth": optional(
        "earthDmgMod", parse_integer
    ),
    "modifier_fire": optional("fireDmgMod", parse_integer),
    "modifier_energy": optional("iceDmgMod", parse_integer),
    "modifier_ice": optional("energyDmgMod", parse_integer),
    "modifier_death": optional(
        "deathDmgMod", parse_integer
    ),
    "modifier_holy": optional("holyDmgMod", parse_integer),
    "modifier_drown": optional(
        "drownDmgMod", parse_integer
    ),
    "modifier_lifedrain": optional(
        "hpDrainDmgMod", parse_integer
    ),
    "modifier_healing": optional("healMod", parse_integer),
    "walks_through": optional(
        "walksthrough", parse_monster_walks
    ),
    "walks_around": optional(
        "walksaround", parse_monster_walks
    ),
    "location": optional("location", clean_links),
    "version": optional("implemented"),
    "status": status(),
}

A map defining how to process every template attribute.

parse_attributes classmethod
parse_attributes(article: Article) -> dict[str, Any]

Parse the attributes of an article into a mapping.

By default, it will apply the attribute map, but it can be overridden to parse attributes in more complex ways. It is called by parse_article.

Parameters:

Name Type Description Default
article Article

The article to extract the data from.

required

Returns:

Type Description
dict[str, Any]

A dictionary containing the parsed attribute values.

Raises:

Type Description
AttributeParsingError

If the required template is not found.

Source code in tibiawikisql/parsers/creature.py
@classmethod
def parse_attributes(cls, article: Article) -> dict[str, Any]:
    row = super().parse_attributes(article)
    raw_attributes = row["_raw_attributes"]
    article_id = row["article_id"]
    if "loot" in raw_attributes:
        loot = parse_loot(raw_attributes["loot"])
        loot_items = []
        for item, amounts in loot:
            if not amounts:
                _min, _max = 0, 1
            else:
                _min, _max = parse_min_max(amounts)
            loot_items.append(
                CreatureDrop(
                    item_title=item,
                    min=_min,
                    max=_max,
                ),
            )
        row["loot"] = loot_items
    if "sounds" in raw_attributes:
        row["sounds"] = parse_sounds(raw_attributes["sounds"])
    if "abilities" in raw_attributes:
        abilities = parse_abilities(raw_attributes["abilities"])
        if abilities:
            row["abilities"] = [CreatureAbility(**ability) for ability in abilities]
    if "maxdmg" in raw_attributes:
        max_damage = parse_maximum_damage(raw_attributes["maxdmg"])
        if max_damage:
            row["max_damage"] = CreatureMaxDamage(**max_damage)
    return row

parse_maximum_damage

parse_maximum_damage(value: str) -> dict[str, int]

Parse the maximum damage template from TibiaWiki.

If no template is found, the highest number found is considered the total damage.

Parameters:

Name Type Description Default
value str

A string containing the creature's max damage.

required

Returns:

Type Description
dict[str, int]

A dictionary containing the maximum damage by element if available.

Source code in tibiawikisql/parsers/creature.py
def parse_maximum_damage(value: str) -> dict[str, int]:
    """Parse the maximum damage template from TibiaWiki.

    If no template is found, the highest number found is considered the total damage.

    Args:
        value: A string containing the creature's max damage.

    Returns:
        A dictionary containing the maximum damage by element if available.

    """
    if not value:
        return {}
    max_damage_template = find_template(value, "Max Damage")
    if not max_damage_template:
        total = parse_maximum_integer(value)
        if total is None:
            return {}
        return {"total": parse_maximum_integer(value)}
    damages = {}
    for element in max_damage_template.params:
        damages[strip_code(element.name).lower()] = parse_integer(strip_code(element.value), -1)
    excluded = {"summons", "manadrain"}
    if "total" not in damages:
        damages["total"] = sum(v for k, v in damages.items() if k not in excluded)
    return damages

parse_maximum_integer

parse_maximum_integer(value: str) -> int | None

From a string, finds the highest integer found.

Parameters:

Name Type Description Default
value str

The string containing integers.

required

Returns:

Type Description
int | None

The highest number found, or None if no number is found.

Source code in tibiawikisql/parsers/creature.py
def parse_maximum_integer(value: str) -> int | None:
    """From a string, finds the highest integer found.

    Args:
        value: The string containing integers.

    Returns:
        The highest number found, or None if no number is found.

    """
    matches = int_pattern.findall(value)
    try:
        return max(list(map(int, matches)))
    except ValueError:
        return None

parse_loot

parse_loot(value: str) -> list[tuple[str, str]]

Get every item drop entry of a creature's drops.

Parameters:

Name Type Description Default
value str

A string containing item drops.

required
Return

A list of tuples containing the name of the item and the amount dropped (or empty for 1).

Source code in tibiawikisql/parsers/creature.py
def parse_loot(value: str) -> list[tuple[str, str]]:
    """Get every item drop entry of a creature's drops.

    Args:
        value: A string containing item drops.

    Return:
        A list of tuples containing the name of the item and the amount dropped (or empty for 1).

    """

    def match(k):
        return "Item" in k.name

    loot_items_templates: Template = mwparserfromhell.parse(value).filter_templates(recursive=True,
                                                                                            matches=match)
    loot = []
    for item_template in loot_items_templates:
        param_count = len(item_template.params)
        if param_count < 2:
            loot.append((strip_code(item_template.get(1)), ""))
        else:
            second_param = strip_code(item_template.get(2))
            if second_param in ["always", "common", "uncommon", "semi-rare", "rare", "very rare"]:
                loot.append((strip_code(item_template.get(1)), ""))
            else:
                loot.append((second_param, (strip_code(item_template.get(1)))))
    return loot

parse_abilities

parse_abilities(value: str) -> list[dict[str, str]]

Parse the abilities of a creature.

Args: value: A string containing the creature's abilities definition.

Returns:

Type Description
list[dict[str, str]]

A list of dictionaries with the ability data.

Source code in tibiawikisql/parsers/creature.py
def parse_abilities(value: str) -> list[dict[str, str]]:
    """Parse the abilities of a creature.

    Args:
    value: A string containing the creature's abilities definition.

    Returns:
        A list of dictionaries with the ability data.

    """
    if not value:
        return []
    parsed = mwparserfromhell.parse(value)
    ability_list_template = find_template(value, "Ability List")
    if not ability_list_template:
        name = strip_code(parsed)
        return [{
            "name": name,
            "element": "no_template",
        }] if name else []
    abilities = []
    for element in ability_list_template.params:
        if not element.strip():
            continue
        ability_template = next(element.value.ifilter_templates(recursive=False), None)
        if not ability_template:
            abilities.append({
                "name": strip_code(element),
                "element": "plain_text",
            })
            continue
        template_name = str(ability_template.name)
        ability = None
        if template_name == "Melee":
            ability = {
                "name": ability_template.get("name", "Melee"),
                "effect": ability_template.get(1, ability_template.get("damage", "?")),
                "element": ability_template.get(2, ability_template.get("element", "physical")),
            }
        if template_name == "Summon":
            ability = {
                "name": ability_template.get(1, ability_template.get("creature", None)),
                "effect": ability_template.get(2, ability_template.get("amount", "1")),
                "element": "summon",
            }
        if template_name == "Healing":
            ability = {
                "name": ability_template.get(1, ability_template.get("name", "Self-Healing")),
                "effect": ability_template.get(2, ability_template.get("range", ability_template.get("damage", "?"))),
                "element": "healing",
            }
        if template_name == "Ability":
            ability = {
                "name": ability_template.get(1, ability_template.get("name", None)),
                "effect": ability_template.get(2, ability_template.get("damage", "?")),
                "element": ability_template.get(3, ability_template.get("element", "physical")),
            }
            if ability["name"] is None:
                ability["name"] = f'{ability["element"].title()} Damage'
        if ability:
            abilities.append(strip_code(ability))
    return abilities

parse_monster_walks

parse_monster_walks(value: str) -> str | None

Match the values against a regex to filter typos or bad data on the wiki.

Element names followed by any character that is not a comma will be considered unknown and will not be returned.

Examples:

  • Poison?, fire will return fire.
  • Poison?, fire?. will return neither.
  • Poison, earth, fire?, [[ice]] will return poison,earth.
  • No, --, >, or None will return None.

Parameters:

Name Type Description Default
value str

The string containing possible field types.

required

Returns:

Type Description
str | None

A list of field types, separated by commas.

Source code in tibiawikisql/parsers/creature.py
def parse_monster_walks(value: str) -> str | None:
    """Match the values against a regex to filter typos or bad data on the wiki.

    Element names followed by any character that is not a comma will be considered unknown and will not be returned.

    Examples:
        - ``Poison?, fire`` will return ``fire``.
        - ``Poison?, fire?.`` will return neither.
        - ``Poison, earth, fire?, [[ice]]`` will return ``poison,earth``.
        - ``No``, ``--``, ``>``, or ``None`` will return ``None``.

    Args:
        value: The string containing possible field types.

    Returns:
        A list of field types, separated by commas.

    """
    regex = re.compile(
        r"(physical)(,|$)|(holy)(,|$)|(death)(,|$)|(fire)(,|$)|(ice)(,|$)|(energy)(,|$)|(earth)(,|$)|"
        r"(poison)(,|$)")
    content = ""
    for match in re.finditer(regex, value.lower().strip()):
        content += match.group()
    if not content:
        return None
    return content

tibiawikisql.parsers.house

Classes:

Name Description
HouseParser

Parses houses and guildhalls.

HouseParser

Bases: BaseParser

Parses houses and guildhalls.

Attributes:

Name Type Description
model

The model to convert the data into.

table

The SQL table where the data wil be stored.

template_name

The name of the template that contains the information.

attribute_map ClassVar

A map defining how to process every template attribute.

model class-attribute instance-attribute
model = House

The model to convert the data into.

table class-attribute instance-attribute
table = HouseTable

The SQL table where the data wil be stored.

template_name class-attribute instance-attribute
template_name = 'Infobox_Building'

The name of the template that contains the information.

attribute_map class-attribute instance-attribute
attribute_map: ClassVar = {
    "house_id": required("houseid", parse_integer),
    "name": required("name"),
    "is_guildhall": required(
        "type",
        lambda x: x is not None and "guildhall" in lower(),
    ),
    "city": required("city"),
    "street": optional("street"),
    "location": optional("location", clean_links),
    "beds": required("beds", parse_integer),
    "rent": required("rent", parse_integer),
    "size": required("size", parse_integer),
    "rooms": optional("rooms", parse_integer),
    "floors": optional("floors", parse_integer),
    "x": optional("posx", convert_tibiawiki_position),
    "y": optional("posy", convert_tibiawiki_position),
    "z": optional("posz", int),
    "version": version(),
    "status": status(),
}

A map defining how to process every template attribute.

tibiawikisql.parsers.imbuement

Classes:

Name Description
ImbuementParser

Parses imbuements.

Functions:

Name Description
parse_astral_sources

Parse the astral sources of an imbuement.

parse_effect

Parse TibiaWiki's effect template into a string effect.

parse_slots

Parse the list of slots.

ImbuementParser

Bases: BaseParser

Parses imbuements.

Methods:

Name Description
parse_attributes

Parse the attributes of an article into a mapping.

Attributes:

Name Type Description
model

The model to convert the data into.

table

The SQL table where the data wil be stored.

template_name

The name of the template that contains the information.

attribute_map ClassVar

A map defining how to process every template attribute.

model class-attribute instance-attribute
model = Imbuement

The model to convert the data into.

table class-attribute instance-attribute

The SQL table where the data wil be stored.

template_name class-attribute instance-attribute
template_name = 'Infobox_Imbuement'

The name of the template that contains the information.

attribute_map class-attribute instance-attribute
attribute_map: ClassVar = {
    "name": required("name"),
    "tier": required("prefix"),
    "type": required("type"),
    "category": required("category"),
    "effect": required("effect", parse_effect),
    "version": required("implemented"),
    "slots": required("slots", parse_slots),
    "status": status(),
}

A map defining how to process every template attribute.

parse_attributes classmethod
parse_attributes(article: Article) -> dict[str, Any]

Parse the attributes of an article into a mapping.

By default, it will apply the attribute map, but it can be overridden to parse attributes in more complex ways. It is called by parse_article.

Parameters:

Name Type Description Default
article Article

The article to extract the data from.

required

Returns:

Type Description
dict[str, Any]

A dictionary containing the parsed attribute values.

Raises:

Type Description
AttributeParsingError

If the required template is not found.

Source code in tibiawikisql/parsers/imbuement.py
@classmethod
def parse_attributes(cls, article: Article) -> dict[str, Any]:
    row = super().parse_attributes(article)
    if not row:
        return row
    raw_attributes = row["_raw_attributes"]
    if "astralsources" in raw_attributes:
        materials = parse_astral_sources(raw_attributes["astralsources"])
        row["materials"] = [Material(item_title=name, amount=amount) for name, amount in materials.items()]
    return row

parse_astral_sources

parse_astral_sources(content: str) -> dict[str, int]

Parse the astral sources of an imbuement.

Parameters:

Name Type Description Default
content str

A string containing astral sources.

required

Returns:

Type Description
dict[str, int]

A dictionary containing the material name and te amount required.

Source code in tibiawikisql/parsers/imbuement.py
def parse_astral_sources(content: str) -> dict[str, int]:
    """Parse the astral sources of an imbuement.

    Args:
         content: A string containing astral sources.

    Returns:
        A dictionary containing the material name and te amount required.

    """
    materials = astral_pattern.findall(content)
    if materials:
        return {item: int(amount) for (item, amount) in materials}
    return {}

parse_effect

parse_effect(effect: str) -> str

Parse TibiaWiki's effect template into a string effect.

Parameters:

Name Type Description Default
effect str

The string containing the template.

required

Returns:

Type Description
str

The effect string.

Source code in tibiawikisql/parsers/imbuement.py
def parse_effect(effect: str) -> str:
    """Parse TibiaWiki's effect template into a string effect.

    Args:
        effect: The string containing the template.

    Returns:
        The effect string.

    """
    m = effect_pattern.search(effect)
    category, amount = m.groups()
    try:
        return effect_map[category].format(amount)
    except KeyError:
        return f"{category} {amount}"

parse_slots

parse_slots(content: str) -> str

Parse the list of slots.

Cleans up spaces between items.

Parameters:

Name Type Description Default
content str

A string containing comma separated values.

required

Returns:

Type Description
str

The slots string.

Source code in tibiawikisql/parsers/imbuement.py
def parse_slots(content: str) -> str:
    """Parse the list of slots.

    Cleans up spaces between items.

    Args:
        content: A string containing comma separated values.

    Returns:
        The slots string.

    """
    slots = content.split(",")
    return ",".join(s.strip() for s in slots)

tibiawikisql.parsers.item

Classes:

Name Description
ItemParser

Parses items and objects.

ItemParser

Bases: BaseParser

Parses items and objects.

Methods:

Name Description
parse_attributes

Parse the attributes of an article into a mapping.

Attributes:

Name Type Description
model

The model to convert the data into.

table

The SQL table where the data wil be stored.

template_name

The name of the template that contains the information.

attribute_map ClassVar

A map defining how to process every template attribute.

model class-attribute instance-attribute
model = Item

The model to convert the data into.

table class-attribute instance-attribute
table = ItemTable

The SQL table where the data wil be stored.

template_name class-attribute instance-attribute
template_name = 'Infobox_Object'

The name of the template that contains the information.

attribute_map class-attribute instance-attribute
attribute_map: ClassVar = {
    "name": required("name"),
    "actual_name": optional("actualname"),
    "plural": optional("plural", clean_question_mark),
    "article": optional("article"),
    "is_marketable": optional(
        "marketable", parse_boolean, False
    ),
    "is_stackable": optional(
        "stackable", parse_boolean, False
    ),
    "is_pickupable": optional(
        "pickupable", parse_boolean, False
    ),
    "is_immobile": optional(
        "immobile", parse_boolean, False
    ),
    "value_sell": optional("npcvalue", parse_integer),
    "value_buy": optional("npcprice", parse_integer),
    "weight": optional("weight", parse_float),
    "flavor_text": optional("flavortext"),
    "item_class": optional("objectclass"),
    "item_type": optional("primarytype"),
    "type_secondary": optional("secondarytype"),
    "light_color": optional(
        "lightcolor",
        lambda x: client_color_to_rgb(parse_integer(x)),
    ),
    "light_radius": optional("lightradius", parse_integer),
    "version": optional("implemented"),
    "client_id": optional("itemid", parse_integer),
    "status": status(),
}

A map defining how to process every template attribute.

parse_attributes classmethod
parse_attributes(article: Article) -> dict[str, Any]

Parse the attributes of an article into a mapping.

By default, it will apply the attribute map, but it can be overridden to parse attributes in more complex ways. It is called by parse_article.

Parameters:

Name Type Description Default
article Article

The article to extract the data from.

required

Returns:

Type Description
dict[str, Any]

A dictionary containing the parsed attribute values.

Raises:

Type Description
AttributeParsingError

If the required template is not found.

Source code in tibiawikisql/parsers/item.py
@classmethod
def parse_attributes(cls, article: Article) -> dict[str, Any]:
    row = super().parse_attributes(article)
    row["attributes"] = []
    for name, attribute in cls.item_attributes.items():
        if attribute in row["_raw_attributes"] and row["_raw_attributes"][attribute]:
            row["attributes"].append(ItemAttribute(
                name=name,
                value=clean_links(row["_raw_attributes"][attribute]),
            ))
    cls.parse_item_attributes(row)
    cls.parse_resistances(row)
    cls.parse_sounds(row)
    cls.parse_store_value(row)
    return row

tibiawikisql.parsers.key

Classes:

Name Description
KeyParser

Parser for keys.

KeyParser

Bases: BaseParser

Parser for keys.

Attributes:

Name Type Description
model

The model to convert the data into.

table

The SQL table where the data wil be stored.

template_name

The name of the template that contains the information.

attribute_map ClassVar

A map defining how to process every template attribute.

model class-attribute instance-attribute
model = Key

The model to convert the data into.

table class-attribute instance-attribute
table = ItemKeyTable

The SQL table where the data wil be stored.

template_name class-attribute instance-attribute
template_name = 'Infobox_Key'

The name of the template that contains the information.

attribute_map class-attribute instance-attribute
attribute_map: ClassVar = {
    "name": optional("aka", clean_links),
    "number": optional("number", parse_integer),
    "material": optional("primarytype"),
    "location": optional("location", clean_links),
    "notes": optional("shortnotes", clean_links),
    "origin": optional("origin", clean_links),
    "status": status(),
    "version": optional("implemented", clean_links),
}

A map defining how to process every template attribute.

tibiawikisql.parsers.mount

Classes:

Name Description
MountParser

Parser for mounts.

Functions:

Name Description
remove_mount

Remove "(Mount)" from the name, if found.

MountParser

Bases: BaseParser

Parser for mounts.

Attributes:

Name Type Description
model

The model to convert the data into.

table

The SQL table where the data wil be stored.

template_name

The name of the template that contains the information.

attribute_map ClassVar

A map defining how to process every template attribute.

model class-attribute instance-attribute
model = Mount

The model to convert the data into.

table class-attribute instance-attribute
table = MountTable

The SQL table where the data wil be stored.

template_name class-attribute instance-attribute
template_name = 'Infobox_Mount'

The name of the template that contains the information.

attribute_map class-attribute instance-attribute
attribute_map: ClassVar = {
    "name": required("name", remove_mount),
    "speed": required("speed", int),
    "taming_method": required("taming_method", clean_links),
    "is_buyable": optional("bought", parse_boolean, False),
    "price": optional("price", parse_integer),
    "achievement": optional("achievement"),
    "light_color": optional(
        "lightcolor",
        lambda x: client_color_to_rgb(parse_integer(x)),
    ),
    "light_radius": optional("lightradius", int),
    "version": version(),
    "status": status(),
}

A map defining how to process every template attribute.

remove_mount

remove_mount(name: str) -> str

Remove "(Mount)" from the name, if found.

Parameters:

Name Type Description Default
name str

The name to check.

required

Returns:

Type Description
str

The name with "(Mount)" removed from it.

Source code in tibiawikisql/parsers/mount.py
def remove_mount(name: str) -> str:
    """Remove "(Mount)" from the name, if found.

    Args:
        name: The name to check.

    Returns:
        The name with "(Mount)" removed from it.

    """
    return name.replace("(Mount)", "").strip()

tibiawikisql.parsers.npc

Classes:

Name Description
NpcParser

Parser for NPCs.

NpcParser

Bases: BaseParser

Parser for NPCs.

Methods:

Name Description
parse_attributes

Parse the attributes of an article into a mapping.

Attributes:

Name Type Description
table

The SQL table where the data wil be stored.

model

The model to convert the data into.

template_name

The name of the template that contains the information.

attribute_map ClassVar

A map defining how to process every template attribute.

table class-attribute instance-attribute
table = NpcTable

The SQL table where the data wil be stored.

model class-attribute instance-attribute
model = Npc

The model to convert the data into.

template_name class-attribute instance-attribute
template_name = 'Infobox_NPC'

The name of the template that contains the information.

attribute_map class-attribute instance-attribute
attribute_map: ClassVar = {
    "name": AttributeParser(
        lambda x: get("actualname") or get("name")
    ),
    "gender": optional("gender"),
    "location": optional("location", clean_links),
    "subarea": optional("subarea"),
    "city": required("city"),
    "x": optional("posx", convert_tibiawiki_position),
    "y": optional("posy", convert_tibiawiki_position),
    "z": optional("posz", int),
    "version": optional("implemented"),
    "status": status(),
}

A map defining how to process every template attribute.

parse_attributes classmethod
parse_attributes(article: Article) -> dict[str, Any]

Parse the attributes of an article into a mapping.

By default, it will apply the attribute map, but it can be overridden to parse attributes in more complex ways. It is called by parse_article.

Parameters:

Name Type Description Default
article Article

The article to extract the data from.

required

Returns:

Type Description
dict[str, Any]

A dictionary containing the parsed attribute values.

Raises:

Type Description
AttributeParsingError

If the required template is not found.

Source code in tibiawikisql/parsers/npc.py
@classmethod
def parse_attributes(cls, article: Article) -> dict[str, Any]:
    row = super().parse_attributes(article)
    raw_attributes = row["_raw_attributes"]
    cls._parse_jobs(row)
    cls._parse_races(row)

    row["destinations"] = []
    destinations = []
    if "notes" in raw_attributes and "{{Transport" in raw_attributes["notes"]:
        destinations.extend(cls._parse_destinations(raw_attributes["notes"]))
    for destination, price, notes in destinations:
        name = destination.strip()
        clean_notes = clean_links(notes.strip())
        if not notes:
            clean_notes = None
        row["destinations"].append(NpcDestination(
            name=name,
            price=price,
            notes=clean_notes,
        ))
    return row

tibiawikisql.parsers.outfit

Classes:

Name Description
OutfitParser

Parser for outfits.

OutfitParser

Bases: BaseParser

Parser for outfits.

Methods:

Name Description
parse_attributes

Parse the attributes of an article into a mapping.

Attributes:

Name Type Description
model

The model to convert the data into.

table

The SQL table where the data wil be stored.

template_name

The name of the template that contains the information.

attribute_map ClassVar

A map defining how to process every template attribute.

model class-attribute instance-attribute
model = Outfit

The model to convert the data into.

table class-attribute instance-attribute
table = OutfitTable

The SQL table where the data wil be stored.

template_name class-attribute instance-attribute
template_name = 'Infobox_Outfit'

The name of the template that contains the information.

attribute_map class-attribute instance-attribute
attribute_map: ClassVar = {
    "name": required("name"),
    "outfit_type": required("primarytype"),
    "is_premium": optional("premium", parse_boolean, False),
    "is_tournament": optional(
        "tournament", parse_boolean, False
    ),
    "is_bought": optional("bought", parse_boolean, False),
    "full_price": optional(
        "fulloutfitprice", parse_integer
    ),
    "achievement": optional("achievement"),
    "status": status(),
    "version": version(),
}

A map defining how to process every template attribute.

parse_attributes classmethod
parse_attributes(article: Article) -> dict[str, Any]

Parse the attributes of an article into a mapping.

By default, it will apply the attribute map, but it can be overridden to parse attributes in more complex ways. It is called by parse_article.

Parameters:

Name Type Description Default
article Article

The article to extract the data from.

required

Returns:

Type Description
dict[str, Any]

A dictionary containing the parsed attribute values.

Raises:

Type Description
AttributeParsingError

If the required template is not found.

Source code in tibiawikisql/parsers/outfit.py
@classmethod
def parse_attributes(cls, article: Article) -> dict[str, Any]:
    row = super().parse_attributes(article)
    if not row:
        return row
    raw_attributes = row["_raw_attributes"]
    row["quests"] = []
    if "outfit" in raw_attributes:
        quests = parse_links(raw_attributes["outfit"])
        for quest in quests:
            row["quests"].append(UnlockQuest(
                quest_title=quest.strip(),
                unlock_type="outfit",
            ))
    if "addons" in raw_attributes:
        quests = parse_links(raw_attributes["addons"])
        for quest in quests:
            row["quests"].append(UnlockQuest(
                quest_title=quest.strip(),
                unlock_type="addons",
            ))
    return row

tibiawikisql.parsers.quest

Classes:

Name Description
QuestParser

Parser for quests.

Functions:

Name Description
parse_links

Find all the links in a string and returns a list of them.

QuestParser

Bases: BaseParser

Parser for quests.

Methods:

Name Description
parse_attributes

Parse the attributes of an article into a mapping.

Attributes:

Name Type Description
model

The model to convert the data into.

table

The SQL table where the data wil be stored.

template_name

The name of the template that contains the information.

attribute_map ClassVar

A map defining how to process every template attribute.

model class-attribute instance-attribute
model = Quest

The model to convert the data into.

table class-attribute instance-attribute
table = QuestTable

The SQL table where the data wil be stored.

template_name class-attribute instance-attribute
template_name = 'Infobox_Quest'

The name of the template that contains the information.

attribute_map class-attribute instance-attribute
attribute_map: ClassVar = {
    "name": required("name", unescape),
    "location": optional("location", clean_links),
    "is_rookgaard_quest": optional(
        "rookgaardquest", parse_boolean, False
    ),
    "type": optional("type"),
    "quest_log": optional("log", parse_boolean),
    "legend": optional("legend", clean_links),
    "level_required": optional("lvl", parse_integer),
    "level_recommended": optional("lvlrec", parse_integer),
    "active_time": optional("time"),
    "estimated_time": optional("timealloc"),
    "is_premium": required("premium", parse_boolean),
    "version": optional("implemented"),
    "status": status(),
}

A map defining how to process every template attribute.

parse_attributes classmethod
parse_attributes(article: Article) -> dict[str, Any]

Parse the attributes of an article into a mapping.

By default, it will apply the attribute map, but it can be overridden to parse attributes in more complex ways. It is called by parse_article.

Parameters:

Name Type Description Default
article Article

The article to extract the data from.

required

Returns:

Type Description
dict[str, Any]

A dictionary containing the parsed attribute values.

Raises:

Type Description
AttributeParsingError

If the required template is not found.

Source code in tibiawikisql/parsers/quest.py
@classmethod
def parse_attributes(cls, article: Article) -> dict[str, Any]:
    row = super().parse_attributes(article)
    if not row:
        return row
    cls._parse_quest_rewards(row)
    cls._parse_quest_dangers(row)
    return row
parse_links(value: str) -> list[str]

Find all the links in a string and returns a list of them.

Parameters:

Name Type Description Default
value str

A string containing links.

required

Returns:

Type Description
list[str]

The links found in the string.

Source code in tibiawikisql/parsers/quest.py
def parse_links(value: str) -> list[str]:
    """Find all the links in a string and returns a list of them.

    Args:
        value: A string containing links.

    Returns:
        The links found in the string.

    """
    return list(link_pattern.findall(value))

tibiawikisql.parsers.spell

Classes:

Name Description
SpellParser

Parser for spells.

SpellParser

Bases: BaseParser

Parser for spells.

Methods:

Name Description
parse_attributes

Parse the attributes of an article into a mapping.

Attributes:

Name Type Description
model

The model to convert the data into.

table

The SQL table where the data wil be stored.

template_name

The name of the template that contains the information.

attribute_map ClassVar

A map defining how to process every template attribute.

model class-attribute instance-attribute
model = Spell

The model to convert the data into.

table class-attribute instance-attribute
table = SpellTable

The SQL table where the data wil be stored.

template_name class-attribute instance-attribute
template_name = 'Infobox_Spell'

The name of the template that contains the information.

attribute_map class-attribute instance-attribute
attribute_map: ClassVar = {
    "name": required("name"),
    "effect": required("effect", clean_links),
    "words": optional("words"),
    "spell_type": required("type"),
    "group_spell": required("subclass"),
    "group_secondary": optional("secondarygroup"),
    "group_rune": optional("runegroup"),
    "element": optional("damagetype"),
    "mana": optional("mana", parse_integer),
    "soul": optional("soul", parse_integer, 0),
    "price": optional("spellcost", parse_integer),
    "cooldown": required("cooldown"),
    "cooldown2": optional("cooldown2"),
    "cooldown3": optional("cooldown3"),
    "cooldown_group": optional("cooldowngroup"),
    "cooldown_group_secondary": optional("cooldowngroup2"),
    "level": optional("levelrequired", parse_integer),
    "is_premium": optional("premium", parse_boolean, False),
    "is_promotion": optional(
        "promotion", parse_boolean, False
    ),
    "is_wheel_spell": optional(
        "wheelspell", parse_boolean, False
    ),
    "is_passive": optional(
        "passivespell", parse_boolean, False
    ),
    "version": optional("implemented"),
    "status": status(),
}

A map defining how to process every template attribute.

parse_attributes classmethod
parse_attributes(article: Article)

Parse the attributes of an article into a mapping.

By default, it will apply the attribute map, but it can be overridden to parse attributes in more complex ways. It is called by parse_article.

Parameters:

Name Type Description Default
article Article

The article to extract the data from.

required

Returns:

Type Description
dict[str, Any]

A dictionary containing the parsed attribute values.

Raises:

Type Description
AttributeParsingError

If the required template is not found.

Source code in tibiawikisql/parsers/spell.py
@classmethod
def parse_attributes(cls, article: Article):
    row = super().parse_attributes(article)
    for vocation in ["knight", "sorcerer", "druid", "paladin", "monk"]:
        if vocation in row["_raw_attributes"].get("voc", "").lower():
            row[vocation] = True
    return row

tibiawikisql.parsers.update

Classes:

Name Description
UpdateParser

Parser for game updates.

UpdateParser

Bases: BaseParser

Parser for game updates.

Attributes:

Name Type Description
model

The model to convert the data into.

table

The SQL table where the data wil be stored.

template_name

The name of the template that contains the information.

attribute_map ClassVar

A map defining how to process every template attribute.

model class-attribute instance-attribute
model = Update

The model to convert the data into.

table class-attribute instance-attribute
table = UpdateTable

The SQL table where the data wil be stored.

template_name class-attribute instance-attribute
template_name = 'Infobox_Update'

The name of the template that contains the information.

attribute_map class-attribute instance-attribute
attribute_map: ClassVar = {
    "name": optional("name"),
    "type_primary": required("primarytype"),
    "type_secondary": optional("secondarytype"),
    "release_date": required("date", parse_date),
    "news_id": optional("newsid", parse_integer),
    "previous": optional("previous"),
    "next": optional("next"),
    "summary": optional("summary", clean_links),
    "changes": optional("changelist", clean_links),
    "version": version(),
}

A map defining how to process every template attribute.

tibiawikisql.parsers.world

Classes:

Name Description
WorldParser

Parser for game worlds (servers).

WorldParser

Bases: BaseParser

Parser for game worlds (servers).

Attributes:

Name Type Description
table

The SQL table where the data wil be stored.

model

The model to convert the data into.

template_name

The name of the template that contains the information.

attribute_map ClassVar

A map defining how to process every template attribute.

table class-attribute instance-attribute
table = WorldTable

The SQL table where the data wil be stored.

model class-attribute instance-attribute
model = World

The model to convert the data into.

template_name class-attribute instance-attribute
template_name = 'Infobox_World'

The name of the template that contains the information.

attribute_map class-attribute instance-attribute
attribute_map: ClassVar = {
    "name": required("name"),
    "location": required("location"),
    "pvp_type": required("type"),
    "is_preview": optional("preview", parse_boolean, False),
    "is_experimental": optional(
        "experimental", parse_boolean, False
    ),
    "online_since": required("online", parse_date),
    "offline_since": optional("offline", parse_date),
    "merged_into": optional("mergedinto"),
    "battleye": optional("battleye", parse_boolean, False),
    "battleye_type": optional("battleyetype"),
    "protected_since": optional(
        "protectedsince", parse_date
    ),
    "world_board": optional("worldboardid", parse_integer),
    "trade_board": optional("tradeboardid", parse_integer),
}

A map defining how to process every template attribute.