# Calendar Integration for Rivet

## Files

1. **calendar-check.py** - Main calendar integration script
2. **google-refresh.py** - Token refresh utility
3. **google-oauth.json** - OAuth credentials (located at `/root/.clawdbot/google-oauth.json`)

## Features

- Fetches today's and tomorrow's events from Google Calendar
- Automatically refreshes access tokens when expired (401 error)
- Detects schedule conflicts (overlapping events)
- Formats output for morning/evening briefs
- Saves summary to `/home/ccuser/rateright-growth/rivet/memory/calendar-summary.md`

## Usage

### Run manually:
```bash
cd /home/ccuser/rateright-growth/rivet
python3 scripts/calendar-check.py
```

### Output:
The script creates `memory/calendar-summary.md` with:
- Event list (title, time, location, description snippet)
- Conflict detection
- Statistics
- Next upcoming event

### Integration with Rivet:
Add to `HEARTBEAT.md` or cron for regular checks:
```bash
# Example cron for morning brief (8 AM Sydney time)
0 8 * * * cd /home/ccuser/rateright-growth/rivet && python3 scripts/calendar-check.py
```

## Token Management

The script automatically handles token expiration:
1. Reads token from `/root/.clawdbot/google-oauth.json`
2. If API returns 401 (unauthorized), runs `google-refresh.py`
3. Refresh script uses refresh token to get new access token
4. Updates the OAuth file with new token
5. Retries the calendar request

## Timezone Handling

All times are in Australia/Sydney timezone. The script:
- Converts API times (UTC) to Sydney time
- Handles all-day events
- Formats times for display

## Dependencies

- Python 3
- `requests` library
- `pytz` library (for timezone handling)

Both are typically available in standard Python environments.

## Testing

Test the script:
```bash
# Test with current token
python3 scripts/calendar-check.py

# Test token refresh (simulate expired token)
python3 scripts/google-refresh.py
```

## Sample Output

See `memory/calendar-summary.md` for example output format.