Skip to content

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.

  • The name goes into each capture record, so it is part of each capture id. reindex finds the extractor by it. Do not change it after the first capture.
  • The request (fetch) changes an input into a URL. With no fetch, 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:

recipe-example.ts
/**
* 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 },
}));
},
});

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.

  1. The input
  2. The request
  3. The capture
  4. The extractor
  5. The documents
  6. 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.

  1. 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.
  2. 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.
  3. 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.
  4. The documents. One capture can give many documents. Each document is a version in the index, with the capture that it came from.
  5. The reading. find, read, verify, extract, diff and the time travel use the documents. reindex reads each capture of the recipe again, with no request.
  • The mode. page (the default) is a web page: robots.txt applies. api is a documented API with terms of its own: robots.txt does not apply, and the pace of minInterval does.
  • 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. runRecipe then 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, sitemap and feed.
  • Register the recipe at open. Give it to openCrawler({ recipes: [menu] }). Then reindex reads its captures before the first fetch.

The library API gives each value of a recipe, and the README of the package gives each rule.