From 0504e1913b7147c5b488045ef38cf0c431fe6426 Mon Sep 17 00:00:00 2001 From: fmk17 <fmk17@inf.ufpr.br> Date: Fri, 26 Aug 2022 12:42:32 -0300 Subject: [PATCH] Fix two digit year and timezone issues --- crawler.py | 8 ++++++-- main.py | 5 +++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/crawler.py b/crawler.py index 6af929f..9c15030 100644 --- a/crawler.py +++ b/crawler.py @@ -17,7 +17,7 @@ LocationDays = namedtuple("LocationDays", "days, location, update_datetime") cached_update_times: Dict[Location, datetime] = dict() cached_responses: Dict[Location, LocationDays] = dict() -DATE_REGEX = r": (\d{1,2})\/(\d{1,2})\/(\d{4})" +DATE_REGEX = r": (\d{1,2})\/(\d{1,2})\/(\d{2,4})" logger = logging.getLogger("crawler") @@ -52,6 +52,8 @@ def get_location_days( if date_re is None: break d, m, y = map(int, date_re.groups()) + if y < 100: + y += 2000 table_children = iter(table_node.select("td")) menus = [] for title_node, item_nodes in zip(table_children, table_children): @@ -90,7 +92,9 @@ def get_location_days( name=item_name, indicators=frozenset(item_indicators) ) ) - menus.append(Menu(meal_name=title_node.text, items=tuple(items))) + menus.append( + Menu(meal_name=title_node.text.strip(), items=tuple(items)) + ) days.append(Day(date=date(y, m, d), date_raw=date_text, menus=menus)) cached_update_times[location] = datetime.now() diff --git a/main.py b/main.py index c474302..84a1f77 100644 --- a/main.py +++ b/main.py @@ -28,6 +28,7 @@ from telegram.ext import ( from crawler import Day, Location, LocationDays, get_location_days from database import ( + CURITIBA_TZ, delete_all_schedules_from_user, delete_menu_item_indicator_preference, delete_schedule_by_key, @@ -125,7 +126,7 @@ def format_location_days(location_days, visible_menu_item_indicators): strings.append( "<i>Atualizado às " - + update_datetime.strftime("%H:%M:%S") + + update_datetime.astimezone(CURITIBA_TZ).strftime("%H:%M:%S") + " de hoje</i>" ) return "\n\n".join(strings) @@ -862,7 +863,7 @@ def try_send_schedule_again(context: CallbackContext) -> None: def send_scheduled(context: CallbackContext) -> None: - dt = datetime.now() + dt = datetime.now(tz=CURITIBA_TZ) dt += timedelta(minutes=EVERY_X_MINUTES / 2) dt -= timedelta( minutes=dt.minute % EVERY_X_MINUTES, -- GitLab