added --find_promos_by_name option

This commit is contained in:
KorenLazar
2020-12-16 16:09:43 +02:00
parent db6bfb3632
commit 4c0eba1771
2 changed files with 26 additions and 8 deletions

14
main.py
View File

@@ -1,9 +1,11 @@
from argparse import ArgumentParser from argparse import ArgumentParser
import logging import logging
from promotion import main_latest_promos from promotion import main_latest_promos, get_promos_by_name
from store import get_store_id, store_id_type from store import get_store_id, store_id_type
from utils import get_products_prices from utils import get_products_prices
# TODO: improve extendability: support addition of different supermarket chains
# TODO: fix problem of left-to-right printing in Windows' cmd
if __name__ == '__main__': if __name__ == '__main__':
parser = ArgumentParser() parser = ArgumentParser()
@@ -13,6 +15,12 @@ if __name__ == '__main__':
nargs=1, nargs=1,
type=store_id_type, type=store_id_type,
) )
parser.add_argument('--find_promos_by_name',
help="prints all promos containing the given promo_name in the given store",
metavar=('store_id', 'promo_name'),
nargs=2,
# type=store_id_type, # TODO: add type-checking of first parameter
)
parser.add_argument('--price', parser.add_argument('--price',
help='prints all products that contain the given name in the requested store', help='prints all products that contain the given name in the requested store',
metavar=('store_id', 'product_name'), metavar=('store_id', 'product_name'),
@@ -46,3 +54,7 @@ if __name__ == '__main__':
elif args.find_store_id: elif args.find_store_id:
arg_city = args.find_store_id[0] arg_city = args.find_store_id[0]
get_store_id(city=arg_city, load_xml=args.load_xml) get_store_id(city=arg_city, load_xml=args.load_xml)
elif args.find_promos_by_name:
arg_store_id = int(args.find_promos_by_name[0])
get_promos_by_name(store_id=arg_store_id, load_xml=args.load_xml, promo_name=args.find_promos_by_name[1])

View File

@@ -16,19 +16,18 @@ class Promotion:
It contains only part of the available information in Shufersal's data. It contains only part of the available information in Shufersal's data.
""" """
def __init__(self, content: str, start_date: datetime, end_date: datetime, update_date: datetime, def __init__(self, content: str, start_date: datetime, end_date: datetime, update_date: datetime, items: List[str]):
code_items: List[str]):
self.content: str = content self.content: str = content
self.start_date = start_date self.start_date = start_date
self.end_date: datetime = end_date self.end_date: datetime = end_date
self.update_date: datetime = update_date self.update_date: datetime = update_date
self.code_items: List[str] = code_items self.items: List[str] = items
def __str__(self): def __str__(self):
title = self.content title = self.content
dates_range = f"Between {self.start_date.date()} and {self.end_date.date()}" dates_range = f"Between {self.start_date.date()} and {self.end_date.date()}"
update_line = f"Updated at {self.update_date.date()}" update_line = f"Updated at {self.update_date.date()}"
items = '\n'.join(str(item) for item in self.code_items) items = '\n'.join(str(item) for item in self.items)
return '\n'.join([title, dates_range, update_line, items]) + '\n' return '\n'.join([title, dates_range, update_line, items]) + '\n'
@@ -51,8 +50,8 @@ def get_available_promos(store_id: int, load_xml: bool) -> List[Promotion]:
start_date=datetime.strptime(cur_promo.find('PromotionStartDate').text, '%Y-%m-%d'), start_date=datetime.strptime(cur_promo.find('PromotionStartDate').text, '%Y-%m-%d'),
end_date=datetime.strptime(cur_promo.find('PromotionEndDate').text, '%Y-%m-%d'), end_date=datetime.strptime(cur_promo.find('PromotionEndDate').text, '%Y-%m-%d'),
update_date=datetime.strptime(cur_promo.find('PromotionUpdateDate').text, '%Y-%m-%d %H:%M'), update_date=datetime.strptime(cur_promo.find('PromotionUpdateDate').text, '%Y-%m-%d %H:%M'),
code_items=[items_dict.get(item.find('ItemCode').text) for item in cur_promo.find_all('Item') items=[items_dict.get(item.find('ItemCode').text) for item in cur_promo.find_all('Item')
if items_dict.get(item.find('ItemCode').text)], if items_dict.get(item.find('ItemCode').text)],
) )
if is_valid_promo(cur_promo): if is_valid_promo(cur_promo):
promo_objs.append(cur_promo) promo_objs.append(cur_promo)
@@ -63,7 +62,7 @@ def is_valid_promo(promo: Promotion):
today_date = datetime.now() today_date = datetime.now()
not_expired = promo.end_date.date() >= today_date.date() not_expired = promo.end_date.date() >= today_date.date()
has_started = promo.start_date <= today_date has_started = promo.start_date <= today_date
has_products = len(promo.code_items) > 0 has_products = len(promo.items) > 0
in_promo_ignore_list = any(product in promo.content for product in PRODUCTS_TO_IGNORE) in_promo_ignore_list = any(product in promo.content for product in PRODUCTS_TO_IGNORE)
return not_expired and has_started and has_products and not in_promo_ignore_list return not_expired and has_started and has_products and not in_promo_ignore_list
@@ -80,3 +79,10 @@ def main_latest_promos(store_id: int, load_xml: bool, logger):
promotions = get_available_promos(store_id, load_xml) promotions = get_available_promos(store_id, load_xml)
promotions.sort(key=lambda promo: max(promo.update_date, promo.start_date), reverse=True) promotions.sort(key=lambda promo: max(promo.update_date, promo.start_date), reverse=True)
logger.info('\n'.join(str(promotion) for promotion in promotions)) logger.info('\n'.join(str(promotion) for promotion in promotions))
def get_promos_by_name(store_id: int, load_xml: bool, promo_name: str):
promotions = get_available_promos(store_id, load_xml)
for promo in promotions:
if promo_name in promo.content:
print(str(promo))