Merge branch 'superm1/robot-encoding' into 'main'
trivial: contrib: use python-magic to detect encoding of emails See merge request kernel-firmware/linux-firmware!422main
commit
3d2c53f21f
|
@ -11,6 +11,7 @@ import email.utils
|
||||||
import smtplib
|
import smtplib
|
||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
|
import magic # https://pypi.python.org/pypi/python-magic
|
||||||
from datetime import date
|
from datetime import date
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
|
|
||||||
|
@ -52,12 +53,11 @@ def classify_content(content):
|
||||||
body = msg.get_payload(decode=True)
|
body = msg.get_payload(decode=True)
|
||||||
|
|
||||||
if body:
|
if body:
|
||||||
for encoding in ["utf-8", "windows-1252"]:
|
m = magic.Magic(mime_encoding=True)
|
||||||
try:
|
try:
|
||||||
decoded = body.decode(encoding)
|
decoded = body.decode(m.from_buffer(body))
|
||||||
break
|
except UnicodeDecodeError:
|
||||||
except UnicodeDecodeError:
|
pass
|
||||||
pass
|
|
||||||
|
|
||||||
if decoded:
|
if decoded:
|
||||||
for key in content_types.keys():
|
for key in content_types.keys():
|
||||||
|
@ -70,8 +70,11 @@ def classify_content(content):
|
||||||
|
|
||||||
|
|
||||||
def fetch_url(url):
|
def fetch_url(url):
|
||||||
|
blob = None
|
||||||
with urllib.request.urlopen(url) as response:
|
with urllib.request.urlopen(url) as response:
|
||||||
return response.read().decode("utf-8")
|
blob = response.read()
|
||||||
|
m = magic.Magic(mime_encoding=True)
|
||||||
|
return blob.decode(m.from_buffer(blob))
|
||||||
|
|
||||||
|
|
||||||
def quiet_cmd(cmd):
|
def quiet_cmd(cmd):
|
||||||
|
|
Loading…
Reference in New Issue