source_logistics_ecommerce_.../source_ecommerce_file_alert...

115 lines
4.0 KiB
Python

#!/usr/bin/env python3
"""
If daily processing crashed, files will be in
2 directories, trigger an email if a file is left over
"""
import pprint
import pathlib
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import shutil
import os
import base64
import google.auth
import pickle
# Gmail API utils
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
SCOPES = ['https://mail.google.com/']
THIS_DIRECTORY = pathlib.Path(__file__).parent
INCOMING_DIRECTORY = THIS_DIRECTORY / "incoming_orders"
SHIPMENTS_DIRECTORY = THIS_DIRECTORY / "incoming_shipments"
SOH_IMPORT_DIRECTORY = THIS_DIRECTORY / "to_import_SOH"
NEW_FILES_DIRECTORY = THIS_DIRECTORY / "new_files_from_ftp"
PROBLEMS_DIRECTORY = THIS_DIRECTORY / "problems"
SOH_IMPORT_DIRECTORY = THIS_DIRECTORY / "to_import_SOH"
SDH_IMPORT_DIRECTORY = THIS_DIRECTORY / "to_import_SDH"
def main():
"""
Check for leftover files which means something unexpected happened. Then clean the files
so there isn't a mess for the next run
"""
file_count = []
for file in INCOMING_DIRECTORY.iterdir():
if file.name[-4:] == '.csv':#move the file that drove the process
file_count.append(f'order: {file.name}')
shutil.move(file, PROBLEMS_DIRECTORY / file.name)
for file in SOH_IMPORT_DIRECTORY.iterdir():#delete any import files we were in the middle of
if file.name[-4:] == '.dat':
os.remove(file)
for file in SHIPMENTS_DIRECTORY.iterdir():
if file.name[-4:] == '.csv':
file_count.append(f'shipment: {file.name}')
shutil.move(file, PROBLEMS_DIRECTORY / file.name)
for file in SDH_IMPORT_DIRECTORY.iterdir():
if file.name[-4:] == '.dat':
os.remove(file)
for file in NEW_FILES_DIRECTORY.iterdir():
if file.name[-4:] == '.csv':
file_count.append(f'new file: {file.name}')
shutil.move(file, PROBLEMS_DIRECTORY / file.name)
if file_count:
file_alert(file_count)
def gmail_authenticate():
creds = None
# the file token.pickle stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first time
if os.path.exists("token.pickle"):
with open("token.pickle", "rb") as token:
creds = pickle.load(token)
# if there are no (valid) credentials availablle, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file('credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
# save the credentials for the next run
with open("token.pickle", "wb") as token:
pickle.dump(creds, token)
return build('gmail', 'v1', credentials=creds)
def gmail_send_message(service, payload):
create_message = {"raw": payload}
# pylint: disable=E1101
send_message = (
service.users()
.messages()
.send(userId="me", body=create_message)
.execute()
)
return send_message
def file_alert(file_list):
file_string = ', '.join(file_list)
msg = MIMEMultipart()
msg['Subject'] = 'Source ecommerce: Files Left Over'
msg['Precedence'] = 'bulk'
msg['From'] = 'x3report@stashtea.com'
msg['To'] = 'bleeson@stashtea.com,icarrera@yamamotoyama.com'
#msg['CC'] = 'bleeson@stashtea.com'
emailtext = f'files: {file_string}'
msg.attach(MIMEText(emailtext, 'plain'))
service = gmail_authenticate()
encoded_message = base64.urlsafe_b64encode(msg.as_bytes()).decode()
gmail_send_message(service, encoded_message)
# with smtplib.SMTP_SSL("smtp.gmail.com", 465) as smtp:
# smtp.login(user='x3reportmk2@yamamotoyama.com', password=r'n</W<7fr"VD~\2&[pZc5')
# smtp.send_message(msg)
if __name__ == "__main__":
main()