_scripts.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import os
  2. import subprocess
  3. import sys
  4. from pathlib import Path
  5. from room_cal._utils import get_config
  6. def _data_home() -> Path:
  7. override = os.environ.get('ROOM_CAL_DATA_HOME')
  8. return Path(override) if override else Path.home() / '.local' / 'room_cal'
  9. def _ts_dir() -> Path:
  10. return Path(__file__).parent / 'typescript'
  11. def _npm_env() -> dict:
  12. dh = _data_home()
  13. env = os.environ.copy()
  14. env.setdefault('ROOM_CAL_DATA_HOME', str(dh))
  15. env.setdefault('PLAYWRIGHT_BROWSERS_PATH', str(dh / '.playwright-browsers'))
  16. api_url = get_config().api_url
  17. if api_url:
  18. env.setdefault('ROOM_CAL_API_URL', api_url)
  19. return env
  20. def _ensure_installed(ts_dir: Path) -> None:
  21. if not (ts_dir / 'node_modules').exists():
  22. print('=== npm install (service-api) ===')
  23. subprocess.run(['npm', 'install'], cwd=ts_dir, check=True)
  24. print()
  25. def refresh_booking_cookies() -> None:
  26. ts_dir = _ts_dir()
  27. _ensure_installed(ts_dir)
  28. env = _npm_env()
  29. env['HEADLESS'] = '1'
  30. try:
  31. subprocess.run(['npm', 'run', 'refresh-booking'], cwd=ts_dir, env=env, check=True)
  32. except subprocess.CalledProcessError as e:
  33. from .types import ServiceAuthError
  34. raise ServiceAuthError(f'Booking.com session expired — re-run without HEADLESS to re-authenticate (exit {e.returncode})') from None
  35. def refresh_airbnb_cookies() -> None:
  36. ts_dir = _ts_dir()
  37. _ensure_installed(ts_dir)
  38. env = _npm_env()
  39. env['HEADLESS'] = '1'
  40. try:
  41. subprocess.run(['npm', 'run', 'refresh-airbnb'], cwd=ts_dir, env=env, check=True)
  42. except subprocess.CalledProcessError as e:
  43. from .types import ServiceAuthError
  44. raise ServiceAuthError(f'Airbnb session expired — re-run without HEADLESS to re-authenticate (exit {e.returncode})') from None
  45. def trigger_booking_chat_auth() -> None:
  46. ts_dir = _ts_dir()
  47. _ensure_installed(ts_dir)
  48. result = subprocess.run(['npm', 'run', 'booking-2fa', '--'] + sys.argv[1:], cwd=ts_dir, env=_npm_env())
  49. sys.exit(result.returncode)
  50. def refresh_cookies() -> None:
  51. ts_dir = _ts_dir()
  52. _ensure_installed(ts_dir)
  53. result = subprocess.run(['npm', 'run', 'refresh-all', '--'] + sys.argv[1:], cwd=ts_dir, env=_npm_env())
  54. sys.exit(result.returncode)