|
| 1 | +from datetime import time |
| 2 | +from typing import Any, Dict, List, Tuple |
| 3 | + |
| 4 | +from opening_hours import OpeningHours |
| 5 | + |
| 6 | +OPENED_24_7 = { |
| 7 | + "Mo": [(time(0, 0), time(23, 59))], |
| 8 | + "Tu": [(time(0, 0), time(23, 59))], |
| 9 | + "We": [(time(0, 0), time(23, 59))], |
| 10 | + "Th": [(time(0, 0), time(23, 59))], |
| 11 | + "Fr": [(time(0, 0), time(23, 59))], |
| 12 | + "Sa": [(time(0, 0), time(23, 59))], |
| 13 | + "Su": [(time(0, 0), time(23, 59))], |
| 14 | +} |
| 15 | + |
| 16 | +DAYS_OF_WEEK = ["Mo", "Tu", "We", "Th", "Fr", "Sa", "Su"] |
| 17 | + |
| 18 | + |
| 19 | +def merge_consecutive_tuples(tuples: list[tuple[Any, Any]]) -> list[tuple[Any, Any]]: |
| 20 | + result = [] |
| 21 | + if tuples: |
| 22 | + current_start, current_end = tuples[0] |
| 23 | + for next_start, next_end in tuples[1:]: |
| 24 | + if current_end == next_start: |
| 25 | + current_end = next_end |
| 26 | + else: |
| 27 | + result.append((current_start, current_end)) |
| 28 | + current_start, current_end = next_start, next_end |
| 29 | + result.append((current_start, current_end)) |
| 30 | + return result |
| 31 | + |
| 32 | + |
| 33 | +def split_and_clean(value: str) -> List[str]: |
| 34 | + """ |
| 35 | + Divise une chaîne par ', ' ou '; ' et nettoie chaque valeur. |
| 36 | + """ |
| 37 | + parts = [] |
| 38 | + for part in value.split("; "): |
| 39 | + parts.extend(part.split(", ")) |
| 40 | + return [part.strip() for part in parts if part.strip()] |
| 41 | + |
| 42 | + |
| 43 | +def interprete_opening_hours(opening_hours: str | None): |
| 44 | + """ |
| 45 | + Interprete les heures d'ouverture d'un lieu. |
| 46 | + """ |
| 47 | + |
| 48 | + if not opening_hours: |
| 49 | + return {} |
| 50 | + |
| 51 | + opening_hours_normalized = str(OpeningHours(opening_hours).normalize()) |
| 52 | + |
| 53 | + if opening_hours_normalized == "24/7": |
| 54 | + return OPENED_24_7 |
| 55 | + |
| 56 | + opening_hours_by_day_of_week: Dict[str, List[Tuple[time, time]]] = { |
| 57 | + day: [] for day in DAYS_OF_WEEK |
| 58 | + } |
| 59 | + |
| 60 | + def get_opening_hours(hours: str): |
| 61 | + hours_list = hours.split(",") |
| 62 | + result_hours = [] |
| 63 | + for hour in hours_list: |
| 64 | + open, close = hour.split("-") |
| 65 | + open_hour, open_minute = map(int, open.split(":")) |
| 66 | + close_hour, close_minute = map(int, close.split(":")) |
| 67 | + result_hours.append( |
| 68 | + (time(open_hour, open_minute), time(close_hour, close_minute)) |
| 69 | + ) |
| 70 | + return result_hours |
| 71 | + |
| 72 | + def get_opening_days(days: str): |
| 73 | + days_list = days.split(",") |
| 74 | + result_days = [] |
| 75 | + for ds in days_list: |
| 76 | + d = ds.split("-") |
| 77 | + if len(d) == 2: |
| 78 | + start_day, end_day = d |
| 79 | + start_day_index = DAYS_OF_WEEK.index(start_day) |
| 80 | + end_day_index = DAYS_OF_WEEK.index(end_day) |
| 81 | + result_days.extend(DAYS_OF_WEEK[start_day_index : end_day_index + 1]) |
| 82 | + else: |
| 83 | + result_days.extend(d) |
| 84 | + return result_days |
| 85 | + |
| 86 | + opening_hours_blocks = split_and_clean(opening_hours_normalized) |
| 87 | + for opening_hours_block in opening_hours_blocks: |
| 88 | + opening_hours_block_parts = opening_hours_block.split(" ") |
| 89 | + if len(opening_hours_block_parts) == 1: |
| 90 | + # only hours, for all days |
| 91 | + days = DAYS_OF_WEEK |
| 92 | + hours = get_opening_hours(opening_hours_block_parts[0]) |
| 93 | + elif len(opening_hours_block_parts) == 2: |
| 94 | + # days and hours |
| 95 | + days = get_opening_days(opening_hours_block_parts[0]) |
| 96 | + hours = get_opening_hours(opening_hours_block_parts[1]) |
| 97 | + else: |
| 98 | + raise ValueError(f"Invalid interval: {opening_hours_block}") |
| 99 | + |
| 100 | + for day in days: |
| 101 | + opening_hours_by_day_of_week[day].extend(hours) |
| 102 | + |
| 103 | + for day, hours in opening_hours_by_day_of_week.items(): |
| 104 | + opening_hours_by_day_of_week[day] = merge_consecutive_tuples(hours) |
| 105 | + |
| 106 | + return opening_hours_by_day_of_week |
0 commit comments