Tables, versions and watches
Some data changes once a year and matters the day it does: a premium file, a tariff, an insurer's conditions. Cormorant's tools for that work together:
- a watch checks a file, a page, a source or a whole site on a schedule and tells you when it changed;
- versions keep every version of a file Cormorant retrieved, with its exact bytes;
- a diff says what changed between two versions, as passages of text or as rows of a table;
- tables read a CSV, a workbook or a PDF into rows you page through;
- a discovery finds the documents of a site, so you can watch what you didn't know was there.
The example follows the approved health-insurance premiums
(bag.okp.premiums, see Swiss open data sources).
1. Watch the file#
curl -s -X POST -H "Authorization: Bearer $KEY" -H "Content-Type: application/json" \
-d '{"target": {"source_id": "bag.okp.premiums"},
"schedule": {"interval": "P7D", "seasons": [{"from": "09-15", "to": "12-31", "interval": "P1D"}]},
"webhook_url": "https://example.org/hooks/cormorant", "reference": "premiums"}' \
"https://api.cormorant.ch/v1/watches"
It is checked weekly, and daily from 15 September to 31 December, when the
new premiums come out. The answer holds the webhook's secret, shown only
this once. Other targets: any address ({"url": …}), a dataset of
opendata.swiss, a site's documents ({"site": …}) or the changes of a
geodata source within a box.
2. Get the event#
A change raises one event, posted to your webhook and kept on a feed:
curl -s -H "Authorization: Bearer $KEY" \
"https://api.cormorant.ch/v1/watches/events?after=$CURSOR&limit=100"
Events are new (the first capture), changed, moved, gone,
blocked, error, resource_added (a dataset's new file, a source's new
year, a site's new document) and resource_removed. A changed event
carries the old and new version, each with its sha256. Check the
webhook's X-Cormorant-Signature with the watch's secret before acting;
the API contract shows how. A failed delivery is
retried for 24 hours, always with the same event_id.
For one web page, POST /v1/pages/watches is the lighter form: it posts
when the page's text changes.
3. See what changed#
curl -s -H "Authorization: Bearer $KEY" \
"https://api.cormorant.ch/v1/diff?from=$OLD&to=$NEW&key=Versicherer,Kanton"
For a table, rows are matched by the key columns and the answer lists the
rows added, removed and changed, with only the changed cells. Cells are
compared as the file holds them: 313.0 and 313.00 differ. For a PDF,
an HTML page or text, the answer lists the passages that changed, with the
PDF page each starts on.
Every version is kept for 24 months after it was last found at its
address, and as long as you pin it (POST /v1/bodies/{sha256}/pin).
GET /v1/versions?source_id=bag.okp.premiums lists them.
4. Read the rows#
curl -s -X POST -H "Authorization: Bearer $KEY" -H "Content-Type: application/json" \
-d '{"sha256": "'"$NEW"'"}' "https://api.cormorant.ch/v1/tables/extract"
curl -s -H "Authorization: Bearer $KEY" \
"https://api.cormorant.ch/v1/tables/$TABLE_ID/rows?offset=0&limit=10000"
Rows come as NDJSON, one object a line, in the file's order; follow
X-Next-Offset for the next page. Nothing is rounded, retyped or dropped:
a total row comes back flagged ragged, a workbook cell has both its
stored value and its displayed text. The same body and options always
give the same table_id and rows.
Find the documents of a site#
When the files you care about are linked from a site rather than at a known address, let a discovery find them:
curl -s -X POST -H "Authorization: Bearer $KEY" -H "Content-Type: application/json" \
-d '{"site": "https://www.example-insurer.ch/de/downloads", "file_types": ["pdf"], "max_pages": 200}' \
"https://api.cormorant.ch/v1/discover"
curl -s -H "Authorization: Bearer $KEY" "https://api.cormorant.ch/v1/discover/$ID/documents"
It crawls the one host politely (one request every two seconds, robots.txt
on every route), downloads every linked document of those types and says,
per document, where it is linked and whether it is new or changed since
your last discovery. A watch with {"site": …} runs the same discovery on
a schedule and raises resource_added, changed and resource_removed.
Costs#
Each request a watch or discovery makes is one fetch of your project
(reason watch in the request log). Versions, diffs and table extraction
are not charged at present. Every field and error is in the
API contract.