fix UnicodeDecodeError raise when detect encoding
This commit is contained in:
parent
6285051c39
commit
347410db37
|
|
@ -65,6 +65,25 @@ MTYPE_TABLE_CUSTOM = {
|
|||
}
|
||||
|
||||
|
||||
def _unicode_packet(packet):
|
||||
# attempt utf-8
|
||||
try:
|
||||
return packet.decode('utf-8')
|
||||
except UnicodeDecodeError:
|
||||
pass
|
||||
|
||||
# attempt to detect encoding
|
||||
res = chardet.detect(packet.split(b':', 1)[-1])
|
||||
if res['confidence'] > 0.7:
|
||||
try:
|
||||
return packet.decode(res['encoding'])
|
||||
except UnicodeDecodeError:
|
||||
pass
|
||||
|
||||
# if everything fails
|
||||
return packet.decode('latin-1')
|
||||
|
||||
|
||||
def parse(packet):
|
||||
"""
|
||||
Parses an APRS packet and returns a dict with decoded data
|
||||
|
|
@ -83,15 +102,7 @@ def parse(packet):
|
|||
|
||||
# attempt to detect encoding
|
||||
if isinstance(packet, bytes):
|
||||
try:
|
||||
packet = packet.decode('utf-8')
|
||||
except UnicodeDecodeError:
|
||||
res = chardet.detect(packet.split(b':', 1)[-1])
|
||||
|
||||
if res['confidence'] > 0.7:
|
||||
packet = packet.decode(res['encoding'])
|
||||
else:
|
||||
packet = packet.decode('latin-1')
|
||||
packet = _unicode_packet(packet)
|
||||
|
||||
packet = packet.rstrip("\r\n")
|
||||
logger.debug("Parsing: %s", packet)
|
||||
|
|
|
|||
Loading…
Reference in New Issue