From 55b6d565b072fb4e73bef26416dbc6b5f512a2ce Mon Sep 17 00:00:00 2001 From: Blallo Date: Sun, 18 Sep 2022 23:29:02 +0200 Subject: [PATCH] Optionally save file to store path --- latecomers/config.py | 3 +++ latecomers/helpers.py | 8 ++++++++ latecomers/main.py | 2 +- latecomers/notifier.py | 8 +++----- latecomers/retrieve.py | 4 ++-- latecomers/serializer.py | 13 +++++++++++-- 6 files changed, 28 insertions(+), 10 deletions(-) diff --git a/latecomers/config.py b/latecomers/config.py index 5f05950..f4befcd 100644 --- a/latecomers/config.py +++ b/latecomers/config.py @@ -20,6 +20,7 @@ def get_section(obj: T.Dict[T.Text, T.Any], key: T.Text) -> T.Any: class Config(object): smtp: T.Dict[T.Text, T.Any] = {} to: T.List[T.Text] = [] + store: T.Optional[T.Text] = None def __init__(self, path: T.Text) -> None: self.from_file(path) @@ -46,5 +47,7 @@ class Config(object): self.smtp["smtp_from"] = smtp.get("smtp_from") + self.store = content.get("store") + def __str__(self) -> T.Text: return f"Config" diff --git a/latecomers/helpers.py b/latecomers/helpers.py index eb5267b..c23e969 100644 --- a/latecomers/helpers.py +++ b/latecomers/helpers.py @@ -1,4 +1,5 @@ # -*- encoding: utf-8 -*- +from datetime import datetime, timedelta import functools import logging import sys @@ -48,3 +49,10 @@ def retry_and_log(logger: logging.Logger, retries: int): return inner return wrapper + + +def get_date() -> T.Text: + """ + Get yesterday's date in %Y-%m-%d format. + """ + return (datetime.now() - timedelta(days=1)).strftime("%Y-%m-%d") diff --git a/latecomers/main.py b/latecomers/main.py index 17c07bd..1fc3acd 100644 --- a/latecomers/main.py +++ b/latecomers/main.py @@ -34,7 +34,7 @@ def main(config: Config): if not data: out.send_no_data(config.to) - excel = to_excel(data) + excel = to_excel(data, config.store) out.send_result(config.to, excel) diff --git a/latecomers/notifier.py b/latecomers/notifier.py index 46aaaf9..269327e 100644 --- a/latecomers/notifier.py +++ b/latecomers/notifier.py @@ -1,6 +1,5 @@ # -*- encoding: utf-8 -*- from contextlib import contextmanager -from datetime import datetime, timedelta from email import encoders from email.mime.base import MIMEBase from email.mime.multipart import MIMEMultipart @@ -10,8 +9,7 @@ import smtplib import ssl import typing as T - -from latecomers.helpers import retry_and_log +from latecomers.helpers import retry_and_log, get_date RETRIES = 3 @@ -58,7 +56,7 @@ class Notifier(object): @retry_and_log(logger, RETRIES) def send_result(self, to: T.List[T.Text], result: bytes) -> None: - date = (datetime.now() - timedelta(days=1)).strftime("%Y-%m-%d") + date = get_date() body = f"Resoconto dei voli dal sito di AdR per l'aereoporto di Ciampino in data {date}" # noqa: E501 message = MIMEMultipart() @@ -84,7 +82,7 @@ class Notifier(object): @retry_and_log(logger, RETRIES) def send_no_data(self, to: T.List[T.Text]) -> None: - date = (datetime.now() - timedelta(days=1)).strftime("%Y-%m-%d") + date = get_date() body = f"""Attenzione Nessun dato รจ stato trovato per i voli in data {date} dal sito di AdR per diff --git a/latecomers/retrieve.py b/latecomers/retrieve.py index 7bc98cd..e30ec48 100644 --- a/latecomers/retrieve.py +++ b/latecomers/retrieve.py @@ -3,7 +3,7 @@ from datetime import datetime, timedelta import logging import typing as T -from latecomers.helpers import logit +from latecomers.helpers import logit, get_date import requests @@ -18,7 +18,7 @@ def remote_inst() -> T.Text: """ Returns the url to retrieve yesterday's data from institutional site. """ - yesterday = (datetime.now() - timedelta(days=1)).strftime("%Y-%m-%d") + yesterday = get_date() url = f"https://www.adr.it/pax-cia-voli-in-tempo-reale?p_p_id=3_WAR_realtimeflightsportlet&p_p_lifecycle=0&p_p_state=normal&p_p_mode=view&_3_WAR_realtimeflightsportlet_tab=arrival&_3_WAR_realtimeflightsportlet_codScaOpe=CIA&_3_WAR_realtimeflightsportlet_rouIata=&_3_WAR_realtimeflightsportlet_searchType=standard&_3_WAR_realtimeflightsportlet_airport=&_3_WAR_realtimeflightsportlet_date={yesterday}&_3_WAR_realtimeflightsportlet_orario=00:00-24:00&_3_WAR_realtimeflightsportlet_codVet=&_3_WAR_realtimeflightsportlet_carrier=&_3_WAR_realtimeflightsportlet_rtFlightsSearchContainerPrimaryKeys=&_3_WAR_realtimeflightsportlet_delta={HOW_MANY}" # noqa: E501 return url diff --git a/latecomers/serializer.py b/latecomers/serializer.py index 3062e95..2eaeb65 100644 --- a/latecomers/serializer.py +++ b/latecomers/serializer.py @@ -1,10 +1,11 @@ # -*- encoding: utf-8 -*- import logging +import os.path from tempfile import NamedTemporaryFile import typing as T from latecomers.parse import Details -from latecomers.helpers import logit +from latecomers.helpers import logit, get_date import pandas as pd @@ -13,7 +14,7 @@ logger = logging.getLogger(__name__) @logit(logger) -def to_excel(data: T.List[Details]) -> bytes: +def to_excel(data: T.List[Details], dst: T.Optional[T.Text] = None) -> bytes: """ This function takes the list of parsed rows as input and returns the bytes corresponding to the excel file derived from such lines. @@ -38,4 +39,12 @@ def to_excel(data: T.List[Details]) -> bytes: tmp.seek(0) content = tmp.read() + if dst: + filepath = os.path.join(dst, f"{get_date()}.xlsx") + try: + with open(filepath, "wb") as out: + out.write(content) + except Exception as e: + logger.warning(f"Cannot save to path '{filepath}': {e}") + return content