Repository navigation
Expand file tree
/
Copy pathstate.py
More file actions
63 lines (51 loc) · 2.06 KB
/
Copy pathstate.py
File metadata and controls
63 lines (51 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
"""
AgentState: persisteert welke artikelen al verwerkt zijn en welke bets geplaatst zijn.
Slaat data op in een lokaal JSON-bestand (werkt gratis op elke hosting).
"""
import json
import logging
import os
from datetime import datetime, timedelta, timezone
from pathlib import Path
log = logging.getLogger(__name__)
STATE_FILE = os.getenv("STATE_FILE", "agent_state.json")
class AgentState:
def __init__(self):
self._path = Path(STATE_FILE)
self._data = self._load()
def _load(self) -> dict:
if self._path.exists():
try:
with open(self._path) as f:
return json.load(f)
except Exception as e:
log.warning(f"State laden mislukt, nieuw bestand: {e}")
return {"processed_articles": {}, "bets": []}
def _save(self):
with open(self._path, "w") as f:
json.dump(self._data, f, indent=2, default=str)
def already_processed(self, article_id: str) -> bool:
return article_id in self._data["processed_articles"]
def mark_processed(self, article_id: str):
self._data["processed_articles"][article_id] = datetime.now(timezone.utc).isoformat()
self._prune_old_articles()
self._save()
def record_bet(self, market_id: str, side: str, amount: float, price: float):
self._data["bets"].append({
"market_id": market_id,
"side": side,
"amount_usdc": amount,
"entry_price": price,
"placed_at": datetime.now(timezone.utc).isoformat(),
})
self._save()
log.info(f"Bet opgeslagen: {side} ${amount} @ {price:.2%} op {market_id}")
def _prune_old_articles(self):
"""Verwijder artikelen ouder dan 48 uur om bestand klein te houden."""
cutoff = (datetime.now(timezone.utc) - timedelta(hours=48)).isoformat()
self._data["processed_articles"] = {
k: v for k, v in self._data["processed_articles"].items()
if v >= cutoff
}
def get_bets(self) -> list:
return self._data.get("bets", [])