updated and cleaned
This commit is contained in:
@@ -29,21 +29,24 @@ T = TypeVar("T")
|
||||
|
||||
|
||||
def check_payload_already_exists_mongo_database(filename: str, mongo_provider) -> bool:
|
||||
find_one_result = mongo_provider.find_one(filter_query={
|
||||
"filename": filename
|
||||
})
|
||||
find_one_result = mongo_provider.find_one(filter_query={"filename": filename})
|
||||
if find_one_result:
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def write_payload_to_mongo_database(payload, filename: str, mail_info:dict, mongo_provider) -> bool:
|
||||
insert_one_result = mongo_provider.insert_one(document={
|
||||
"filename": filename,
|
||||
"payload": payload,
|
||||
"stage": "read",
|
||||
"created_at": str(arrow.now()), **mail_info
|
||||
})
|
||||
def write_payload_to_mongo_database(
|
||||
payload, filename: str, mail_info: dict, mongo_provider
|
||||
) -> bool:
|
||||
insert_one_result = mongo_provider.insert_one(
|
||||
document={
|
||||
"filename": filename,
|
||||
"payload": payload,
|
||||
"stage": "read",
|
||||
"created_at": str(arrow.now()),
|
||||
**mail_info,
|
||||
}
|
||||
)
|
||||
if insert_one_result.acknowledged:
|
||||
return True
|
||||
return False
|
||||
@@ -57,7 +60,7 @@ def read_email_and_write_to_mongo_database(email_message, mail_info: dict) -> bo
|
||||
storage_reason=[mongo_prefix, str(arrow.now().date())],
|
||||
)
|
||||
if email_message.is_multipart(): # Check if email has multipart content
|
||||
for part in email_message.walk(): # Each part can be an attachment
|
||||
for part in email_message.walk(): # Each part can be an attachment
|
||||
content_disposition = part.get("Content-Disposition")
|
||||
if content_disposition and "attachment" in content_disposition:
|
||||
if filename := part.get_filename():
|
||||
@@ -71,7 +74,7 @@ def read_email_and_write_to_mongo_database(email_message, mail_info: dict) -> bo
|
||||
payload=payload,
|
||||
filename=filename,
|
||||
mongo_provider=mongo_provider,
|
||||
mail_info=mail_info
|
||||
mail_info=mail_info,
|
||||
)
|
||||
else: # Handle non-multipart email, though this is rare for emails with attachments
|
||||
content_disposition = email_message.get("Content-Disposition")
|
||||
@@ -81,7 +84,7 @@ def read_email_and_write_to_mongo_database(email_message, mail_info: dict) -> bo
|
||||
filename=filename,
|
||||
mongo_provider=mongo_provider,
|
||||
)
|
||||
is_iban_in_filename= authorized_iban_cleaned in str(filename)
|
||||
is_iban_in_filename = authorized_iban_cleaned in str(filename)
|
||||
if is_iban_in_filename and file_exists:
|
||||
payload = email_message.get_payload(decode=True)
|
||||
return write_payload_to_mongo_database(
|
||||
@@ -114,10 +117,10 @@ def app():
|
||||
if email_message := banks_mail.email:
|
||||
headers = {k.lower(): v for k, v in banks_mail.headers.items()}
|
||||
mail_info = {
|
||||
"from": headers['from'],
|
||||
"to": headers['to'],
|
||||
"subject": headers['subject'],
|
||||
"date": str(headers['date']),
|
||||
"from": headers["from"],
|
||||
"to": headers["to"],
|
||||
"subject": headers["subject"],
|
||||
"date": str(headers["date"]),
|
||||
}
|
||||
read_email_and_write_to_mongo_database(
|
||||
email_message=email_message, mail_info=mail_info
|
||||
@@ -127,6 +130,6 @@ def app():
|
||||
if __name__ == "__main__":
|
||||
|
||||
while True:
|
||||
print('Running email service...')
|
||||
print("Running email service...")
|
||||
app()
|
||||
time.sleep(Config.EMAIL_SLEEP)
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
|
||||
class Config:
|
||||
# IP_ADDRESS: str = "http://10.10.2.46:41575/internal/isbank/retreive"
|
||||
SERVICE_TIMING: int = 900 # 15 min
|
||||
|
||||
Reference in New Issue
Block a user