migrator functions updated

This commit is contained in:
2024-11-11 22:23:07 +03:00
parent f6135ced5f
commit ffb85a62f6
56 changed files with 567 additions and 485 deletions

View File

@@ -1,20 +1,18 @@
class Config:
IP_ADDRESS: str = "http://10.10.2.46:41575/internal/isbank/retreive"
SERVICE_TIMING: int = 900 # 15 min
SERVICE_TIMING: int = 900 # 15 min
UNREAD_PATH: str = '/home/bank/isbank/unread/'
PARSED_PATH: str = '/home/bank/isbank/parsed/parsed_data.json'
ARCHIVE_PATH: str = '/home/bank/isbank/archive/'
INCOMING_PATH: str = '/home/bank/isbank/unread'
COMPLETED_PATH: str = '/home/bank/isbank/completed'
UNREAD_PATH: str = "/home/bank/isbank/unread/"
PARSED_PATH: str = "/home/bank/isbank/parsed/parsed_data.json"
ARCHIVE_PATH: str = "/home/bank/isbank/archive/"
INCOMING_PATH: str = "/home/bank/isbank/unread"
COMPLETED_PATH: str = "/home/bank/isbank/completed"
MAILBOX: str = 'bilgilendirme@ileti.isbank.com.tr'
KARATAY: str = 'karatay.berkay@gmail.com'
MAILBOX: str = "bilgilendirme@ileti.isbank.com.tr"
KARATAY: str = "karatay.berkay@gmail.com"
EMAIL_HOST: str = '10.10.2.34'
EMAIL_USERNAME: str = 'karatay@mehmetkaratay.com.tr'
EMAIL_PASSWORD: str = ''
EMAIL_HOST: str = "10.10.2.34"
EMAIL_USERNAME: str = "karatay@mehmetkaratay.com.tr"
EMAIL_PASSWORD: str = ""
INFO_MAIL = 'mehmet.karatay@hotmail.com'
INFO_MAIL = "mehmet.karatay@hotmail.com"

View File

@@ -6,9 +6,10 @@ email_sender = EmailSender(
host=Config.EMAIL_HOST,
port=993,
username=Config.EMAIL_USERNAME,
password=Config.EMAIL_PASSWORD
password=Config.EMAIL_PASSWORD,
)
def mail_sender_service(subject, list_of_read_attachment_names):
email_sender.connect()
try:
@@ -20,5 +21,5 @@ def mail_sender_service(subject, list_of_read_attachment_names):
return True
except Exception as e:
err = e
print('Mail send error raised : ', err)
return False
print("Mail send error raised : ", err)
return False

View File

@@ -16,13 +16,13 @@ basicConfig(filename=__name__, level=INFO)
def read_json_file(json_file_path):
if os.path.exists(json_file_path):
with open(json_file_path, 'r') as json_file:
with open(json_file_path, "r") as json_file:
return json.load(json_file)
return {}
def write_json_file(json_file_path, data):
with open(json_file_path, 'w') as json_file:
with open(json_file_path, "w") as json_file:
json.dump(data, json_file, indent=4)
@@ -39,7 +39,7 @@ def parser_service():
excel_files = glob.glob(os.path.join(absolute_path, "*.xls*"))
for file_path in excel_files:
xl_name = str(file_path).split('/')[-1]
xl_name = str(file_path).split("/")[-1]
xl_file = pandas.read_excel(file_path)
xl_frame = pandas.DataFrame(xl_file)
iban = ""
@@ -68,13 +68,17 @@ def parser_service():
)
logger.info(f"Insert Dict: {insert_dict}")
existing_data = read_json_file(parsed)
if xl_name in existing_data: # Check if the key already exists
if xl_name in existing_data: # Check if the key already exists
if insert_dict not in existing_data[xl_name]:
existing_data[xl_name].append(insert_dict)
else:
existing_data[xl_name] = [insert_dict] # Update the JSON data with the new key-value pair
write_json_file(parsed, existing_data) # Write the updated data back to the JSON file
existing_data[xl_name] = [
insert_dict
] # Update the JSON data with the new key-value pair
write_json_file(
parsed, existing_data
) # Write the updated data back to the JSON file
shutil.move(
file_path,
os.path.join(current_directory, completed, os.path.basename(file_path)),
@@ -83,8 +87,7 @@ def parser_service():
# if __name__ == "__main__":
# parse_xl_files_and_copy_to_database()
# time.sleep(int(os.getenv("PARSER_SEQUENCE", 60)))
# while True:
# parse_xl_files_and_copy_to_database()
# time.sleep(int(os.getenv("PARSER_SEQUENCE", 60)))
# time.sleep(int(os.getenv("PARSER_SEQUENCE", 60)))
# while True:
# parse_xl_files_and_copy_to_database()
# time.sleep(int(os.getenv("PARSER_SEQUENCE", 60)))

View File

@@ -6,19 +6,14 @@ from redbox import EmailBox
from redbox.query import FROM, UNSEEN, OR
host='10.10.2.34'
port=993
username='isbank@mehmetkaratay.com.tr'
password='system'
host = "10.10.2.34"
port = 993
username = "isbank@mehmetkaratay.com.tr"
password = "system"
authorized_iban = "4245-0093333"
authorized_iban_cleaned = authorized_iban.replace("-", "")
box = EmailBox(
host=host,
port=port,
username=username,
password=password
)
box = EmailBox(host=host, port=port, username=username, password=password)
filter_mail = OR(FROM(Config.MAILBOX), FROM(Config.KARATAY))
filter_print = f"{Config.MAILBOX} & {Config.KARATAY}"
@@ -29,29 +24,45 @@ filter_print = f"{Config.MAILBOX} & {Config.KARATAY}"
def reader_service():
mailbox = box.inbox
banks_mails = mailbox.search(filter_mail & UNSEEN)
print(f'Service is reading mailbox [{username}] with mail sender [{filter_print}] with count : {len(banks_mails)}')
print(
f"Service is reading mailbox [{username}] with mail sender [{filter_print}] with count : {len(banks_mails)}"
)
for banks_mail in banks_mails:
message_attc = lambda fn: f"FROM: {banks_mail.from_}, SEEN: {banks_mail.seen} MSG: email_message.is_multipart Downloaded attachment: {fn}"
message_non_attc = lambda fn: f"FROM: {banks_mail.from_}, SEEN: {banks_mail.seen} MSG: Handle non-multipart email Downloaded attachment: {fn}"
message_attc = (
lambda fn: f"FROM: {banks_mail.from_}, SEEN: {banks_mail.seen} MSG: email_message.is_multipart Downloaded attachment: {fn}"
)
message_non_attc = (
lambda fn: f"FROM: {banks_mail.from_}, SEEN: {banks_mail.seen} MSG: Handle non-multipart email Downloaded attachment: {fn}"
)
# mail_subject = banks_mail.subject
email_message = banks_mail.email
full_path = lambda fn: str(Config.UNREAD_PATH) + str(fn)
completed_path = lambda fn: str(Config.COMPLETED_PATH) + '/' + str(fn)
file_exists = lambda fn: not os.path.exists(full_path(fn)) and not os.path.exists(completed_path(fn))
if email_message.is_multipart(): # Check if email has multipart content
completed_path = lambda fn: str(Config.COMPLETED_PATH) + "/" + str(fn)
file_exists = lambda fn: not os.path.exists(
full_path(fn)
) and not os.path.exists(completed_path(fn))
if email_message.is_multipart(): # Check if email has multipart content
for part in email_message.walk():
content_disposition = part.get('Content-Disposition') # Each part can be an attachment
if content_disposition and 'attachment' in content_disposition:
content_disposition = part.get(
"Content-Disposition"
) # Each part can be an attachment
if content_disposition and "attachment" in content_disposition:
if filename := part.get_filename():
if authorized_iban_cleaned in str(filename) and file_exists(filename):
with open(full_path(filename), 'wb') as f:
if authorized_iban_cleaned in str(filename) and file_exists(
filename
):
with open(full_path(filename), "wb") as f:
print(message_attc(filename))
f.write(part.get_payload(decode=True)) # Save attachment
f.write(
part.get_payload(decode=True)
) # Save attachment
else: # Handle non-multipart email, though this is rare for emails with attachments
content_disposition = email_message.get('Content-Disposition')
if content_disposition and 'attachment' in content_disposition:
content_disposition = email_message.get("Content-Disposition")
if content_disposition and "attachment" in content_disposition:
if filename := email_message.get_filename():
if authorized_iban_cleaned in str(filename) and file_exists(filename):
with open(full_path(filename), 'wb') as f:
if authorized_iban_cleaned in str(filename) and file_exists(
filename
):
with open(full_path(filename), "wb") as f:
print(message_non_attc(filename))
f.write(email_message.get_payload(decode=True))

View File

@@ -28,19 +28,20 @@ class BankReceive(BaseModel):
def read_json_file(json_file_path):
if os.path.exists(json_file_path):
with open(json_file_path, 'r') as json_file:
with open(json_file_path, "r") as json_file:
return json.load(json_file)
return {}
def write_json_file(json_file_path, data):
with open(json_file_path, 'w') as json_file:
with open(json_file_path, "w") as json_file:
json.dump(data, json_file, indent=4)
def sender_service():
parsed_data = read_json_file(Config.PARSED_PATH)
if not parsed_data:
print('Parsed data can not found')
print("Parsed data can not found")
return
if is_bank_retrieve_account_records(bank_data=json.dumps(parsed_data)):
@@ -49,14 +50,15 @@ def sender_service():
print("is_bank_retrieve_account_records is failed")
return
today_date = str(arrow.now('GMT+0'))
archive_path = str(Config.ARCHIVE_PATH + today_date + '.json').replace(' ', '')
today_date = str(arrow.now("GMT+0"))
archive_path = str(Config.ARCHIVE_PATH + today_date + ".json").replace(" ", "")
already_archived = read_json_file(archive_path)
already_archived[today_date] = parsed_data
write_json_file(archive_path, already_archived)
write_json_file(Config.PARSED_PATH, {})
return
def is_bank_retrieve_account_records(bank_data):
from databases import AccountRecords
@@ -88,4 +90,3 @@ def is_bank_retrieve_account_records(bank_data):
# err = e
# print('Exception',e)
# return False, {}, 500

View File

@@ -1,14 +1,13 @@
import time
import datetime
from config_isbank import Config
from config_isbank import Config
from isbank_reader import reader_service
from isbank_parser import parser_service
from isbank_sender import sender_service
if __name__ == "__main__":
print('Bank service booted...')
print("Bank service booted...")
while True:
try:
reader_service()
@@ -17,10 +16,9 @@ if __name__ == "__main__":
time.sleep(1)
sender_service()
time.sleep(1)
print(datetime.datetime.now().__str__(), ' : system completed a cycle...')
print(datetime.datetime.now().__str__(), " : system completed a cycle...")
except Exception as e:
err = e
print('Raised Error :', err)
print("Raised Error :", err)
time.sleep(int(Config.SERVICE_TIMING or 900))
# time.sleep(10)

View File

@@ -1,26 +1,27 @@
import time
import datetime
from config_isbank import Config
from config_isbank import Config
from isbank_reader import reader_service
from isbank_parser import parser_service
from isbank_sender import sender_service
if __name__ == "__main__":
print('Bank service booted...')
print("Bank service booted...")
# while True:
# try:
# try:
reader_service()
time.sleep(1)
parser_service()
time.sleep(1)
sender_service()
time.sleep(1)
print(datetime.datetime.now().__str__(), ' : system completed a cycle without error...')
# except Exception as e:
# err = e
# print('Raised Error :', err)
# time.sleep(int(Config.SERVICE_TIMING or 900))
# time.sleep(10)
print(
datetime.datetime.now().__str__(),
" : system completed a cycle without error...",
)
# except Exception as e:
# err = e
# print('Raised Error :', err)
# time.sleep(int(Config.SERVICE_TIMING or 900))
# time.sleep(10)