patch_reservation_codes.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #!/usr/bin/env python3
  2. """Patch wifi_key and/or door_code on a reservation that contains a given date.
  3. Usage:
  4. uv run python3 scripts/patch_reservation_codes.py 2026-07-05 --wifi stone-angel-market --door 0472
  5. uv run python3 scripts/patch_reservation_codes.py 2026-07-05 --wifi new-word-combo
  6. uv run python3 scripts/patch_reservation_codes.py 2026-07-05 --door 1234
  7. """
  8. import argparse
  9. import asyncio
  10. import json
  11. import sys
  12. from datetime import date
  13. from room_cal.cal import Calendar, DEFAULT_CALENDAR_NAME
  14. from room_cal._utils import safe_json_loads, filter_events, service_name
  15. _PREFIX = 'Guest: '
  16. def _build_description(xd: dict, wk: str, dc: str) -> str:
  17. svc = xd.get('service', '')
  18. return (
  19. f"Name: {xd.get('guest_name', '')}\n"
  20. f"Service: {service_name(svc)}\n"
  21. f"Reservation ID: {xd.get('reservation_id', '')}\n"
  22. f"Door: {dc}\n"
  23. f"WiFi: {wk}"
  24. )
  25. async def patch(target: date, new_wifi: str | None, new_door: str | None, calendar_name: str) -> None:
  26. cal = Calendar(calendar_name)
  27. events = await cal.fetch_date_range(target, target)
  28. reservations = filter_events(events, _PREFIX, all_day=False)
  29. if not reservations:
  30. print(f"No reservation found overlapping {target}", file=sys.stderr)
  31. sys.exit(1)
  32. if len(reservations) > 1:
  33. reservations.sort(key=lambda e: e['start'])
  34. event = reservations[-1]
  35. print(f"Multiple reservations overlap {target}, using the later one ({event['start'].date()} – {event['end'].date()})")
  36. else:
  37. event = reservations[0]
  38. xd = safe_json_loads(event['x_data']) or {}
  39. old_wifi = xd.get('wifi_key', '(none)')
  40. old_door = xd.get('door_code', '(none)')
  41. wk = new_wifi if new_wifi is not None else old_wifi
  42. dc = new_door if new_door is not None else old_door
  43. print(f"Reservation: {event['title']} ({event['start'].date()} – {event['end'].date()})")
  44. if new_wifi is not None:
  45. print(f" wifi_key: {old_wifi!r} → {wk!r}")
  46. if new_door is not None:
  47. print(f" door_code: {old_door!r} → {dc!r}")
  48. confirm = input("Apply? [y/N] ").strip().lower()
  49. if confirm != 'y':
  50. print("Aborted.")
  51. return
  52. xd['wifi_key'] = wk
  53. xd['door_code'] = dc
  54. new_x_data = json.dumps(xd)
  55. new_description = _build_description(xd, wk, dc)
  56. await cal.update_event(event, description=new_description, x_data=new_x_data)
  57. print("Updated.")
  58. def main():
  59. parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
  60. parser.add_argument('date', type=date.fromisoformat, help='Any date within the reservation (YYYY-MM-DD)')
  61. parser.add_argument('--wifi', metavar='WIFI_KEY', help='New wifi_key string')
  62. parser.add_argument('--door', metavar='DOOR_CODE', help='New door_code string')
  63. parser.add_argument('--calendar', default=DEFAULT_CALENDAR_NAME, help=f'Calendar name (default: {DEFAULT_CALENDAR_NAME})')
  64. args = parser.parse_args()
  65. if args.wifi is None and args.door is None:
  66. parser.error('Provide at least one of --wifi or --door')
  67. asyncio.run(patch(args.date, args.wifi, args.door, args.calendar))
  68. if __name__ == '__main__':
  69. main()