2020-03-31 18:32:54 +02:00
|
|
|
import sys
|
2020-09-01 19:18:55 +02:00
|
|
|
from datetime import datetime
|
2020-03-31 18:32:54 +02:00
|
|
|
|
|
|
|
import click
|
2020-09-01 19:18:55 +02:00
|
|
|
import dateparser
|
2020-03-31 18:32:54 +02:00
|
|
|
|
2021-11-20 15:00:43 +01:00
|
|
|
from ofx_processor.downloaders import LclDownloader
|
2020-03-31 18:32:54 +02:00
|
|
|
from ofx_processor.utils.base_ofx import OfxBaseLine, OfxBaseProcessor
|
|
|
|
|
|
|
|
|
|
|
|
class LclLine(OfxBaseLine):
|
2020-09-01 19:18:55 +02:00
|
|
|
def _extract_payee_and_date(self):
|
|
|
|
default_date = (
|
|
|
|
self.data.dtposted.isoformat().split("T")[0] if self.data.dtposted else None
|
|
|
|
)
|
|
|
|
|
2020-09-12 08:08:44 +02:00
|
|
|
if self.data.trntype.lower() == "check":
|
|
|
|
return "CHQ", default_date
|
|
|
|
|
|
|
|
if not self.data.name:
|
|
|
|
return "", default_date
|
|
|
|
|
|
|
|
split = self.data.name.split()
|
2020-09-01 19:18:55 +02:00
|
|
|
if not split:
|
|
|
|
return "", default_date
|
|
|
|
|
|
|
|
date_spot = split[-1]
|
|
|
|
if "/" not in date_spot:
|
|
|
|
return " ".join(split), default_date
|
|
|
|
|
|
|
|
date = dateparser.parse(
|
|
|
|
split[-1], date_formats=["%d/%m/%Y"], languages=["fr"]
|
|
|
|
) # type: datetime
|
|
|
|
if date:
|
|
|
|
name = " ".join(split[:-1])
|
|
|
|
date = date.strftime("%Y-%m-%d") # type: str
|
|
|
|
else:
|
|
|
|
name = " ".join(split)
|
|
|
|
date = default_date
|
|
|
|
|
|
|
|
return name, date
|
|
|
|
|
|
|
|
def get_payee(self):
|
|
|
|
return self._extract_payee_and_date()[0]
|
|
|
|
|
|
|
|
def get_date(self):
|
|
|
|
return self._extract_payee_and_date()[1]
|
2020-03-31 18:32:54 +02:00
|
|
|
|
2020-09-12 08:08:44 +02:00
|
|
|
def get_memo(self):
|
|
|
|
if self.data.trntype.lower() == "check":
|
|
|
|
return f"CHQ {self.data.checknum}"
|
|
|
|
return super().get_memo()
|
|
|
|
|
2020-03-31 18:32:54 +02:00
|
|
|
|
|
|
|
class LclProcessor(OfxBaseProcessor):
|
|
|
|
line_class = LclLine
|
|
|
|
account_name = "lcl"
|
2020-05-08 18:02:25 +02:00
|
|
|
command_name = "lcl"
|
2020-03-31 18:32:54 +02:00
|
|
|
|
|
|
|
def parse_file(self):
|
|
|
|
# The first line of this file needs to be removed.
|
|
|
|
# It contains something that is not part of the header of an OFX file.
|
|
|
|
try:
|
|
|
|
with open(self.filename, "r") as user_file:
|
|
|
|
data = user_file.read().splitlines(True)
|
|
|
|
except FileNotFoundError:
|
|
|
|
click.secho("Couldn't find ofx file", fg="red")
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
if "Content-Type:" in data[0]:
|
|
|
|
with open(self.filename, "w") as temp_file:
|
|
|
|
temp_file.writelines(data[1:])
|
|
|
|
|
|
|
|
transactions = super(LclProcessor, self).parse_file()
|
|
|
|
|
|
|
|
if "Content-Type:" in data[0]:
|
|
|
|
with open(self.filename, "w") as temp_file:
|
|
|
|
temp_file.writelines(data)
|
|
|
|
|
|
|
|
return transactions
|
|
|
|
|
2020-05-08 18:02:25 +02:00
|
|
|
|
2021-11-20 15:00:43 +01:00
|
|
|
def main(filename, keep, download):
|
2020-05-08 18:02:25 +02:00
|
|
|
"""Import LCL bank statement (OFX file)."""
|
2021-11-20 15:00:43 +01:00
|
|
|
if download:
|
|
|
|
if filename:
|
|
|
|
click.secho(
|
|
|
|
"You can't specify a file name with auto download. "
|
|
|
|
"Specified file name will be ignored.",
|
|
|
|
fg="yellow",
|
|
|
|
)
|
|
|
|
filename = LclDownloader().download()
|
2020-05-08 18:02:25 +02:00
|
|
|
LclProcessor(filename).push_to_ynab(keep)
|