store cookies in xdg-data-dir, clean up typing, fix some tests
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
from dataclasses import dataclass
|
||||
import datetime
|
||||
import os
|
||||
import re
|
||||
@@ -6,19 +7,29 @@ from amazonorders.conf import AmazonOrdersConfig
|
||||
from amazonorders.orders import AmazonOrders
|
||||
from amazonorders.transactions import AmazonTransactions
|
||||
from amazonorders.session import AmazonSession
|
||||
from amazonorders.entity.order import Order
|
||||
from amazonorders.entity.transaction import Transaction
|
||||
import xdg_base_dirs
|
||||
|
||||
|
||||
def get_accounts(env: dict[str, str] = os.environ):
|
||||
@dataclass
|
||||
class AmazonAccount:
|
||||
email: str
|
||||
password: str
|
||||
|
||||
|
||||
def get_accounts(env: dict[str, str] = os.environ) -> list[AmazonAccount]:
|
||||
accts = []
|
||||
for k, v in env.items():
|
||||
if k == 'AMAZON_EMAIL':
|
||||
pwd = env['AMAZON_PASSWORD']
|
||||
accts.append((v, pwd))
|
||||
acct = AmazonAccount(email=v, password=pwd)
|
||||
accts.append(acct)
|
||||
elif m := re.match(r'AMAZON_EMAIL_(\d+)', k):
|
||||
idx = m.group(1)
|
||||
pwd = env[f'AMAZON_PASSWORD_{idx}']
|
||||
accts.append((v, pwd))
|
||||
acct = AmazonAccount(email=v, password=pwd)
|
||||
accts.append(acct)
|
||||
|
||||
return accts
|
||||
|
||||
@@ -27,17 +38,15 @@ class Session:
|
||||
orders: AmazonOrders
|
||||
transactions: AmazonTransactions
|
||||
|
||||
def __init__(self, email, pwd):
|
||||
self.email = email
|
||||
self.pwd = pwd
|
||||
def __init__(self, acct: AmazonAccount):
|
||||
self.acct = acct
|
||||
self._cookie_jar_path = xdg_base_dirs.xdg_data_home() / f'catnab/cookies/{self.acct.email}.json'
|
||||
|
||||
def __enter__(self):
|
||||
config = AmazonOrdersConfig(data={
|
||||
'cookie_jar_path': f'.cookies/{self.email}.json',
|
||||
})
|
||||
config = AmazonOrdersConfig(data={'cookie_jar_path': self._cookie_jar_path})
|
||||
self.amazon_session = AmazonSession(
|
||||
self.email,
|
||||
self.pwd,
|
||||
username=self.acct.email,
|
||||
password=self.acct.password,
|
||||
config=config
|
||||
)
|
||||
self.amazon_session.login()
|
||||
@@ -50,13 +59,13 @@ class Session:
|
||||
def __exit__(self, *exc_info):
|
||||
pass
|
||||
|
||||
def list_transactions(self, days: int):
|
||||
def list_transactions(self, days: int) -> list[Transaction]:
|
||||
return self.transactions.get_transactions(days)
|
||||
|
||||
def get_order(self, order_number: str):
|
||||
def get_order(self, order_number: str) -> Order:
|
||||
return self.orders.get_order(order_number)
|
||||
|
||||
def get_orders(self, days: int):
|
||||
def get_orders(self, days: int) -> list[Order]:
|
||||
today = datetime.datetime.today()
|
||||
since_date = today - datetime.timedelta(days=days)
|
||||
years = range(since_date.year, today.year + 1)
|
||||
|
||||
Reference in New Issue
Block a user