codomo-alerts 0.2.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- codomo_alerts/__init__.py +5 -0
- codomo_alerts/__main__.py +5 -0
- codomo_alerts/catalog.py +219 -0
- codomo_alerts/cli.py +401 -0
- codomo_alerts/client.py +152 -0
- codomo_alerts/codomo.py +477 -0
- codomo_alerts/config.py +370 -0
- codomo_alerts/dashboard.py +204 -0
- codomo_alerts/demo.py +128 -0
- codomo_alerts/mcp_server.py +86 -0
- codomo_alerts/messages.py +109 -0
- codomo_alerts/notify.py +60 -0
- codomo_alerts/runtime.py +62 -0
- codomo_alerts/schedule.py +56 -0
- codomo_alerts/search.py +219 -0
- codomo_alerts/store.py +256 -0
- codomo_alerts/term.py +62 -0
- codomo_alerts/tracker.py +98 -0
- codomo_alerts/upstream.py +93 -0
- codomo_alerts/watcher.py +334 -0
- codomo_alerts/web/app.css +219 -0
- codomo_alerts/web/app.js +211 -0
- codomo_alerts/web/favicon.svg +1 -0
- codomo_alerts/web/index.html +75 -0
- codomo_alerts/wizard.py +161 -0
- codomo_alerts-0.2.2.dist-info/METADATA +372 -0
- codomo_alerts-0.2.2.dist-info/RECORD +30 -0
- codomo_alerts-0.2.2.dist-info/WHEEL +4 -0
- codomo_alerts-0.2.2.dist-info/entry_points.txt +2 -0
- codomo_alerts-0.2.2.dist-info/licenses/LICENSE +21 -0
codomo_alerts/catalog.py
ADDED
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
"""Names used by Codomo, with readable labels and the words people type."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import re
|
|
6
|
+
|
|
7
|
+
# our status name -> the site's publish_mode
|
|
8
|
+
STATUS = {"available": "direct_booking", "interest": "interest"}
|
|
9
|
+
|
|
10
|
+
# countries with homes on codomo.eu on 2026-11-17; a new one is read when the site lists it
|
|
11
|
+
MARKETS = {"nl": "Netherlands", "es": "Spain", "pt": "Portugal", "at": "Austria"}
|
|
12
|
+
MARKET_ALIASES = {
|
|
13
|
+
"holland": "nl", "the netherlands": "nl", "nederland": "nl", "niederlande": "nl", "paises bajos": "nl",
|
|
14
|
+
"spain": "es", "espana": "es", "españa": "es", "spanje": "es", "spanien": "es", "espanha": "es",
|
|
15
|
+
"austria": "at", "osterreich": "at", "österreich": "at", "oostenrijk": "at", "áustria": "at",
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
# Cities on codomo.eu on 2026-11-15, with their market. `codomo-alerts cities` shows new ones.
|
|
19
|
+
CITIES: dict[str, str] = {
|
|
20
|
+
"Amersfoort": "nl", "Amsterdam": "nl", "Arnhem": "nl", "Capelle aan den IJssel": "nl",
|
|
21
|
+
"Delft": "nl", "Den Bosch": "nl", "Diemen": "nl", "Dordrecht": "nl", "Eindhoven": "nl",
|
|
22
|
+
"Groningen": "nl", "Haarlem": "nl", "Helmond": "nl", "Leiden": "nl", "Maarssen": "nl",
|
|
23
|
+
"Maastricht": "nl", "Nieuwegein": "nl", "Nijmegen": "nl", "Rijswijk": "nl", "Rotterdam": "nl",
|
|
24
|
+
"Sittard": "nl", "The Hague": "nl", "Tilburg": "nl", "Utrecht": "nl", "Velp": "nl", "Zeist": "nl",
|
|
25
|
+
"Zoetermeer": "nl",
|
|
26
|
+
"Linz": "at", "Granada": "es", "Carcavelos": "pt",
|
|
27
|
+
}
|
|
28
|
+
# the site's URL names where they differ from the city name
|
|
29
|
+
SLUGS = {"The Hague": "den-haag"}
|
|
30
|
+
|
|
31
|
+
CITY_ALIASES = {
|
|
32
|
+
"den haag": "the hague",
|
|
33
|
+
"hague": "the hague",
|
|
34
|
+
"'s gravenhage": "the hague",
|
|
35
|
+
"s gravenhage": "the hague",
|
|
36
|
+
"'s hertogenbosch": "den bosch",
|
|
37
|
+
"s hertogenbosch": "den bosch",
|
|
38
|
+
"hertogenbosch": "den bosch",
|
|
39
|
+
"capelle": "capelle aan den ijssel",
|
|
40
|
+
"lisbon": "carcavelos",
|
|
41
|
+
"lisboa": "carcavelos",
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
MAX_BEDROOMS = 9
|
|
45
|
+
|
|
46
|
+
CONTRACTS = {"indefinite": "Indefinite", "2y": "Max 2 years", "1y": "Max 1 year",
|
|
47
|
+
"6m": "Max 6 months", "4m": "Max 4 months"}
|
|
48
|
+
CONTRACT_KEYS = {
|
|
49
|
+
"indefinite": ["indefinite"],
|
|
50
|
+
"2y": ["2y"],
|
|
51
|
+
"1y": ["1y"],
|
|
52
|
+
"6m": ["6m"],
|
|
53
|
+
"4m": ["4m"],
|
|
54
|
+
"long": ["indefinite", "2y", "1y"],
|
|
55
|
+
"short": ["short"], # every maximum under a year
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
FURNISHING = {"fully-furnished": "Fully furnished", "furnished": "Furnished",
|
|
59
|
+
"semi-furnished": "Semi-furnished", "unfurnished": "Unfurnished"}
|
|
60
|
+
FURNISHING_ALIASES = {
|
|
61
|
+
"fully": "fully-furnished", "semi": "semi-furnished", "none": "unfurnished",
|
|
62
|
+
"volledig-gemeubileerd": "fully-furnished", "gemeubileerd": "furnished",
|
|
63
|
+
"gestoffeerd": "semi-furnished", "ongemeubileerd": "unfurnished", "kaal": "unfurnished",
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
GENDERS = {"female": "Female only", "male": "Male only"}
|
|
67
|
+
GENDER_ALIASES = {"f": "female", "woman": "female", "women": "female", "m": "male", "man": "male", "men": "male"}
|
|
68
|
+
|
|
69
|
+
PERSONS = {"one": 1, "single": 1, "couple": 2, "two": 2, "three": 3, "four": 4, "family": 3}
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def norm(text: str) -> str:
|
|
73
|
+
return " ".join(str(text).strip().lower().replace("’", "'").replace("-", " ").split())
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def city_name(name: str) -> str:
|
|
77
|
+
"""The city's name as Codomo lists it: 'den-haag' and 'Den Haag' -> 'The Hague'."""
|
|
78
|
+
key = norm(name)
|
|
79
|
+
key = CITY_ALIASES.get(key, key)
|
|
80
|
+
for known in CITIES:
|
|
81
|
+
if norm(known) == key:
|
|
82
|
+
return known
|
|
83
|
+
raise KeyError(name)
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def same_city(a: str, b: str) -> bool:
|
|
87
|
+
ka, kb = norm(a), norm(b)
|
|
88
|
+
return CITY_ALIASES.get(ka, ka) == CITY_ALIASES.get(kb, kb)
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def city_label(name: str | None) -> str:
|
|
92
|
+
if not name:
|
|
93
|
+
return ""
|
|
94
|
+
try:
|
|
95
|
+
return city_name(name)
|
|
96
|
+
except KeyError:
|
|
97
|
+
return name
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def city_slug(name: str) -> str:
|
|
101
|
+
return SLUGS.get(name, norm(name).replace(" ", "-"))
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def market_of(name: str) -> str | None:
|
|
105
|
+
return CITIES.get(city_label(name))
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def market(value: str) -> list[str]:
|
|
109
|
+
"""'Spain', 'españa' and 'es' -> ['es']. 'all' -> [] (every country)."""
|
|
110
|
+
key = norm(value)
|
|
111
|
+
if key in ("all", "*", "any", "every", "everywhere"):
|
|
112
|
+
return []
|
|
113
|
+
if key in MARKETS:
|
|
114
|
+
return [key]
|
|
115
|
+
if key in MARKET_ALIASES:
|
|
116
|
+
return [MARKET_ALIASES[key]]
|
|
117
|
+
for code, name in MARKETS.items():
|
|
118
|
+
if norm(name) == key:
|
|
119
|
+
return [code]
|
|
120
|
+
raise KeyError(value)
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
def market_label(code: str) -> str:
|
|
124
|
+
return MARKETS.get(code, code.upper())
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
def gender(value: str) -> str:
|
|
128
|
+
key = norm(value)
|
|
129
|
+
key = GENDER_ALIASES.get(key, key)
|
|
130
|
+
if key not in GENDERS:
|
|
131
|
+
raise KeyError(value)
|
|
132
|
+
return key
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
def gender_of(tags: list[str]) -> str | None:
|
|
136
|
+
"""The gender a home is for when Codomo marks it 'Female only' or 'Male only'."""
|
|
137
|
+
for tag in tags:
|
|
138
|
+
for key, label in GENDERS.items():
|
|
139
|
+
if norm(tag) == norm(label):
|
|
140
|
+
return key
|
|
141
|
+
return None
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
def bedrooms(value: str) -> list[int]:
|
|
145
|
+
"""'studio' (and 'loft') is 0. '2', '2+' (two or more), '1-3'."""
|
|
146
|
+
key = norm(value).replace("bedrooms", "").replace("bedroom", "").strip()
|
|
147
|
+
key = {"studio": "0", "loft": "0"}.get(key, key)
|
|
148
|
+
if re.fullmatch(r"\d+\+", key):
|
|
149
|
+
return list(range(int(key[:-1]), MAX_BEDROOMS + 1))
|
|
150
|
+
m = re.fullmatch(r"(\w+) ?- ?(\w+)", value.strip().lower())
|
|
151
|
+
if m:
|
|
152
|
+
a, b = ({"studio": "0", "loft": "0"}.get(g, g) for g in m.groups())
|
|
153
|
+
if a.isdigit() and b.isdigit():
|
|
154
|
+
lo, hi = sorted((int(a), int(b)))
|
|
155
|
+
return list(range(lo, hi + 1))
|
|
156
|
+
if key.isdigit() and int(key) <= MAX_BEDROOMS:
|
|
157
|
+
return [int(key)]
|
|
158
|
+
raise KeyError(value)
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
def bedrooms_label(n: int | None) -> str:
|
|
162
|
+
if n is None:
|
|
163
|
+
return ""
|
|
164
|
+
if n == 0:
|
|
165
|
+
return "Studio"
|
|
166
|
+
return "1 bedroom" if n == 1 else f"{n} bedrooms"
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
def contracts(value: str) -> list[str]:
|
|
170
|
+
key = norm(value).replace(" ", "").replace("_", "")
|
|
171
|
+
key = {"shortstay": "short", "longstay": "long", "1year": "1y", "2years": "2y",
|
|
172
|
+
"1ymax": "1y", "6mmax": "6m", "4mmax": "4m"}.get(key, key)
|
|
173
|
+
if key not in CONTRACT_KEYS:
|
|
174
|
+
raise KeyError(value)
|
|
175
|
+
return CONTRACT_KEYS[key]
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
def contract_ok(key: str | None, wanted: list[str]) -> bool:
|
|
179
|
+
if not wanted:
|
|
180
|
+
return True
|
|
181
|
+
if key is None:
|
|
182
|
+
return False
|
|
183
|
+
return key in wanted or ("short" in wanted and key.endswith("m"))
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
def contract_label(key: str | None) -> str:
|
|
187
|
+
if not key:
|
|
188
|
+
return ""
|
|
189
|
+
if key in CONTRACTS:
|
|
190
|
+
return CONTRACTS[key]
|
|
191
|
+
m = re.fullmatch(r"(\d+)m", key)
|
|
192
|
+
return f"Max {m[1]} months" if m else key
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
def furnishing(value: str) -> str:
|
|
196
|
+
key = norm(value).replace(" ", "-")
|
|
197
|
+
key = FURNISHING_ALIASES.get(key, key)
|
|
198
|
+
if key not in FURNISHING:
|
|
199
|
+
raise KeyError(value)
|
|
200
|
+
return key
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
def furnishing_of(label: str | None) -> str | None:
|
|
204
|
+
"""The key for what a home card says ('Semi furnished' -> 'semi-furnished')."""
|
|
205
|
+
if not label:
|
|
206
|
+
return None
|
|
207
|
+
try:
|
|
208
|
+
return furnishing(label)
|
|
209
|
+
except KeyError:
|
|
210
|
+
return None
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
def persons(value) -> int:
|
|
214
|
+
key = norm(str(value))
|
|
215
|
+
if key.isdigit() and 1 <= int(key) <= 12:
|
|
216
|
+
return int(key)
|
|
217
|
+
if key in PERSONS:
|
|
218
|
+
return PERSONS[key]
|
|
219
|
+
raise KeyError(value)
|
codomo_alerts/cli.py
ADDED
|
@@ -0,0 +1,401 @@
|
|
|
1
|
+
"""Command line entry point."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import argparse
|
|
6
|
+
import json
|
|
7
|
+
import logging
|
|
8
|
+
import os
|
|
9
|
+
import signal
|
|
10
|
+
import socket
|
|
11
|
+
import sqlite3
|
|
12
|
+
import statistics
|
|
13
|
+
import sys
|
|
14
|
+
import threading
|
|
15
|
+
import time
|
|
16
|
+
import urllib.request
|
|
17
|
+
from collections import Counter
|
|
18
|
+
from datetime import timedelta
|
|
19
|
+
from pathlib import Path
|
|
20
|
+
|
|
21
|
+
from . import __version__, catalog, messages, schedule
|
|
22
|
+
from .client import Client, CodomoError
|
|
23
|
+
from .config import Config, ConfigError, build, load
|
|
24
|
+
from .notify import Notifier, masked
|
|
25
|
+
from .runtime import AlreadyRunning, Lock, db_path, setup_logging
|
|
26
|
+
from .search import contract, discover_cities, fetch
|
|
27
|
+
from .store import Store, iso, parse, utcnow
|
|
28
|
+
from .term import bad, bold, dim, ok, table, warn
|
|
29
|
+
|
|
30
|
+
log = logging.getLogger("codomo_alerts")
|
|
31
|
+
DAYS = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
|
|
32
|
+
LIST_PAGES = 30 # home pages `list` reads for the maximum stay
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def _configured(cfg: Config) -> bool:
|
|
36
|
+
return cfg.source is not None or any(k.startswith("CODOMO_") for k in os.environ)
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
# Commands
|
|
40
|
+
|
|
41
|
+
def cmd_watch(cfg: Config, args) -> int:
|
|
42
|
+
from .dashboard import serve
|
|
43
|
+
from .watcher import Watcher
|
|
44
|
+
|
|
45
|
+
if getattr(args, "port", None):
|
|
46
|
+
cfg.dashboard.port = args.port
|
|
47
|
+
if getattr(args, "no_dashboard", False):
|
|
48
|
+
cfg.dashboard.enabled = False
|
|
49
|
+
setup_logging("DEBUG" if args.verbose else cfg.log_level)
|
|
50
|
+
for w in cfg.warnings:
|
|
51
|
+
log.warning(w)
|
|
52
|
+
log.info("codomo-alerts %s", __version__)
|
|
53
|
+
log.info("Search: %s", cfg.search.summary())
|
|
54
|
+
notifier = Notifier(cfg.notify.urls)
|
|
55
|
+
for url in notifier.invalid:
|
|
56
|
+
log.warning("Skipping a notification link Apprise doesn't understand: %s", masked(url))
|
|
57
|
+
if not notifier.enabled:
|
|
58
|
+
log.warning("No notification links set, alerts only go to this log and the dashboard")
|
|
59
|
+
|
|
60
|
+
try:
|
|
61
|
+
with Lock(cfg.data_dir):
|
|
62
|
+
store = Store(db_path(cfg))
|
|
63
|
+
watcher = Watcher(cfg, store, notifier=notifier)
|
|
64
|
+
if args.once:
|
|
65
|
+
watcher.tick()
|
|
66
|
+
store.close()
|
|
67
|
+
return 0
|
|
68
|
+
server = serve(store, cfg) if cfg.dashboard.enabled else None
|
|
69
|
+
|
|
70
|
+
def stop(*_):
|
|
71
|
+
watcher.stop.set()
|
|
72
|
+
|
|
73
|
+
signal.signal(signal.SIGTERM, stop)
|
|
74
|
+
signal.signal(signal.SIGINT, stop)
|
|
75
|
+
try:
|
|
76
|
+
watcher.run()
|
|
77
|
+
finally:
|
|
78
|
+
if server:
|
|
79
|
+
server.shutdown()
|
|
80
|
+
store.close()
|
|
81
|
+
log.info("Stopped")
|
|
82
|
+
except AlreadyRunning as e:
|
|
83
|
+
print(bad(str(e)), file=sys.stderr)
|
|
84
|
+
return 1
|
|
85
|
+
except (OSError, sqlite3.Error) as e:
|
|
86
|
+
reason = getattr(e, "strerror", None) or str(e)
|
|
87
|
+
print(bad(f"Can't write to {cfg.data_dir}: {reason}"), file=sys.stderr)
|
|
88
|
+
if Path("/.dockerenv").exists():
|
|
89
|
+
print(dim("Mounted a folder at /data? Make it writable for user 1000: sudo chown 1000 ./data"),
|
|
90
|
+
file=sys.stderr)
|
|
91
|
+
return 1
|
|
92
|
+
return 0
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def cmd_health(cfg: Config, args) -> int:
|
|
96
|
+
"""Exit 0 when the watcher's dashboard answers (used by the Docker health check)."""
|
|
97
|
+
if not cfg.dashboard.enabled:
|
|
98
|
+
return 0
|
|
99
|
+
host = "127.0.0.1" if cfg.dashboard.host in ("0.0.0.0", "::", "") else cfg.dashboard.host
|
|
100
|
+
try:
|
|
101
|
+
with urllib.request.urlopen(f"http://{host}:{cfg.dashboard.port}/healthz", timeout=4) as r:
|
|
102
|
+
return 0 if r.status == 200 else 1
|
|
103
|
+
except OSError:
|
|
104
|
+
return 1
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def cmd_list(cfg: Config, args) -> int:
|
|
108
|
+
if args.city or args.country:
|
|
109
|
+
picked = build({"search": {"cities": args.city or [], "countries": args.country or []}}, {}).search
|
|
110
|
+
cfg.search.cities, cfg.search.countries = picked.cities, picked.countries
|
|
111
|
+
cfg.search.exclude_cities = []
|
|
112
|
+
client = Client(cfg.base_url)
|
|
113
|
+
status = "interest" if args.interest else "available"
|
|
114
|
+
store = Store(db_path(cfg)) if db_path(cfg).exists() else None
|
|
115
|
+
try:
|
|
116
|
+
found = sorted(fetch(client, cfg.search, status).values(), key=lambda x: x.price or 0)
|
|
117
|
+
if store:
|
|
118
|
+
for x in found:
|
|
119
|
+
store.known_details(x)
|
|
120
|
+
# the maximum stay is only on each home's own page
|
|
121
|
+
for x in [x for x in found if x.contract is None][:LIST_PAGES]:
|
|
122
|
+
x.contract = contract(client, x)
|
|
123
|
+
if cfg.search.contracts:
|
|
124
|
+
found = [x for x in found if catalog.contract_ok(x.contract, cfg.search.contracts)
|
|
125
|
+
or x.contract is None]
|
|
126
|
+
except CodomoError as e:
|
|
127
|
+
print(bad(str(e)), file=sys.stderr)
|
|
128
|
+
return 1
|
|
129
|
+
if args.json:
|
|
130
|
+
print(json.dumps([{"name": x.name, "url": x.url, "price": x.price, "city": x.city_name,
|
|
131
|
+
"bedrooms": x.bedrooms_name, "contract": x.contract_name,
|
|
132
|
+
"furnishing": x.furnishing_name, "gender": x.gender_name, "status": x.status,
|
|
133
|
+
"interest": x.interest}
|
|
134
|
+
for x in found], indent=2))
|
|
135
|
+
return 0
|
|
136
|
+
print(dim(cfg.search.summary()))
|
|
137
|
+
if not found:
|
|
138
|
+
print("Nothing matches right now.")
|
|
139
|
+
return 0
|
|
140
|
+
rows = [[messages.money(x.price), x.name, x.city_name, x.bedrooms_name, x.contract_name or "?"]
|
|
141
|
+
for x in found]
|
|
142
|
+
print(table(rows, right={0}))
|
|
143
|
+
print()
|
|
144
|
+
for x in found[:3]:
|
|
145
|
+
print(dim(x.url))
|
|
146
|
+
more = f", {len(found) - 3} more links with --json" if len(found) > 3 else ""
|
|
147
|
+
print(dim(f"{len(found)} home{'s' if len(found) != 1 else ''}{more}"))
|
|
148
|
+
if args.interest:
|
|
149
|
+
print(dim(messages.SHORTLIST))
|
|
150
|
+
return 0
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
def cmd_cities(cfg: Config, args) -> int:
|
|
154
|
+
order = list(catalog.MARKETS)
|
|
155
|
+
rows = [[name, catalog.market_label(market)]
|
|
156
|
+
for name, market in sorted(catalog.CITIES.items(), key=lambda kv: (order.index(kv[1]), kv[0]))]
|
|
157
|
+
print(table(rows))
|
|
158
|
+
try:
|
|
159
|
+
live = discover_cities(Client(cfg.base_url))
|
|
160
|
+
unknown = [c for c in live if not any(catalog.same_city(c, k) for k in catalog.CITIES)]
|
|
161
|
+
countries = sorted({m for m in live.values() if m not in catalog.MARKETS})
|
|
162
|
+
except CodomoError:
|
|
163
|
+
unknown, countries = [], []
|
|
164
|
+
if unknown:
|
|
165
|
+
print()
|
|
166
|
+
print(warn("New cities on Codomo: " + ", ".join(unknown)))
|
|
167
|
+
print(dim("Update codomo-alerts to use them in your search."))
|
|
168
|
+
if countries:
|
|
169
|
+
print(warn("New countries on Codomo: " + ", ".join(c.upper() for c in countries)))
|
|
170
|
+
print(dim("A search without cities or countries already reads them."))
|
|
171
|
+
return 0
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
def cmd_test(cfg: Config, args) -> int:
|
|
175
|
+
if not cfg.notify.urls:
|
|
176
|
+
print(warn("No notification links in your config. Run codomo-alerts init or set CODOMO_NOTIFY."))
|
|
177
|
+
return 1
|
|
178
|
+
results = Notifier(cfg.notify.urls).test(*messages.test())
|
|
179
|
+
for url, sent in results:
|
|
180
|
+
print(ok(url) if sent else bad(f"{url} (failed)"))
|
|
181
|
+
return 0 if all(sent for _, sent in results) else 1
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
def cmd_doctor(cfg: Config, args) -> int:
|
|
185
|
+
good = True
|
|
186
|
+
print(ok(f"Config: {cfg.source}") if cfg.source else ok("Config: environment variables only"))
|
|
187
|
+
for w in cfg.warnings:
|
|
188
|
+
print(warn(w))
|
|
189
|
+
print(ok(f"Search: {cfg.search.summary()}"))
|
|
190
|
+
|
|
191
|
+
try:
|
|
192
|
+
cfg.data_dir.mkdir(parents=True, exist_ok=True)
|
|
193
|
+
probe = cfg.data_dir / ".write-test"
|
|
194
|
+
probe.write_text("ok")
|
|
195
|
+
probe.unlink()
|
|
196
|
+
print(ok(f"Data folder: {cfg.data_dir}"))
|
|
197
|
+
except OSError as e:
|
|
198
|
+
good = False
|
|
199
|
+
print(bad(f"Data folder {cfg.data_dir} is not writable ({e.strerror})"))
|
|
200
|
+
|
|
201
|
+
notifier = Notifier(cfg.notify.urls)
|
|
202
|
+
if notifier.invalid:
|
|
203
|
+
good = False
|
|
204
|
+
for url in notifier.invalid:
|
|
205
|
+
print(bad(f"Notification link not understood: {masked(url)}"))
|
|
206
|
+
if notifier.enabled:
|
|
207
|
+
print(ok(f"Notifications: {len(notifier.apprise)} channel{'s' if len(notifier.apprise) != 1 else ''}"
|
|
208
|
+
" (send one with codomo-alerts test)"))
|
|
209
|
+
else:
|
|
210
|
+
print(warn("Notifications: none set, alerts only go to the log and the dashboard"))
|
|
211
|
+
|
|
212
|
+
client = Client(cfg.base_url)
|
|
213
|
+
started = time.monotonic()
|
|
214
|
+
try:
|
|
215
|
+
found = fetch(client, cfg.search, "available")
|
|
216
|
+
ms = int((time.monotonic() - started) * 1000)
|
|
217
|
+
print(ok(f"Codomo: {len(found)} home{'s' if len(found) != 1 else ''} match right now ({ms} ms)"))
|
|
218
|
+
except CodomoError as e:
|
|
219
|
+
good = False
|
|
220
|
+
print(bad(f"Codomo: {e}"))
|
|
221
|
+
|
|
222
|
+
if cfg.dashboard.enabled:
|
|
223
|
+
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
|
224
|
+
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
|
225
|
+
try:
|
|
226
|
+
s.bind((cfg.dashboard.host if cfg.dashboard.host != "localhost" else "127.0.0.1",
|
|
227
|
+
cfg.dashboard.port))
|
|
228
|
+
print(ok(f"Dashboard port {cfg.dashboard.port} is free"))
|
|
229
|
+
except OSError:
|
|
230
|
+
print(warn(f"Dashboard port {cfg.dashboard.port} is in use (fine if the watcher is running)"))
|
|
231
|
+
|
|
232
|
+
now = schedule.local()
|
|
233
|
+
print(ok(f"Amsterdam time {now:%a %H:%M}, {schedule.window(now)}"))
|
|
234
|
+
return 0 if good else 1
|
|
235
|
+
|
|
236
|
+
|
|
237
|
+
def cmd_stats(cfg: Config, args) -> int:
|
|
238
|
+
path = db_path(cfg)
|
|
239
|
+
if not path.exists():
|
|
240
|
+
print("No data yet. Start the watcher first: codomo-alerts watch")
|
|
241
|
+
return 1
|
|
242
|
+
store = Store(path)
|
|
243
|
+
first = store.q("SELECT MIN(at) FROM checks")[0][0]
|
|
244
|
+
checks, errors = store.q("SELECT COUNT(*), SUM(ok=0) FROM checks WHERE at>=?",
|
|
245
|
+
(iso(utcnow() - timedelta(days=1)),))[0]
|
|
246
|
+
active = store.q("SELECT COUNT(*) FROM listings WHERE active=1 AND status='available'")[0][0]
|
|
247
|
+
week = iso(utcnow() - timedelta(days=7))
|
|
248
|
+
new = store.q("SELECT COUNT(*) FROM events WHERE kind IN ('new','back','bookable') AND at>=?", (week,))[0][0]
|
|
249
|
+
gone = [int(r[0]) for r in store.q(
|
|
250
|
+
"SELECT detail FROM events WHERE kind='gone' AND status='available' AND detail IS NOT NULL")
|
|
251
|
+
if str(r[0]).isdigit()]
|
|
252
|
+
heat = Counter()
|
|
253
|
+
for r in store.q("SELECT at FROM events WHERE kind IN ('new','back','bookable') AND status='available'"):
|
|
254
|
+
t = schedule.local(parse(r[0]))
|
|
255
|
+
heat[(t.weekday(), t.hour)] += 1
|
|
256
|
+
|
|
257
|
+
print(dim(cfg.search.summary()))
|
|
258
|
+
if first:
|
|
259
|
+
print(f"Watching since {schedule.local(parse(first)):%d %b %H:%M}")
|
|
260
|
+
print(f"Last 24 hours {checks} checks, {errors or 0} failed")
|
|
261
|
+
print(f"Matching now {active}")
|
|
262
|
+
print(f"New this week {new}")
|
|
263
|
+
if gone:
|
|
264
|
+
print(f"Time listed typical {round(statistics.median(gone) / 60)} min, "
|
|
265
|
+
f"fastest {round(min(gone) / 60)} min ({len(gone)} homes)")
|
|
266
|
+
if heat:
|
|
267
|
+
top = ", ".join(f"{DAYS[d]} {h:02d}:00" for (d, h), _ in heat.most_common(3))
|
|
268
|
+
print(f"Busiest hours {top}")
|
|
269
|
+
rows = store.q("""SELECT e.kind, e.at, l.name, l.price FROM events e LEFT JOIN listings l ON l.id = e.listing_id
|
|
270
|
+
WHERE e.kind != 'building' ORDER BY e.id DESC LIMIT 10""")
|
|
271
|
+
if rows:
|
|
272
|
+
print()
|
|
273
|
+
print(table([[f"{schedule.local(parse(r['at'])):%a %H:%M}", r["kind"], r["name"] or "",
|
|
274
|
+
messages.money(r["price"])] for r in rows], right={3}))
|
|
275
|
+
store.close()
|
|
276
|
+
return 0
|
|
277
|
+
|
|
278
|
+
|
|
279
|
+
def cmd_demo(args) -> int:
|
|
280
|
+
from . import demo
|
|
281
|
+
from .dashboard import serve
|
|
282
|
+
|
|
283
|
+
if args.export:
|
|
284
|
+
path = demo.export(Path(args.export))
|
|
285
|
+
print(ok(f"Static demo written to {path}"))
|
|
286
|
+
return 0
|
|
287
|
+
setup_logging("WARNING")
|
|
288
|
+
store, cfg, folder = demo.temp_store()
|
|
289
|
+
cfg.dashboard.port = args.port
|
|
290
|
+
server = serve(store, cfg, demo=True)
|
|
291
|
+
if not server:
|
|
292
|
+
print(bad(f"Port {args.port} is in use. Try --port 8081"))
|
|
293
|
+
return 1
|
|
294
|
+
print(f"Demo dashboard with sample data: {bold(f'http://localhost:{server.server_address[1]}')}")
|
|
295
|
+
print(dim("Ctrl+C to stop"))
|
|
296
|
+
try:
|
|
297
|
+
threading.Event().wait()
|
|
298
|
+
except KeyboardInterrupt:
|
|
299
|
+
pass
|
|
300
|
+
finally:
|
|
301
|
+
server.shutdown()
|
|
302
|
+
store.close()
|
|
303
|
+
demo.cleanup(folder)
|
|
304
|
+
return 0
|
|
305
|
+
|
|
306
|
+
|
|
307
|
+
def cmd_upstream(cfg: Config, args) -> int:
|
|
308
|
+
from . import upstream
|
|
309
|
+
|
|
310
|
+
checks = upstream.run(Client(cfg.base_url, min_gap=2.0))
|
|
311
|
+
for c in checks:
|
|
312
|
+
line = f"{c.name}" + (dim(f" {c.detail}") if c.detail else "")
|
|
313
|
+
print({"ok": ok, "fail": bad, "notice": warn}[c.status](line))
|
|
314
|
+
if args.markdown:
|
|
315
|
+
Path(args.markdown).write_text(upstream.markdown(checks), encoding="utf-8")
|
|
316
|
+
return 1 if any(c.status == "fail" for c in checks) else 0
|
|
317
|
+
|
|
318
|
+
|
|
319
|
+
def cmd_mcp(cfg: Config, args) -> int:
|
|
320
|
+
from .mcp_server import run
|
|
321
|
+
run(cfg)
|
|
322
|
+
return 0
|
|
323
|
+
|
|
324
|
+
|
|
325
|
+
# Parser
|
|
326
|
+
|
|
327
|
+
def parser() -> argparse.ArgumentParser:
|
|
328
|
+
p = argparse.ArgumentParser(prog="codomo-alerts",
|
|
329
|
+
description="Alerts for new Codomo homes (formerly Holland2Stay).")
|
|
330
|
+
p.add_argument("--config", metavar="PATH", help="config file (default: ./config.toml or ~/.config/...)")
|
|
331
|
+
p.add_argument("-v", "--verbose", action="store_true", help="more logging")
|
|
332
|
+
p.add_argument("--version", action="version", version=f"codomo-alerts {__version__}")
|
|
333
|
+
# the config and verbose options also work after the command name
|
|
334
|
+
common = argparse.ArgumentParser(add_help=False)
|
|
335
|
+
common.add_argument("--config", metavar="PATH", default=argparse.SUPPRESS, help="config file")
|
|
336
|
+
common.add_argument("-v", "--verbose", action="store_true", default=argparse.SUPPRESS, help="more logging")
|
|
337
|
+
sub = p.add_subparsers(dest="command", metavar="command")
|
|
338
|
+
sub.add_parser("init", parents=[common], help="set up your search and notifications")
|
|
339
|
+
w = sub.add_parser("watch", parents=[common], help="check for new homes and send alerts")
|
|
340
|
+
w.add_argument("--once", action="store_true", help="check once and exit")
|
|
341
|
+
w.add_argument("--no-dashboard", action="store_true", help="don't start the dashboard")
|
|
342
|
+
w.add_argument("--port", type=int, help="dashboard port")
|
|
343
|
+
li = sub.add_parser("list", parents=[common], help="show what matches right now")
|
|
344
|
+
li.add_argument("--interest", action="store_true", help="register-interest homes instead")
|
|
345
|
+
li.add_argument("--city", action="append", help="only this city (repeat for more)")
|
|
346
|
+
li.add_argument("--country", action="append", help="only this country: nl, es, pt or at (repeat for more)")
|
|
347
|
+
li.add_argument("--json", action="store_true", help="machine readable output")
|
|
348
|
+
sub.add_parser("cities", parents=[common], help="city names you can use")
|
|
349
|
+
sub.add_parser("test", parents=[common], help="send a test notification")
|
|
350
|
+
sub.add_parser("doctor", parents=[common], help="check your setup")
|
|
351
|
+
sub.add_parser("stats", parents=[common], help="what the watcher has seen so far")
|
|
352
|
+
d = sub.add_parser("demo", help="try the dashboard with sample data")
|
|
353
|
+
d.add_argument("--port", type=int, default=8080)
|
|
354
|
+
d.add_argument("--export", metavar="DIR", help="write a static copy instead of serving it")
|
|
355
|
+
sub.add_parser("mcp", parents=[common], help="MCP server for AI assistants")
|
|
356
|
+
sub.add_parser("health", parents=[common], help="exit 0 when the watcher is running")
|
|
357
|
+
up = sub.add_parser("upstream", parents=[common], help="check that codomo.eu still works the same way")
|
|
358
|
+
up.add_argument("--markdown", metavar="FILE", help="also write a Markdown report")
|
|
359
|
+
return p
|
|
360
|
+
|
|
361
|
+
|
|
362
|
+
COMMANDS = {"watch": cmd_watch, "list": cmd_list, "cities": cmd_cities, "test": cmd_test,
|
|
363
|
+
"doctor": cmd_doctor, "stats": cmd_stats, "mcp": cmd_mcp, "health": cmd_health,
|
|
364
|
+
"upstream": cmd_upstream}
|
|
365
|
+
|
|
366
|
+
|
|
367
|
+
def main(argv: list[str] | None = None) -> int:
|
|
368
|
+
p = parser()
|
|
369
|
+
args = p.parse_args(argv)
|
|
370
|
+
if args.command == "init":
|
|
371
|
+
from .wizard import run
|
|
372
|
+
try:
|
|
373
|
+
return run(args.config)
|
|
374
|
+
except KeyboardInterrupt:
|
|
375
|
+
print()
|
|
376
|
+
return 130
|
|
377
|
+
if args.command == "demo":
|
|
378
|
+
return cmd_demo(args)
|
|
379
|
+
try:
|
|
380
|
+
cfg = load(args.config)
|
|
381
|
+
except ConfigError as e:
|
|
382
|
+
print(bad(f"Config problem: {e}"), file=sys.stderr)
|
|
383
|
+
return 2
|
|
384
|
+
command = args.command or "watch"
|
|
385
|
+
if command == "watch" and not args.command and not _configured(cfg):
|
|
386
|
+
p.print_help()
|
|
387
|
+
print(f"\nNew here? Run {bold('codomo-alerts init')} to set up your search.")
|
|
388
|
+
return 1
|
|
389
|
+
if command == "watch" and not hasattr(args, "once"):
|
|
390
|
+
args.once, args.no_dashboard, args.port = False, False, None
|
|
391
|
+
try:
|
|
392
|
+
return COMMANDS[command](cfg, args)
|
|
393
|
+
except KeyboardInterrupt:
|
|
394
|
+
return 130
|
|
395
|
+
except ConfigError as e:
|
|
396
|
+
print(bad(f"Config problem: {e}"), file=sys.stderr)
|
|
397
|
+
return 2
|
|
398
|
+
|
|
399
|
+
|
|
400
|
+
if __name__ == "__main__":
|
|
401
|
+
sys.exit(main())
|