Skip to content

Commit

Permalink
Fix wrong utf-8 encoding with ftfy
Browse files Browse the repository at this point in the history
  • Loading branch information
ol-iver committed Feb 4, 2024
1 parent 30b9918 commit 9adf799
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
20 changes: 10 additions & 10 deletions denonavr/input.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
"""

import asyncio
import html
import logging
from copy import deepcopy
from typing import Dict, Hashable, List, Optional, Set, Tuple

import attr
import httpx
from ftfy import fix_text

from .appcommand import AppCommands
from .const import (
Expand Down Expand Up @@ -56,11 +56,11 @@ def lower_string(value: Optional[str]) -> Optional[str]:
return str(value).lower()


def unescape_string(value: Optional[str]) -> Optional[str]:
"""Perform HTML unescape on value."""
def fix_string(value: Optional[str]) -> Optional[str]:
"""Fix errors in string like unescaped HTML and wrong utf-8 encoding."""
if value is None:
return value
return html.unescape(str(value)).strip("\x00").strip("\x01").strip()
return fix_text(str(value)).strip()


def set_input_func(
Expand Down Expand Up @@ -143,22 +143,22 @@ class DenonAVRInput(DenonAVRFoundation):
)

_artist: Optional[str] = attr.ib(
converter=attr.converters.optional(unescape_string), default=None
converter=attr.converters.optional(fix_string), default=None
)
_album: Optional[str] = attr.ib(
converter=attr.converters.optional(unescape_string), default=None
converter=attr.converters.optional(fix_string), default=None
)
_band: Optional[str] = attr.ib(
converter=attr.converters.optional(unescape_string), default=None
converter=attr.converters.optional(fix_string), default=None
)
_title: Optional[str] = attr.ib(
converter=attr.converters.optional(unescape_string), default=None
converter=attr.converters.optional(fix_string), default=None
)
_frequency: Optional[str] = attr.ib(
converter=attr.converters.optional(unescape_string), default=None
converter=attr.converters.optional(fix_string), default=None
)
_station: Optional[str] = attr.ib(
converter=attr.converters.optional(unescape_string), default=None
converter=attr.converters.optional(fix_string), default=None
)
_image_url: Optional[str] = attr.ib(
converter=attr.converters.optional(str), default=None
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ dependencies = [
"asyncstdlib>=3.10.2",
"attrs>=21.2.0",
"defusedxml>=0.7.1",
"ftfy>=6.1.1",
"httpx>=0.23.1",
"netifaces>=0.11.0",
'async-timeout>=4.0.2; python_version < "3.11"',
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
asyncstdlib>=3.10.2
attrs>=21.2.0
defusedxml>=0.7.1
ftfy>=6.1.1,
httpx>=0.21.0
netifaces>=0.11.0
async-timeout>=4.0.2; python_version < "3.11"

0 comments on commit 9adf799

Please sign in to comment.