From 8db0eefdae4ba11dd697a3da7ec56a0cb88a03a9 Mon Sep 17 00:00:00 2001 From: offish Date: Tue, 26 Sep 2023 19:21:56 +0200 Subject: [PATCH] v2.0.1 (#4) * rewrite and new version * small fixes and documentation * update version number * fix bug with wrong skus --- README.md | 4 +-- src/tf2_sku/__init__.py | 4 +-- src/tf2_sku/sku.py | 67 ++++++++++++++++++++++------------------- src/tf2_sku/utils.py | 15 +++++++-- tests/test_sku.py | 22 +++++++++++++- 5 files changed, 74 insertions(+), 38 deletions(-) diff --git a/README.md b/README.md index 48ee508..1499865 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,6 @@ Parse TF2 items to SKU format with Python. "crate_number": -1, "output_defindex": -1, "output_quality": -1, - "paint": -1, } # https://marketplace.tf/items/tf2/161;3;kt-3 @@ -48,7 +47,8 @@ Parse TF2 items to SKU format with Python. ... "wear": 3, ... "skin": 292, ... "strange": True, -... "killstreak_tier": 3}) +... "killstreak_tier": 3 +... }) "199;5;u702;w3;pk292;strange;kt-3" # https://marketplace.tf/items/tf2/199;5;u702;w3;pk292;strange;kt-3 ``` diff --git a/src/tf2_sku/__init__.py b/src/tf2_sku/__init__.py index 77d6852..63e2b50 100644 --- a/src/tf2_sku/__init__.py +++ b/src/tf2_sku/__init__.py @@ -6,7 +6,7 @@ __title__ = "tf2-sku" __author__ = "offish" __license__ = "MIT" -__version__ = "2.0.0" +__version__ = "2.0.1" from .sku import to_sku, from_sku -from .utils import SKU, MAPPING +from .utils import SKU, MAPPING, get_key_from_value, has_string_value diff --git a/src/tf2_sku/sku.py b/src/tf2_sku/sku.py index a952291..5152371 100644 --- a/src/tf2_sku/sku.py +++ b/src/tf2_sku/sku.py @@ -1,14 +1,14 @@ -from .utils import SKU, MAPPING +from .utils import SKU, MAPPING, has_string_value, get_key_from_value def to_sku(item: dict) -> str: """Takes an item dictionary and formats it to a SKU. - :param item: Item dictionary containing specific keys and values - :type item: dict + Args: + Item dictionary containing specific keys and values. - :return: SKU string - :rtype: str + Returns: + SKU string for given item. Example: to_sku({ @@ -26,8 +26,7 @@ def to_sku(item: dict) -> str: "craft_number": -1, "crate_number": -1, "output_defindex": -1, - "output_quality": -1, - "paint": -1, + "output_quality": -1 }) """ sku = SKU(**item) @@ -37,38 +36,44 @@ def to_sku(item: dict) -> str: def from_sku(sku: str) -> dict: """Takes a SKU and formats it to an item dictionary. - :param sku: SKU string - :type sku: str + Args: + SKU string. E.g. `199;5;u702;w3;pk292;strange;kt-3` - :return: Item dictionary containing specific keys and values - :rtype: dict - - Example: - from_sku("199;5;u702;w3;pk292;strange;kt-3") + Returns: + Item dictionary containing item properties. """ - parts = sku.split(";") - item = {"defindex": int(parts[0]), "quality": int(parts[1])} - - for part in parts[2:]: - for key in MAPPING: - default = MAPPING[key].format("") - - if not default in part: + sku_parts = sku.split(";") + item = {"defindex": int(sku_parts[0]), "quality": int(sku_parts[1])} + + # loop through each part of the sku + # -> u702, w3, pk292, strange, kt-3 + for sku_value in sku_parts[2:]: + # loop through every key + # effect, australium, craftable + for map_key in MAPPING: + default = MAPPING[map_key].format("") + + # dont override + if map_key in item: continue - value = part.replace(default, "") + # no chance this is the right key -> skip + if not default in sku_value: + continue - if key == "craftable": - value = False + value = None + # check special keys like uncraftable, australium, strange and festive + if has_string_value(sku_value): + # get the correct key + map_key = get_key_from_value(sku_value) + # craftable is false, everything else true + value = True if sku_value != "uncraftable" else False else: - if not value: - value = True - - else: - value = int(value) + # replace the difference + value = int(sku_value.replace(default, "")) - item[key] = value + item[map_key] = value break sku = SKU(**item) diff --git a/src/tf2_sku/utils.py b/src/tf2_sku/utils.py index 08c33af..a4ab208 100644 --- a/src/tf2_sku/utils.py +++ b/src/tf2_sku/utils.py @@ -15,7 +15,7 @@ "crate_number": "c{}", "output_defindex": "od-{}", "output_quality": "oq-{}", - "paint": "p", + # "paint": "p", # "sheen": "ks-{}", # "killstreaker": "ke-{}", } @@ -38,7 +38,7 @@ class SKU: crate_number: int = -1 output_defindex: int = -1 output_quality: int = -1 - paint: int = -1 + # paint: int = -1 # sheen: int = -1 # killstreaker: int = -1 @@ -55,3 +55,14 @@ def __str__(self) -> str: sku += ";" + MAPPING[field.name].format(value) return sku + + +def get_key_from_value(value: str) -> str: + for key in MAPPING: + if value == MAPPING[key]: + return key + return "" + + +def has_string_value(sku_value: str) -> bool: + return sku_value in ["uncraftable", "australium", "strange", "festive"] diff --git a/tests/test_sku.py b/tests/test_sku.py index 9f36443..31fc349 100644 --- a/tests/test_sku.py +++ b/tests/test_sku.py @@ -19,7 +19,6 @@ "crate_number": -1, "output_defindex": -1, "output_quality": -1, - "paint": -1, } @@ -52,6 +51,27 @@ def test_ellis_cap_double(self): self.assertEqual(sku, sku_again) + def test_uncraftable(self): + orginal = "6526;6;uncraftable;kt-3;td-205" + item = from_sku(orginal) + sku = to_sku(item) + + self.assertEqual(sku, orginal) + + def test_uncraftable_festive(self): + orginal = "208;11;australium;kt-3;festive" + item = from_sku(orginal) + sku = to_sku(item) + + self.assertEqual(sku, orginal) + + def test_australium_festive(self): + orginal = "206;11;australium;festive" + item = from_sku(orginal) + sku = to_sku(item) + + self.assertEqual(sku, orginal) + def test_skin(self): original = "199;5;u702;w3;pk292;strange;kt-3" item = from_sku(original)