Find a number for books the catalogues have no LC number for
Google Books joins Open Library and the Library of Congress as a source of title, authors, publisher and categories when no number is in hand. The similar-books tier widens its search step by step (other word forms, place and topic words from the subtitle and publisher, fewer words) when the first search finds no class, and needs at least two agreeing books before it accepts one. The result is marked approximate. Books with no number or an approximate one are looked up again at startup once the last lookup is a week old. An ISBN no catalogue knows that is within three digits of a book already here says it may be a misread of that book. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
1 parent
ddf6020731
commit
a08fbff685
21 files changed
+750
-30
No files matched your search
+49
-8
@@ -12,6 +12,12 @@ export const DEFAULT_RETRY_DELAYS_MS = [5_000, 30_000, 120_000];
|
||||
*/
|
||||
export const SETTLE_AFTER_ATTEMPTS = 6;
|
||||
|
||||
/** A book with no number, or only an approximate one, is asked about again at the next start once this old. */
|
||||
export const RECHECK_AFTER_MS = 7 * 24 * 60 * 60 * 1000;
|
||||
|
||||
/** The most digits of an ISBN that may differ for a book already here to be named as the likely original of a misread. */
|
||||
export const MISREAD_MAX_DIFF = 3;
|
||||
|
||||
export const BACKOFF_FIRST_MS = 2_000;
|
||||
export const BACKOFF_CAP_MS = 300_000;
|
||||
|
||||
@@ -35,10 +41,14 @@ export type ResolverLike = Resolver;
|
||||
|
||||
export interface Jobs {
|
||||
/** `fresh`: the lookup ignores every cached source answer ("Look up again"), through its retries too. */
|
||||
enqueue(id: number, opts?: { fresh?: boolean }): void;
|
||||
enqueue(id: number, opts?: { fresh?: boolean; recheck?: boolean }): void;
|
||||
/** The book was removed: its waiting retry, queue place and remembered answers all go. */
|
||||
cancel(id: number): void;
|
||||
/** At start: pending books (and books an older version gave up on only because a source was unreachable) go on. */
|
||||
/**
|
||||
* At start: pending books (and books an older version gave up on only because a source was unreachable) go on, and
|
||||
* books with no number or an approximate one that were last looked up over 7 days ago are asked about again.
|
||||
* A hand-entered number is never touched; a recheck never takes a number away or swaps one guess for another.
|
||||
*/
|
||||
resume(): void;
|
||||
/**
|
||||
* Backfill, for books made before the hierarchy tail existed: look up only the tail (nothing else about the book
|
||||
@@ -72,6 +82,14 @@ function failingSource(e: unknown, message: string): string {
|
||||
: /openlibrary|Open Library/i.test(message) ? 'Open Library' : 'A lookup service';
|
||||
}
|
||||
|
||||
/** How many of the 13 digits differ; null when either is not 13 digits. */
|
||||
function isbnDistance(a: string, b: string): number | null {
|
||||
if (a.length !== 13 || b.length !== 13) return null;
|
||||
let n = 0;
|
||||
for (let i = 0; i < 13; i++) if (a[i] !== b[i]) n++;
|
||||
return n;
|
||||
}
|
||||
|
||||
export function createJobs(deps: {
|
||||
db: Db; resolver: ResolverLike; onChange: (book: Book) => void;
|
||||
/** The tail and edition backfills' schedule (they give up until the next start). */
|
||||
@@ -92,6 +110,8 @@ export function createJobs(deps: {
|
||||
const queued = new Set<number>();
|
||||
const attempts = new Map<number, number>();
|
||||
const fresh = new Set<number>();
|
||||
// Books asked about again at start although they are not pending: their lookup must not disturb what they have.
|
||||
const rechecks = new Set<number>();
|
||||
// What each pending lookup has been definitively told so far (see jobMemo): a retry asks only for the rest.
|
||||
const memos = new Map<number, Map<string, string>>();
|
||||
// The lookup being attempted now, and the books asked for again meanwhile (true: "fresh"): they run once more after.
|
||||
@@ -131,7 +151,7 @@ export function createJobs(deps: {
|
||||
|
||||
/** Everything remembered about a lookup that has ended (or been dropped). */
|
||||
function forget(id: number): void {
|
||||
attempts.delete(id); fresh.delete(id); memos.delete(id); db.setRetry(id, null);
|
||||
attempts.delete(id); fresh.delete(id); rechecks.delete(id); memos.delete(id); db.setRetry(id, null);
|
||||
}
|
||||
|
||||
function schedule(id: number, delayMs: number): void {
|
||||
@@ -149,7 +169,8 @@ export function createJobs(deps: {
|
||||
|
||||
async function attempt(id: number): Promise<void> {
|
||||
const book = db.getBook(id);
|
||||
if (!book || book.status !== 'pending') { forget(id); return; }
|
||||
if (!book || (book.status !== 'pending' && !rechecks.has(id))) { forget(id); return; }
|
||||
const recheck = rechecks.has(id);
|
||||
const n = attempts.get(id) ?? 0;
|
||||
const input: ResolveInput = {};
|
||||
if (book.isbn13) input.isbn13 = book.isbn13;
|
||||
@@ -184,19 +205,35 @@ export function createJobs(deps: {
|
||||
if (cur.edition === null) tail.edition = res.edition;
|
||||
else if (wasFresh && res.edition !== '') tail.edition = res.edition;
|
||||
}
|
||||
const lookedUpAt = new Date(now()).toISOString();
|
||||
// A recheck of a book that has an approximate number takes nothing away and swaps one guess for another never:
|
||||
// it only changes the book when the catalogues now give an exact number (or the book had none).
|
||||
const keepsOwn = recheck && !!cur.callNumber && (!res.callNumber || res.confidence !== 'exact');
|
||||
if (cur.manualOverride) {
|
||||
emit(id, { dewey, ...(cur.hierarchyTail === null ? { hierarchyTail: [] } : {}), ...(tail.edition !== undefined ? { edition: tail.edition } : {}) });
|
||||
emit(id, { dewey, lookedUpAt, ...(cur.hierarchyTail === null ? { hierarchyTail: [] } : {}), ...(tail.edition !== undefined ? { edition: tail.edition } : {}) });
|
||||
log(`[lookup] ${label}: kept the hand-entered call number (${took()})`);
|
||||
} else if (keepsOwn) {
|
||||
emit(id, { lookedUpAt });
|
||||
log(`[lookup] ${label}: looked up again, nothing better than ${cur.callNumber} (${took()})`);
|
||||
} else if (res.callNumber) {
|
||||
emit(id, {
|
||||
callNumber: res.callNumber, sortKey: res.sortKey, confidence: res.confidence,
|
||||
source: res.source, dewey, status: 'resolved', attentionReason: null, ...tail,
|
||||
source: res.source, dewey, status: 'resolved', attentionReason: null, lookedUpAt, ...tail,
|
||||
});
|
||||
log(`[lookup] ${label}: ${res.callNumber} (${res.source === 'similar' && similar ? `similar ${similar}` : res.source}) in ${took()}`);
|
||||
} else {
|
||||
// Only here, with every source answered for real, is the person asked to look.
|
||||
const reason = res.reason ?? 'No Library of Congress number found';
|
||||
emit(id, { dewey, status: 'needs_attention', attentionReason: reason, ...tail });
|
||||
let reason = res.reason ?? 'No Library of Congress number found';
|
||||
if (!res.meta && cur.isbn13) {
|
||||
// No catalogue has this ISBN at all: a book already here with nearly the same digits is the likely original.
|
||||
let best: { book: Book; d: number } | null = null;
|
||||
for (const o of db.listBooks()) {
|
||||
const d = o.id !== id && o.isbn13 ? isbnDistance(cur.isbn13, o.isbn13) : null;
|
||||
if (d !== null && d <= MISREAD_MAX_DIFF && (!best || d < best.d)) best = { book: o, d };
|
||||
}
|
||||
if (best) reason = `No catalogue has this ISBN. It may be a misread of ${best.book.title} (${best.book.isbn13}), which is already here.`;
|
||||
}
|
||||
emit(id, { dewey, status: 'needs_attention', attentionReason: reason, lookedUpAt, ...tail });
|
||||
log(`[lookup] ${label}: no call number, ${reason} in ${took()}`);
|
||||
}
|
||||
} catch (e) {
|
||||
@@ -315,6 +352,7 @@ export function createJobs(deps: {
|
||||
|
||||
return {
|
||||
enqueue(id, opts) {
|
||||
if (opts?.recheck) rechecks.add(id);
|
||||
if (opts?.fresh) { fresh.add(id); memos.delete(id); }
|
||||
// A fresh request (scan, "Look up again") overrides any wait and starts the retries over.
|
||||
const t = waiting.get(id);
|
||||
@@ -338,6 +376,9 @@ export function createJobs(deps: {
|
||||
const n = db.requeueTemporaryFailures();
|
||||
if (n > 0) log(`[lookup] ${n} book(s) an earlier version gave up on because a source did not answer will be looked up again`);
|
||||
for (const id of db.listPendingIds()) this.enqueue(id);
|
||||
const stale = db.listRecheckIds(new Date(now() - RECHECK_AFTER_MS).toISOString());
|
||||
if (stale.length > 0) log(`[lookup] ${stale.length} book(s) with no number or an approximate one will be looked up again`);
|
||||
for (const id of stale) this.enqueue(id, { recheck: true });
|
||||
},
|
||||
resumeTails() {
|
||||
const n = db.recheckEmptyTails(TAIL_RULES_VERSION);
|
||||
|
||||
Reference in new issue
Block a user