Write a recipe
A recipe tells the crawler what to fetch and how to read it. The package has two recipes: readVenue reads the opening hours of a venue, and readProduct reads a product page. The recipe reader starts their readers on a page that you paste. For a source of your own, write a recipe with defineRecipe.
A recipe in three parts
Section titled “A recipe in three parts”- The name goes into each capture record, so it is part of each capture id.
reindexfinds the extractor by it. Do not change it after the first capture. - The request (
fetch) changes an input into a URL. With nofetch, the input is the URL. - The extractor (
extract) reads one captured body into documents. It is pure: the same body always gives the same documents, and it sends no request.
This recipe reads the menu of a made-up bakery from its JSON API:
/** * The recipe of the guide "Write a recipe": the menu of a made-up bakery, from its JSON API. The * guide shows this file, and its figure starts this recipe in the browser. */import { defineRecipe, readJson } from "polite-crawler";
/** The answer of the menu API. */interface Menu { readonly day: string; readonly items: ReadonlyArray<{ readonly id: string; readonly name: string; readonly price: string; readonly notes?: string; }>;}
export const menu = defineRecipe({ // the name goes into each capture record: do not change it after the first capture name: "forno-menu", description: "The menu of Forno Esempio for one day", // an API with documented terms: robots.txt does not apply, and the pace does mode: "api", minInterval: "2s", maxAge: "12h", fetch: (day: string) => `https://api.forno-esempio.example/menu?day=${encodeURIComponent(day)}`, // pure: the same body always gives the same documents, and it sends no request extract: (body, capture) => { const menu = readJson(body, capture) as Menu; return menu.items.map((item) => ({ doc: `menu:forno-esempio/${menu.day}/${item.id}`, kind: "menu-item", title: item.name, text: `${item.name}: ${item.price}${item.notes === undefined ? "" : `. ${item.notes}`}`, url: capture.url, meta: { day: menu.day, price: item.price }, })); },});Start it in your browser
Section titled “Start it in your browser”The figure starts this recipe, compiled into this page. Select an input and an answer of the API, or change the body. The crawler does not fetch here: the figure gives the body of the capture.
- The input
- The request
- The capture
- The extractor
- The documents
- find, read, verify
1 and 2
The input, and menu.request(input)
{
"url": "https://api.forno-esempio.example/menu?day=tue",
"mode": "api",
"recipe": "forno-menu",
"accept": "application/json",
"minIntervalMs": 2000,
"maxAgeMs": 43200000
}The crawler fetches this request. The mode api skips robots.txt, and the pace of minInterval applies. A stored copy younger than maxAge answers with no request.
3
The capture: the body that the crawler keeps
4 and 5
menu.extractor(body, capture)
The extractor reads the body…
6
The index: find, read and verify
The documents go into the index as versions. find searches them, read gives a range of a text, and verify finds the exact span of a quote, with the capture to cite.
No document to verify.
From a capture to a citation
Section titled “From a capture to a citation”- The request.
menu.request("tue")gives the request of the crawler. It names the recipe, the mode and the pace.runRecipe(crawler, menu, "tue")fetches it. - The capture. The crawler fetches the URL with each polite rule, and it writes a capture record and the body. The capture id is a hash of the record, so a citation can name it.
- The extractor. The runtime gives the body to the extractor. The extractor checks each document: an id, a kind, a title, a text, a URL and metadata that is JSON. A document that is not correct makes it throw, and the runtime reports the error.
- The documents. One capture can give many documents. Each document is a version in the index, with the capture that it came from.
- The reading.
find,read,verify,extract,diffand the time travel use the documents.reindexreads each capture of the recipe again, with no request.
The rules
Section titled “The rules”- The mode.
page(the default) is a web page: robots.txt applies.apiis a documented API with terms of its own: robots.txt does not apply, and the pace ofminIntervaldoes. - A body that is not JSON.
readJson(body, capture)reads JSON in the encoding of the response. A login page or a maintenance page throws an error that names the URL and the content type. - A stored copy that the extractor cannot read does not stay as the answer.
runRecipethen asks the source one more time. - The names of the crawler. A recipe cannot take a name of
RESERVED_RECIPES:page,archive-copy,archive-index,robots.txt,sitemapandfeed. - Register the recipe at open. Give it to
openCrawler({ recipes: [menu] }). Thenreindexreads its captures before the first fetch.
The library API gives each value of a recipe, and the README of the package gives each rule.