diff --git a/mcstatus/pinger.py b/mcstatus/pinger.py index c683905..8b2fe83 100644 --- a/mcstatus/pinger.py +++ b/mcstatus/pinger.py @@ -253,10 +253,13 @@ class PingResponse: for entry in entries: for style_key, style_val in STYLE_MAP.items(): if entry.get(style_key): - if isinstance(style_val, dict): - style_val = style_val[entry[style_key]] + try: + if isinstance(style_val, dict): + style_val = style_val[entry[style_key]] - description += f"§{style_val}" + description += f"§{style_val}" + except KeyError: + pass # ignoring these key errors strips out html color codes description += entry.get("text", "") return description + end diff --git a/mcstatus/tests/test_pinger.py b/mcstatus/tests/test_pinger.py index cb7022d..cafc259 100644 --- a/mcstatus/tests/test_pinger.py +++ b/mcstatus/tests/test_pinger.py @@ -110,6 +110,65 @@ class TestPingResponse: } ) + def test_parse_description_strips_html_color_codes(self): + out = PingResponse._parse_description( + { + "extra": [ + {"text": " "}, + {"strikethrough": True, "color": "#b3eeff", "text": "="}, + {"strikethrough": True, "color": "#b9ecff", "text": "="}, + {"strikethrough": True, "color": "#c0eaff", "text": "="}, + {"strikethrough": True, "color": "#c7e8ff", "text": "="}, + {"strikethrough": True, "color": "#cee6ff", "text": "="}, + {"strikethrough": True, "color": "#d5e4ff", "text": "="}, + {"strikethrough": True, "color": "#dce2ff", "text": "="}, + {"strikethrough": True, "color": "#e3e0ff", "text": "="}, + {"strikethrough": True, "color": "#eadeff", "text": "="}, + {"strikethrough": True, "color": "#f1dcff", "text": "="}, + {"strikethrough": True, "color": "#f8daff", "text": "="}, + {"strikethrough": True, "color": "#ffd9ff", "text": "="}, + {"strikethrough": True, "color": "#f4dcff", "text": "="}, + {"strikethrough": True, "color": "#f9daff", "text": "="}, + {"strikethrough": True, "color": "#ffd9ff", "text": "="}, + {"color": "white", "text": " "}, + {"bold": True, "color": "#66ff99", "text": "C"}, + {"bold": True, "color": "#75f5a2", "text": "r"}, + {"bold": True, "color": "#84ebab", "text": "e"}, + {"bold": True, "color": "#93e2b4", "text": "a"}, + {"bold": True, "color": "#a3d8bd", "text": "t"}, + {"bold": True, "color": "#b2cfc6", "text": "i"}, + {"bold": True, "color": "#c1c5cf", "text": "v"}, + {"bold": True, "color": "#d1bbd8", "text": "e"}, + {"bold": True, "color": "#e0b2e1", "text": "F"}, + {"bold": True, "color": "#efa8ea", "text": "u"}, + {"bold": True, "color": "#ff9ff4", "text": "n "}, + {"strikethrough": True, "color": "#b3eeff", "text": "="}, + {"strikethrough": True, "color": "#b9ecff", "text": "="}, + {"strikethrough": True, "color": "#c0eaff", "text": "="}, + {"strikethrough": True, "color": "#c7e8ff", "text": "="}, + {"strikethrough": True, "color": "#cee6ff", "text": "="}, + {"strikethrough": True, "color": "#d5e4ff", "text": "="}, + {"strikethrough": True, "color": "#dce2ff", "text": "="}, + {"strikethrough": True, "color": "#e3e0ff", "text": "="}, + {"strikethrough": True, "color": "#eadeff", "text": "="}, + {"strikethrough": True, "color": "#f1dcff", "text": "="}, + {"strikethrough": True, "color": "#f8daff", "text": "="}, + {"strikethrough": True, "color": "#ffd9ff", "text": "="}, + {"strikethrough": True, "color": "#f4dcff", "text": "="}, + {"strikethrough": True, "color": "#f9daff", "text": "="}, + {"strikethrough": True, "color": "#ffd9ff", "text": "="}, + {"color": "white", "text": " \n "}, + {"bold": True, "color": "#E5E5E5", "text": "The server has been updated to "}, + {"bold": True, "color": "#97ABFF", "text": "1.17.1"}, + ], + "text": "", + } + ) + assert ( + out + == " ===============§f §lC§lr§le§la§lt§li§lv§le§lF§lu§ln ===============§f \n §lThe server has been updated to §l1.17.1" + ) + def test_parse_description(self): out = PingResponse._parse_description("test §2description") assert out == "test §2description"