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.
@@ -0,0 +1,219 @@
1
+ """Turn a Search into list page requests and the home cards into Listings."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import logging
6
+ from collections.abc import Iterable
7
+ from dataclasses import dataclass
8
+ from datetime import date
9
+
10
+ from . import catalog
11
+ from .client import Changed, Client, CodomoError
12
+ from .codomo import Card, Counts, contract_key, parse_buildings
13
+ from .config import Search
14
+
15
+ log = logging.getLogger("codomo_alerts")
16
+
17
+ PAGE_SIZE = 25 # homes per list page
18
+ MAX_PAGES = 10
19
+
20
+
21
+ @dataclass
22
+ class Listing:
23
+ id: str # the last part of the home's address, e.g. g-h-betzweg-215
24
+ name: str
25
+ url: str
26
+ price: float | None # per month, service costs included
27
+ status: str # available (book directly) or interest (register interest)
28
+ city: str | None = None
29
+ bedrooms: int | None = None
30
+ contract: str | None = None
31
+ furnishing: str | None = None
32
+ building: str | None = None
33
+ persons: int | None = None
34
+ available_from: str | None = None
35
+ interest: int | None = None # people who registered interest so far
36
+ gender: str | None = None # female or male when Codomo keeps the home for one gender
37
+
38
+ @property
39
+ def city_name(self) -> str:
40
+ return catalog.city_label(self.city)
41
+
42
+ @property
43
+ def bedrooms_name(self) -> str:
44
+ return catalog.bedrooms_label(self.bedrooms)
45
+
46
+ @property
47
+ def contract_name(self) -> str:
48
+ return catalog.contract_label(self.contract)
49
+
50
+ @property
51
+ def furnishing_name(self) -> str:
52
+ return catalog.FURNISHING.get(self.furnishing or "", "")
53
+
54
+ @property
55
+ def gender_name(self) -> str:
56
+ return catalog.GENDERS.get(self.gender or "", "")
57
+
58
+
59
+ def to_listing(card: Card, base_url: str) -> Listing:
60
+ return Listing(
61
+ id=card.key, name=card.name or card.key, url=f"{base_url}{card.path}", price=card.total,
62
+ status="available" if card.direct else "interest", city=catalog.city_label(card.city) or None,
63
+ bedrooms=card.bedrooms, furnishing=catalog.furnishing_of(card.finishing),
64
+ building=card.building or None, persons=int(card.persons) if card.persons.isdigit() else None,
65
+ available_from=card.available_from or None, interest=card.interest, gender=catalog.gender_of(card.tags))
66
+
67
+
68
+ def matches(search: Search, card: Card, markets: list[str] | None = None) -> bool | None:
69
+ """Every filter, checked on our side. None: the home fails a filter the site should have
70
+ applied (bedrooms), so the answer looks wrong. A home without a bedroom count (rooms in
71
+ Portugal and Austria) counts as a studio."""
72
+ if search.bedrooms and card.bedrooms is not None and card.bedrooms not in search.bedrooms:
73
+ return None
74
+ if search.bedrooms and card.bedrooms is None and 0 not in search.bedrooms:
75
+ return False
76
+ if search.cities and not any(catalog.same_city(card.city, c) for c in search.cities):
77
+ return False
78
+ if card.country not in (markets or search.markets()):
79
+ return False
80
+ other = catalog.gender_of(card.tags)
81
+ if search.gender and other and other != search.gender:
82
+ return False
83
+ if any(catalog.same_city(card.city, c) for c in search.exclude_cities):
84
+ return False
85
+ if search.furnishing and catalog.furnishing_of(card.finishing) not in search.furnishing:
86
+ return False
87
+ total = card.total
88
+ if total is not None and (search.price_min and total < search.price_min
89
+ or search.price_max and total > search.price_max):
90
+ return False
91
+ if search.persons and card.persons.isdigit() and int(card.persons) < search.persons:
92
+ return False
93
+ late = bool(search.move_in_before and card.available_from
94
+ and date.fromisoformat(card.available_from) > search.move_in_before)
95
+ return not late
96
+
97
+
98
+ def _all(client: Client, country: str, params: dict[str, str]) -> tuple[list[Card], Counts]:
99
+ """Every home of one country for these filters, over all pages. Raises Changed when the
100
+ homes read do not fit the page's own numbers.
101
+
102
+ The site counts a few homes it never shows (2026-11-16: 155 counted, 144 on the pages), so
103
+ fewer homes than counted is normal. More than counted, far fewer, or the wrong booking type
104
+ means the page was misread. A page past the end shows page 1 again, so a page with nothing
105
+ new ends the list."""
106
+ mode = params.get("publish_mode")
107
+ found: dict[str, Card] = {}
108
+ others = 0
109
+ counts = Counts({None: 0}, {})
110
+ for page in range(1, MAX_PAGES + 1):
111
+ p = dict(params)
112
+ if page > 1:
113
+ p["page"] = str(page)
114
+ cards, counts = client.homes(country, p)
115
+ new = 0
116
+ for c in cards:
117
+ if c.country != country:
118
+ others += 1
119
+ elif c.path not in found:
120
+ found[c.path] = c
121
+ new += 1
122
+ if others and not found:
123
+ return [], counts # "no homes in this country yet, here are homes elsewhere"
124
+ if len(cards) < PAGE_SIZE or new == 0:
125
+ break
126
+ else:
127
+ raise Changed(f"More than {MAX_PAGES} pages of homes: a filter is probably ignored")
128
+ homes = list(found.values())
129
+ expected = counts.booking.get(mode)
130
+ if expected is None:
131
+ raise Changed("Codomo's homes page has no total")
132
+ if len(homes) > expected or expected >= 4 and len(homes) < expected / 2:
133
+ raise Changed(f"Codomo's homes page says {expected} homes, {len(homes)} were read")
134
+ direct = sum(c.direct for c in homes)
135
+ if mode == "direct_booking" and direct != len(homes) or mode == "interest" and direct:
136
+ raise Changed(f"Codomo sent homes of the wrong kind (asked for {mode})")
137
+ if mode is None and (direct > counts.booking.get("direct_booking", direct)
138
+ or len(homes) - direct > counts.booking.get("interest", len(homes))):
139
+ raise Changed("Codomo's booking counts do not match its homes")
140
+ return homes, counts
141
+
142
+
143
+ def cards(client: Client, country: str, params: dict[str, str], bedrooms: list[int]) -> list[Card]:
144
+ """One request per bedroom count that has homes (the site filters one count at a time).
145
+ Where the site counts no bedrooms at all (Portugal and Austria list rooms without a count),
146
+ a studio search reads the homes without a count too."""
147
+ if not bedrooms:
148
+ return _all(client, country, params)[0]
149
+
150
+ def one(n: int) -> tuple[list[Card], Counts]:
151
+ homes, counts = _all(client, country, {**params, "bedrooms": str(n)})
152
+ wrong = [c for c in homes if c.bedrooms is not None and c.bedrooms != n]
153
+ if wrong:
154
+ raise Changed(f"Codomo ignored the bedroom filter (asked for {n}, got {wrong[0].bedrooms_label})")
155
+ return homes, counts
156
+
157
+ homes, counts = one(bedrooms[0])
158
+ for n, count in sorted(counts.bedrooms.items()):
159
+ if n in bedrooms[1:] and count > 0:
160
+ homes += one(n)[0]
161
+ if 0 in bedrooms and not any(counts.bedrooms.values()):
162
+ homes += [c for c in _all(client, country, params)[0] if c.bedrooms is None]
163
+ return homes
164
+
165
+
166
+ def fetch(client: Client, search: Search, status: str, seen: Iterable[str] = ()) -> dict[str, Listing]:
167
+ """Every home that matches the search right now, by its key. `seen`: country codes found
168
+ on the site lately, read too when the search covers every country."""
169
+ params = {"publish_mode": catalog.STATUS[status]}
170
+ if search.price_max:
171
+ # the site filters on the rent without service costs, so this keeps every home whose
172
+ # total fits; the total is checked in matches()
173
+ params["max_rent"] = str(search.price_max)
174
+ if len(search.cities) == 1:
175
+ params["city"] = catalog.city_slug(search.cities[0])
176
+ out: dict[str, Listing] = {}
177
+ odd = 0
178
+ markets = search.markets(seen)
179
+ for country in markets:
180
+ for card in cards(client, country, params, search.bedrooms):
181
+ ok = matches(search, card, markets)
182
+ if ok is None:
183
+ odd += 1
184
+ elif ok:
185
+ out[card.key] = to_listing(card, client.base_url)
186
+ if odd and not out:
187
+ raise Changed(f"All {odd} homes Codomo sent fail filters it should have applied")
188
+ if odd:
189
+ log.warning("Codomo sent %d homes that do not match the filters it applied; ignored them", odd)
190
+ return out
191
+
192
+
193
+ def contract(client: Client, listing: Listing) -> str | None:
194
+ """The longest stay a home allows, read from its own page. None when the page can't be
195
+ read; a block, rate limit or maintenance is raised so the watcher backs off."""
196
+ try:
197
+ page = client.unit(listing.url.removeprefix(client.base_url))
198
+ except CodomoError as e:
199
+ if e.kind in ("blocked", "rate_limited", "maintenance"):
200
+ raise
201
+ log.info("Couldn't read the page of %s: %s", listing.name, e)
202
+ return None
203
+ return contract_key(page)
204
+
205
+
206
+ def discover_cities(client: Client) -> dict[str, str]:
207
+ """Cities codomo.eu lists right now: name -> country code."""
208
+ return {c.name: c.country.lower() for c in client.cities()}
209
+
210
+
211
+ def buildings(client: Client) -> list[tuple[str, str, str]]:
212
+ """(country, city slug, building slug) of every building page in the sitemap."""
213
+ return parse_buildings(client.sitemap())
214
+
215
+
216
+ def building_name(slug: str) -> str:
217
+ """'the-betz' -> 'The Betz', 'vb-student-living' -> 'VB Student Living'."""
218
+ return " ".join(w.upper() if len(w) <= 2 and w not in ("de", "op", "of", "in") else w.capitalize()
219
+ for w in slug.split("-"))
codomo_alerts/store.py ADDED
@@ -0,0 +1,256 @@
1
+ """SQLite storage: listings, events, checks and a few settings."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import json
6
+ import sqlite3
7
+ import threading
8
+ from contextlib import contextmanager
9
+ from datetime import UTC, datetime, timedelta
10
+ from pathlib import Path
11
+
12
+ from .search import Listing
13
+
14
+ SCHEMA = """
15
+ CREATE TABLE IF NOT EXISTS listings (
16
+ id TEXT PRIMARY KEY,
17
+ name TEXT NOT NULL,
18
+ url TEXT NOT NULL,
19
+ price REAL,
20
+ status TEXT NOT NULL,
21
+ city TEXT,
22
+ bedrooms INTEGER,
23
+ contract TEXT,
24
+ furnishing TEXT,
25
+ building TEXT,
26
+ persons INTEGER,
27
+ available_from TEXT,
28
+ interest INTEGER,
29
+ gender TEXT,
30
+ active INTEGER NOT NULL DEFAULT 0,
31
+ misses INTEGER NOT NULL DEFAULT 0,
32
+ first_seen TEXT NOT NULL,
33
+ listed_at TEXT,
34
+ last_seen TEXT,
35
+ gone_at TEXT
36
+ );
37
+ CREATE INDEX IF NOT EXISTS listings_active ON listings(active, status);
38
+ CREATE TABLE IF NOT EXISTS events (
39
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
40
+ at TEXT NOT NULL,
41
+ kind TEXT NOT NULL,
42
+ listing_id TEXT,
43
+ status TEXT,
44
+ price REAL,
45
+ detail TEXT,
46
+ notified INTEGER NOT NULL DEFAULT 0,
47
+ attempts INTEGER NOT NULL DEFAULT 0
48
+ );
49
+ CREATE INDEX IF NOT EXISTS events_at ON events(at);
50
+ CREATE TABLE IF NOT EXISTS checks (
51
+ at TEXT NOT NULL,
52
+ ok INTEGER NOT NULL,
53
+ ms INTEGER,
54
+ found INTEGER,
55
+ requests INTEGER,
56
+ error TEXT
57
+ );
58
+ CREATE INDEX IF NOT EXISTS checks_at ON checks(at);
59
+ CREATE TABLE IF NOT EXISTS buildings (
60
+ slug TEXT PRIMARY KEY,
61
+ name TEXT NOT NULL,
62
+ city TEXT,
63
+ url TEXT,
64
+ first_seen TEXT NOT NULL
65
+ );
66
+ CREATE TABLE IF NOT EXISTS pages (
67
+ id TEXT PRIMARY KEY,
68
+ contract TEXT,
69
+ read_at TEXT NOT NULL
70
+ );
71
+ CREATE TABLE IF NOT EXISTS meta (key TEXT PRIMARY KEY, value TEXT);
72
+ """
73
+ VERSION = 3
74
+ ALERT_KINDS = ("new", "back", "bookable", "price", "interest")
75
+ DETAILS = ("city", "bedrooms", "contract", "furnishing", "building", "persons", "available_from", "interest",
76
+ "gender")
77
+
78
+
79
+ def utcnow() -> datetime:
80
+ return datetime.now(UTC)
81
+
82
+
83
+ def iso(dt: datetime) -> str:
84
+ return dt.astimezone(UTC).isoformat(timespec="seconds")
85
+
86
+
87
+ def parse(text: str | None) -> datetime | None:
88
+ return datetime.fromisoformat(text) if text else None
89
+
90
+
91
+ def to_listing(row) -> Listing:
92
+ return Listing(id=row["id"], name=row["name"], url=row["url"], price=row["price"], status=row["status"],
93
+ **{k: row[k] for k in DETAILS})
94
+
95
+
96
+ class Store:
97
+ def __init__(self, path: Path | str):
98
+ self.path = Path(path)
99
+ if str(path) != ":memory:":
100
+ self.path.parent.mkdir(parents=True, exist_ok=True)
101
+ self.db = sqlite3.connect(str(path), check_same_thread=False, isolation_level=None)
102
+ self.db.row_factory = sqlite3.Row
103
+ self.lock = threading.RLock()
104
+ with self.lock:
105
+ self.db.execute("PRAGMA journal_mode=WAL")
106
+ self.db.execute("PRAGMA busy_timeout=5000")
107
+ self.db.executescript(SCHEMA)
108
+ columns = {r["name"] for r in self.db.execute("PRAGMA table_info(listings)")}
109
+ if "gender" not in columns: # added in version 3
110
+ self.db.execute("ALTER TABLE listings ADD COLUMN gender TEXT")
111
+ self.db.execute(f"PRAGMA user_version={VERSION}")
112
+
113
+ def close(self) -> None:
114
+ with self.lock:
115
+ self.db.close()
116
+
117
+ def q(self, sql: str, args=()) -> list[sqlite3.Row]:
118
+ with self.lock:
119
+ return self.db.execute(sql, args).fetchall()
120
+
121
+ def x(self, sql: str, args=()) -> int:
122
+ with self.lock:
123
+ return self.db.execute(sql, args).lastrowid
124
+
125
+ @contextmanager
126
+ def transaction(self):
127
+ """All or nothing: a crash halfway never leaves a home without its alert."""
128
+ with self.lock:
129
+ self.db.execute("BEGIN")
130
+ try:
131
+ yield
132
+ except BaseException:
133
+ self.db.execute("ROLLBACK")
134
+ raise
135
+ self.db.execute("COMMIT")
136
+
137
+ # Settings
138
+
139
+ def get(self, key: str, default=None):
140
+ rows = self.q("SELECT value FROM meta WHERE key=?", (key,))
141
+ return json.loads(rows[0]["value"]) if rows else default
142
+
143
+ def set(self, key: str, value) -> None:
144
+ self.x("INSERT INTO meta(key, value) VALUES(?, ?) ON CONFLICT(key) DO UPDATE SET value=excluded.value",
145
+ (key, json.dumps(value)))
146
+
147
+ # Listings
148
+
149
+ def listing(self, key: str) -> sqlite3.Row | None:
150
+ rows = self.q("SELECT * FROM listings WHERE id=?", (key,))
151
+ return rows[0] if rows else None
152
+
153
+ def active(self, status: str) -> dict[str, sqlite3.Row]:
154
+ return {r["id"]: r for r in self.q("SELECT * FROM listings WHERE active=1 AND status=?", (status,))}
155
+
156
+ def activate(self, x: Listing, now: datetime, new_period: bool) -> None:
157
+ details = [getattr(x, k) for k in DETAILS]
158
+ with self.lock:
159
+ row = self.listing(x.id)
160
+ if row is None:
161
+ self.x(f"""INSERT INTO listings(id, name, url, price, status, {", ".join(DETAILS)},
162
+ active, misses, first_seen, listed_at, last_seen)
163
+ VALUES(?,?,?,?,?,{",".join("?" * len(DETAILS))},1,0,?,?,?)""",
164
+ (x.id, x.name, x.url, x.price, x.status, *details, iso(now),
165
+ iso(now) if new_period else None, iso(now)))
166
+ return
167
+ listed_at = iso(now) if new_period else row["listed_at"]
168
+ keep = ", ".join(f"{k}=COALESCE(?, {k})" for k in DETAILS)
169
+ self.x(f"""UPDATE listings SET name=?, url=?, price=?, status=?, active=1, misses=0,
170
+ listed_at=?, last_seen=?, gone_at=NULL, {keep} WHERE id=?""",
171
+ (x.name, x.url, x.price, x.status, listed_at, iso(now), *details, x.id))
172
+
173
+ def missed(self, key: str, misses: int) -> None:
174
+ self.x("UPDATE listings SET misses=? WHERE id=?", (misses, key))
175
+
176
+ def gone(self, key: str, now: datetime) -> None:
177
+ self.x("UPDATE listings SET active=0, misses=0, gone_at=? WHERE id=?", (iso(now), key))
178
+
179
+ def retire(self, key: str) -> None:
180
+ """No longer matches the search (filters changed). Not the same as taken."""
181
+ self.x("UPDATE listings SET active=0, misses=0, listed_at=NULL WHERE id=?", (key,))
182
+
183
+ def details(self, x: Listing) -> None:
184
+ keep = ", ".join(f"{k}=COALESCE(?, {k})" for k in DETAILS)
185
+ self.x(f"UPDATE listings SET {keep} WHERE id=?", (*[getattr(x, k) for k in DETAILS], x.id))
186
+
187
+ def known_details(self, x: Listing) -> None:
188
+ row = self.listing(x.id)
189
+ if row:
190
+ for attr in DETAILS:
191
+ if getattr(x, attr) is None and row[attr] is not None:
192
+ setattr(x, attr, row[attr])
193
+ if x.contract is None:
194
+ x.contract = self.contract(x.id)
195
+
196
+ # Contracts read from a home's own page, kept for homes that don't match too
197
+
198
+ def contract(self, key: str) -> str | None:
199
+ rows = self.q("SELECT contract FROM pages WHERE id=?", (key,))
200
+ return rows[0]["contract"] if rows else None
201
+
202
+ def save_contract(self, key: str, contract: str) -> None:
203
+ self.x("""INSERT INTO pages(id, contract, read_at) VALUES(?,?,?)
204
+ ON CONFLICT(id) DO UPDATE SET contract=excluded.contract, read_at=excluded.read_at""",
205
+ (key, contract, iso(utcnow())))
206
+
207
+ # Events
208
+
209
+ def event(self, kind: str, listing_id: str | None, status: str | None = None, price: float | None = None,
210
+ detail: str | None = None, at: datetime | None = None, notified: bool | None = None) -> int:
211
+ if notified is None:
212
+ notified = kind not in ALERT_KINDS
213
+ return self.x("INSERT INTO events(at, kind, listing_id, status, price, detail, notified) VALUES(?,?,?,?,?,?,?)",
214
+ (iso(at or utcnow()), kind, listing_id, status, price, detail, int(notified)))
215
+
216
+ def pending(self, since: datetime) -> list[sqlite3.Row]:
217
+ """Unsent alerts, oldest first. Building notices have no listing, so a LEFT JOIN."""
218
+ return self.q("""SELECT e.id AS event_id, e.kind, e.at, e.status AS event_status, e.price AS event_price,
219
+ e.detail, e.attempts, l.* FROM events e LEFT JOIN listings l ON l.id = e.listing_id
220
+ WHERE e.notified=0 AND e.at >= ? ORDER BY e.id""", (iso(since),))
221
+
222
+ def expire(self, before: datetime) -> int:
223
+ """Give up on alerts that could not be sent for too long."""
224
+ with self.lock:
225
+ return self.db.execute("UPDATE events SET notified=1 WHERE notified=0 AND at < ?",
226
+ (iso(before),)).rowcount
227
+
228
+ def mark_sent(self, event_ids: list[int]) -> None:
229
+ with self.lock:
230
+ self.db.executemany("UPDATE events SET notified=1 WHERE id=?", [(i,) for i in event_ids])
231
+
232
+ def mark_failed(self, event_ids: list[int]) -> None:
233
+ with self.lock:
234
+ self.db.executemany("UPDATE events SET attempts=attempts+1 WHERE id=?", [(i,) for i in event_ids])
235
+
236
+ # Checks
237
+
238
+ def check(self, ok: bool, ms: int, found: int | None, requests: int, error: str | None = None) -> None:
239
+ self.x("INSERT INTO checks(at, ok, ms, found, requests, error) VALUES(?,?,?,?,?,?)",
240
+ (iso(utcnow()), int(ok), ms, found, requests, error))
241
+
242
+ def prune(self, days: int = 30) -> None:
243
+ cutoff = iso(utcnow() - timedelta(days=days))
244
+ self.x("DELETE FROM checks WHERE at < ?", (cutoff,))
245
+ self.x("DELETE FROM pages WHERE read_at < ?", (cutoff,))
246
+
247
+ # Buildings
248
+
249
+ def building(self, slug: str, name: str, city: str | None, url: str | None, now: datetime) -> bool:
250
+ """Record a building page; True when it was not known before."""
251
+ with self.lock:
252
+ if self.q("SELECT 1 FROM buildings WHERE slug=?", (slug,)):
253
+ return False
254
+ self.x("INSERT INTO buildings(slug, name, city, url, first_seen) VALUES(?,?,?,?,?)",
255
+ (slug, name, city, url, iso(now)))
256
+ return True
codomo_alerts/term.py ADDED
@@ -0,0 +1,62 @@
1
+ """Tiny helpers for terminal output."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import os
6
+ import sys
7
+
8
+
9
+ def _color() -> bool:
10
+ return sys.stdout.isatty() and not os.environ.get("NO_COLOR")
11
+
12
+
13
+ def _wrap(code: str, text: str) -> str:
14
+ return f"\033[{code}m{text}\033[0m" if _color() else text
15
+
16
+
17
+ def bold(text: str) -> str:
18
+ return _wrap("1", text)
19
+
20
+
21
+ def dim(text: str) -> str:
22
+ return _wrap("2", text)
23
+
24
+
25
+ def ok(text: str) -> str:
26
+ return f"{_wrap('32', '✓')} {text}"
27
+
28
+
29
+ def bad(text: str) -> str:
30
+ return f"{_wrap('31', '✗')} {text}"
31
+
32
+
33
+ def warn(text: str) -> str:
34
+ return f"{_wrap('33', '!')} {text}"
35
+
36
+
37
+ def ask(question: str, default: str = "") -> str:
38
+ hint = f" {dim('[' + default + ']')}" if default else ""
39
+ try:
40
+ answer = input(f"{question}{hint} ").strip()
41
+ except EOFError:
42
+ answer = ""
43
+ return answer or default
44
+
45
+
46
+ def yes(question: str, default: bool = True) -> bool:
47
+ answer = ask(question, "Y/n" if default else "y/N").lower()
48
+ if answer == "y/n": # pressed enter
49
+ return default
50
+ return answer.startswith("y")
51
+
52
+
53
+ def table(rows: list[list[str]], right: set[int] | None = None) -> str:
54
+ if not rows:
55
+ return ""
56
+ right = right or set()
57
+ widths = [max(len(r[i]) for r in rows) for i in range(len(rows[0]))]
58
+ lines = []
59
+ for r in rows:
60
+ cells = [c.rjust(w) if i in right else c.ljust(w) for i, (c, w) in enumerate(zip(r, widths, strict=True))]
61
+ lines.append(" ".join(cells).rstrip())
62
+ return "\n".join(lines)
@@ -0,0 +1,98 @@
1
+ """Decide what changed between two checks."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from dataclasses import dataclass, field
6
+ from datetime import datetime, timedelta
7
+
8
+ from .search import Listing
9
+ from .store import Store, parse, to_listing
10
+
11
+ MISSES_TO_GONE = 2 # one odd answer must never mark a home as taken
12
+ MISSES_IF_ALL_GONE = 3 # an empty answer after many homes is more likely a glitch
13
+ FLICKER = timedelta(minutes=5)
14
+ PRICE_STEP = 1.0 # ignore rounding noise
15
+
16
+
17
+ @dataclass
18
+ class Change:
19
+ kind: str # new, back, bookable, interest, price, gone
20
+ listing: Listing
21
+ detail: str | None = None
22
+
23
+
24
+ @dataclass
25
+ class Result:
26
+ status: str
27
+ changes: list[Change] = field(default_factory=list)
28
+ baseline: bool = False
29
+ filters_changed: bool = False
30
+ matching: int = 0
31
+
32
+
33
+ class Tracker:
34
+ def __init__(self, store: Store, fingerprint: str):
35
+ self.store = store
36
+ self.fingerprint = fingerprint
37
+
38
+ def apply(self, status: str, current: dict[str, Listing], now: datetime) -> Result:
39
+ store = self.store
40
+ result = Result(status=status, matching=len(current))
41
+ key = f"baseline:{status}"
42
+ known = store.get(key)
43
+ active = store.active(status)
44
+
45
+ if known != self.fingerprint:
46
+ # First run or new filters: take what matches now as the starting point, quietly.
47
+ result.baseline = True
48
+ result.filters_changed = known is not None
49
+ for pid in active:
50
+ if pid not in current:
51
+ store.retire(pid)
52
+ for x in current.values():
53
+ # start unknown, so it does not count towards time on market
54
+ row = store.listing(x.id)
55
+ if row is not None and not row["active"]:
56
+ store.retire(x.id)
57
+ store.activate(x, now, new_period=False)
58
+ store.set(key, self.fingerprint)
59
+ return result
60
+
61
+ for pid, x in current.items():
62
+ row = active.get(pid) or store.listing(pid)
63
+ if pid in active:
64
+ old = row["price"]
65
+ if old is not None and x.price is not None and x.price < old - PRICE_STEP:
66
+ result.changes.append(Change("price", x, f"{old:.0f}"))
67
+ store.activate(x, now, new_period=False)
68
+ continue
69
+
70
+ if row is None:
71
+ kind = "interest" if status == "interest" else "new"
72
+ elif row["active"] and row["status"] != status:
73
+ kind = "bookable" if status == "available" else "interest"
74
+ elif row["gone_at"] and now - parse(row["gone_at"]) < FLICKER:
75
+ kind = None
76
+ elif row["gone_at"]:
77
+ kind = "interest" if status == "interest" else "back"
78
+ else:
79
+ kind = "interest" if status == "interest" else "new"
80
+ store.activate(x, now, new_period=kind is not None)
81
+ if kind:
82
+ result.changes.append(Change(kind, x))
83
+
84
+ need = MISSES_IF_ALL_GONE if not current and len(active) >= 5 else MISSES_TO_GONE
85
+ for pid, row in active.items():
86
+ if pid in current:
87
+ continue
88
+ misses = row["misses"] + 1
89
+ if misses < need:
90
+ store.missed(pid, misses)
91
+ continue
92
+ store.gone(pid, now)
93
+ listed = parse(row["listed_at"])
94
+ # last_seen is the last time it was there, so that is when it went
95
+ seen = parse(row["last_seen"]) or now
96
+ duration = str(int((seen - listed).total_seconds())) if listed else None
97
+ result.changes.append(Change("gone", to_listing(row), duration))
98
+ return result