major refactor (untested)

This commit is contained in:
Aron Petau 2025-12-27 11:59:04 +01:00
parent a29412b4da
commit a77a0c0393
22 changed files with 1037 additions and 24 deletions

View file

@ -1,4 +1,4 @@
# Copilot Instructions for inberlin-monitor
# Copilot Instructions for wohn-bot
## Project Overview
@ -6,22 +6,29 @@ A Python-based apartment monitoring bot for Berlin's public housing portal (inbe
## Architecture
**Single-file monolith** (`monitor.py`, ~1600 lines) with five main classes:
- `InBerlinMonitor` - Core scraping/monitoring loop for inberlinwohnen.de, login handling, listing detection
- `WGCompanyMonitor` - Monitors wgcompany.de WG rooms with configurable search filters
- `ApplicationHandler` - Company-specific form automation (each `_apply_*` method handles one housing company)
- `TelegramBot` - Command handling via long-polling in a daemon thread
- Main loop runs synchronous with `asyncio.get_event_loop().run_until_complete()` for Playwright calls
**Modularized structure** with the following key components:
**Data flow**: Fetch listings → Compare with `listings.json` / `wgcompany_listings.json` → Detect new → Log to CSV → Auto-apply if autopilot enabled (inberlin only) → Save to `applications.json` → Send Telegram notification
- `main.py`: Entry point for the bot.
- `handlers/`: Contains company-specific handlers for auto-apply functionality. Each handler is responsible for automating the application process for a specific housing company. Includes:
- `howoge_handler.py`
- `gewobag_handler.py`
- `degewo_handler.py`
- `gesobau_handler.py`
- `stadtundland_handler.py`
- `wbm_handler.py`
- `base_handler.py`: Provides shared functionality for all handlers.
- `application_handler.py`: Delegates application tasks to the appropriate handler based on the company.
- `telegram_bot.py`: Handles Telegram bot commands and notifications.
**Data flow**: Fetch listings → Compare with `listings.json` / `wgcompany_listings.json` → Detect new → Log to CSV → Auto-apply if autopilot enabled → Save to `applications.json` → Send Telegram notification.
## Key Patterns
### Company-specific handlers
Each housing company has a dedicated `_apply_{company}()` method in `ApplicationHandler`. When adding support for a new company:
1. Add detection in `_detect_company()` (line ~350)
2. Add handler call in `apply()` switch (line ~330)
3. Implement `_apply_newcompany()` following existing patterns (cookie dismiss → find button → fill form → submit → screenshot)
Each housing company has a dedicated handler in the `handlers/` directory. When adding support for a new company:
1. Create a new handler file in `handlers/` (e.g., `newcompany_handler.py`).
2. Implement the handler by extending `BaseHandler` and overriding necessary methods.
3. Update `application_handler.py` to include the new handler in the `handlers` dictionary.
### Listing identification
Listings are hashed by `md5(key_fields)[:12]` to generate stable IDs:
@ -39,13 +46,13 @@ Listings are hashed by `md5(key_fields)[:12]` to generate stable IDs:
### Run locally
```bash
# Install deps (requires Playwright)
# Install dependencies (requires Playwright)
pip install -r requirements.txt
playwright install chromium
# Set env vars and run
export TELEGRAM_BOT_TOKEN=... TELEGRAM_CHAT_ID=...
python monitor.py
python main.py
```
### Docker (production)
@ -70,12 +77,30 @@ WGcompany: `WGCOMPANY_ENABLED`, `WGCOMPANY_MIN_SIZE`, `WGCOMPANY_MAX_SIZE`, `WGC
## Common Tasks
### Fix a broken company handler
Check `data/*_nobtn_*.png` screenshots and `data/debug_page.html` to see actual page structure. Update selectors in the corresponding `_apply_{company}()` method.
Check `data/*_nobtn_*.png` screenshots and `data/debug_page.html` to see actual page structure. Update selectors in the corresponding handler file in `handlers/`.
### Add Telegram command
1. Add case in `TelegramBot._handle_update()` (line ~95)
2. Implement `_handle_{command}_command()` method
1. Add a case in `TelegramBot._handle_update()`.
2. Implement the corresponding `_handle_{command}_command()` method.
### Modify listing extraction
- InBerlin: Update regex patterns in `InBerlinMonitor.fetch_listings()`. Test against `data/debug_page.html`.
- WGcompany: Update parsing in `WGCompanyMonitor.fetch_listings()`. Test against `data/wgcompany_debug.html`.
## Unit Tests
### Overview
The project includes unit tests to ensure functionality and reliability. Key test files:
- `tests/test_telegram_bot.py`: Tests the Telegram bot's commands and messaging functionality.
- `tests/test_error_rate_plot.py`: Tests the error rate plot generator for autopilot applications.
### Running Tests
To run the tests, use:
```bash
pytest tests/
```
Ensure all dependencies are installed and the environment is configured correctly before running the tests.