Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix wrong utf-8 encoding with ftfy #280

Merged
merged 1 commit into from
Feb 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
Loading