(20241127) Google API OAuth2.0 support ready!
This commit is contained in:
@@ -24,8 +24,7 @@
|
||||
N/A
|
||||
|
||||
"""
|
||||
|
||||
|
||||
import base64
|
||||
# *****************************************************************************************************************
|
||||
# ***** ****
|
||||
# *** IMPORT ***
|
||||
@@ -49,6 +48,9 @@ from utils_v2.date_time import date_time
|
||||
# To work with mails:
|
||||
import mailparser
|
||||
|
||||
# To parse the HTML content in the mail:
|
||||
from bs4 import BeautifulSoup
|
||||
|
||||
# To work with datatypes:
|
||||
from typing import Any, Dict
|
||||
|
||||
@@ -111,6 +113,42 @@ def parse(raw_mail: str | bytes) -> Dict[str, Any]:
|
||||
} for attachment in parsed_mail.attachments
|
||||
]
|
||||
|
||||
# Figure out which entity (text and HTML) came in which sequence.
|
||||
# The library doesn't give us any sequence info so we do some custom string processing here to figure out the order
|
||||
# in which to render the contents of the page.
|
||||
parts = [
|
||||
{
|
||||
"no": None,
|
||||
"offset": max(
|
||||
parsed_mail.message_as_string.find(t),
|
||||
parsed_mail.message_as_string.find(base64.b64encode(t.encode()).decode())
|
||||
),
|
||||
"type": "text/plain",
|
||||
"data": t
|
||||
} for t in parsed_mail.text_plain
|
||||
]
|
||||
parts = parts + [
|
||||
{
|
||||
"partNo": None,
|
||||
"offset": max(
|
||||
parsed_mail.message_as_string.find(h),
|
||||
parsed_mail.message_as_string.find(base64.b64encode(h.encode()).decode())
|
||||
),
|
||||
"type": "text/html",
|
||||
"data": h
|
||||
} for h in parsed_mail.text_html
|
||||
]
|
||||
parts = sorted(parts, key = lambda x: x["offset"])
|
||||
for i, p in enumerate(parts): p["no"] = i
|
||||
|
||||
# Get the unformatted text from everything in the mail:
|
||||
unformatted_text = []
|
||||
for p in parts:
|
||||
if p["type"] == "text/html":
|
||||
html_parser = BeautifulSoup(p["data"], "html.parser")
|
||||
unformatted_text.append(html_parser.get_text())
|
||||
else: unformatted_text.append(p["data"])
|
||||
|
||||
# Put everything together:
|
||||
return {
|
||||
"ts": date_time.to_timezone(parsed_mail.date, timezone = date_time.TIMEZONE_UTC),
|
||||
@@ -121,6 +159,8 @@ def parse(raw_mail: str | bytes) -> Dict[str, Any]:
|
||||
"bcc": [{"name": _[0] or _[1], "email": _[1]} for _ in parsed_mail.headers.get("Bcc", [])],
|
||||
"text": parsed_mail.text_plain,
|
||||
"html": parsed_mail.text_html,
|
||||
"parts": parts,
|
||||
"unformattedText": "\n".join(unformatted_text),
|
||||
"attachments": message_attachments,
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user