Following and saving position¶
LogFollower ¶
Bases: _FollowerBase
Polls GET /v1/server with Since and hands back only entries you haven't seen.
Handles the API's cursor rules for you: entries are returned oldest first, entries you've
already handled (by Seq, per log) are skipped, and the request is timed for just after
the server's next heartbeat (slowly while it's offline).
Looping over it keeps going through network problems, rate limits and API errors, waiting a little longer after each failure in a row (up to a minute). It only stops for errors that waiting won't fix, like a wrong key, and raises those.
An entry that changes (a mod call resolved, a command result arriving, a shot gaining its
hit) comes again with a higher Seq and the same Id: replace what you have by Id.
Example::
for data, new in api.follow("Events", "KillLogs", sections=["Players"]):
for event in new["Events"]:
print(event["Type"], event)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
api
|
Any
|
The client. |
required |
logs
|
Sequence[str]
|
Log sections to follow ( |
('Events',)
|
sections
|
Sequence[str]
|
Other sections to fetch on every poll (e.g. |
()
|
limit
|
Optional[int]
|
Most entries per log per request (API default 100, max 500). |
None
|
cursor
|
Optional[int]
|
Resume from a |
None
|
save_to
|
Any
|
A file path, or an object with |
None
|
state ¶
Remembering where a follower got to, so a restarted bot carries on where it left off.
Pass save_to= to run_forever() or follow():
- a file path (
"cursor.json"), which oxrpy reads at start and rewrites after each poll, or - your own object with
load() -> dict | Noneandsave(state: dict)methods, to keep it in a database, Redis, or wherever your bot keeps things.
The position is saved after your handlers have run for a poll. If your bot crashes part-way
through, the entries from that poll come again on restart, so nothing is lost. A handler may
very occasionally see an entry twice; use the entry's Id if that matters.
StateStore ¶
Bases: Protocol
Anything with these two methods can hold a follower's position.
FileState ¶
Keeps the position in a small JSON file. Writes are atomic: a crash mid-write never leaves a half-written file.
read_state ¶
Check a loaded state; returns {"Cursor", "Handled"} or None if it's unusable.