Skip to content

Commit

Permalink
v2.0.1 (#4)
Browse files Browse the repository at this point in the history
* rewrite and new version

* small fixes and documentation

* update version number

* fix bug with wrong skus
  • Loading branch information
offish authored Sep 26, 2023
1 parent 205d682 commit 8db0eef
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 38 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
```
Expand Down
4 changes: 2 additions & 2 deletions src/tf2_sku/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
67 changes: 36 additions & 31 deletions src/tf2_sku/sku.py
Original file line number Diff line number Diff line change
@@ -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({
Expand All @@ -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)
Expand All @@ -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)
Expand Down
15 changes: 13 additions & 2 deletions src/tf2_sku/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"crate_number": "c{}",
"output_defindex": "od-{}",
"output_quality": "oq-{}",
"paint": "p",
# "paint": "p",
# "sheen": "ks-{}",
# "killstreaker": "ke-{}",
}
Expand All @@ -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

Expand All @@ -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"]
22 changes: 21 additions & 1 deletion tests/test_sku.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
"crate_number": -1,
"output_defindex": -1,
"output_quality": -1,
"paint": -1,
}


Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 8db0eef

Please sign in to comment.