Updated openlibrary api query url

Updated cover field
Added workaround to get specific editions title and covers
This commit is contained in:
ltctceplrm 2026-01-14 02:53:17 +01:00
parent fa691741ce
commit f973d15c4f

View file

@ -8,16 +8,23 @@ import { APIModel } from '../APIModel';
import type { paths } from '../schemas/OpenLibrary'; import type { paths } from '../schemas/OpenLibrary';
interface SearchResponse { interface SearchResponse {
cover_i: number; editions: {
has_fulltext: boolean; docs: {
edition_count: number; key?: string;
title: string; title?: string;
author_name: string[]; cover_i?: number;
first_publish_year: number; isbn?: string[];
}[];
};
cover_i?: number;
has_fulltext?: boolean;
edition_count?: number;
title?: string;
author_name?: string[];
first_publish_year?: number;
key: string; key: string;
number_of_pages_median?: number; number_of_pages_median?: number;
cover_edition_key?: string;
isbn?: string[]; isbn?: string[];
ratings_average?: number; ratings_average?: number;
} }
@ -69,7 +76,7 @@ export class OpenLibraryAPI extends APIModel {
year: result.first_publish_year?.toString() ?? 'unknown', year: result.first_publish_year?.toString() ?? 'unknown',
dataSource: this.apiName, dataSource: this.apiName,
id: result.key, id: result.key,
author: result.author_name.join(', '), author: result.author_name?.join(', '),
}), }),
); );
} }
@ -85,8 +92,8 @@ export class OpenLibraryAPI extends APIModel {
const response = await client.GET('/search.json', { const response = await client.GET('/search.json', {
params: { params: {
query: { query: {
q: `key:${id}`, q: `${id}`,
fields: 'key,title,author_name,number_of_pages_median,first_publish_year,isbn,ratings_score,first_sentence,title_suggest,rating*,cover_edition_key', fields: 'key,title,author_name,number_of_pages_median,first_publish_year,isbn,ratings_score,first_sentence,title_suggest,rating*,cover*,editions',
}, },
}, },
fetch: obsidianFetch, fetch: obsidianFetch,
@ -98,31 +105,44 @@ export class OpenLibraryAPI extends APIModel {
const data = response.data as { const data = response.data as {
docs: SearchResponse[]; docs: SearchResponse[];
q?: string;
}; };
// TODO: maybe description.
// console.debug(data);
const result = data.docs[0]; const result = data.docs[0];
let key = result.key;
let title = result.title;
let cover_i = result.cover_i;
let isbnArr = result.isbn;
// Check if the query is for /isbn/ or /books/ and extract from editions.docs if present
const q = data.q ?? '';
if ((q.includes('/isbn/') || q.includes('/books/')) && result.editions && Array.isArray(result.editions.docs) && result.editions.docs.length > 0) {
const edition = result.editions.docs[0];
key = edition.key ?? key;
title = edition.title ?? title;
cover_i = edition.cover_i ?? cover_i;
isbnArr = edition.isbn ?? isbnArr;
}
const pages = Number(result.number_of_pages_median); const pages = Number(result.number_of_pages_median);
const isbn = Number((result.isbn ?? []).find((el: string) => el.length <= 10)); const isbn = Number((isbnArr ?? []).find((el: string) => el.length <= 10));
const isbn13 = Number((result.isbn ?? []).find((el: string) => el.length == 13)); const isbn13 = Number((isbnArr ?? []).find((el: string) => el.length == 13));
return new BookModel({ return new BookModel({
title: result.title, title: title,
year: result.first_publish_year?.toString() ?? 'unknown', year: result.first_publish_year?.toString() ?? 'unknown',
dataSource: this.apiName, dataSource: this.apiName,
url: `https://openlibrary.org` + result.key, url: `https://openlibrary.org` + key,
id: result.key, id: key,
isbn: Number.isNaN(isbn) ? undefined : isbn, isbn: Number.isNaN(isbn) ? undefined : isbn,
isbn13: Number.isNaN(isbn13) ? undefined : isbn13, isbn13: Number.isNaN(isbn13) ? undefined : isbn13,
englishTitle: result.title, englishTitle: title,
author: result.author_name.join(', '), author: result.author_name?.join(', '),
pages: Number.isNaN(pages) ? undefined : pages, pages: Number.isNaN(pages) ? undefined : pages,
onlineRating: result.ratings_average, onlineRating: result.ratings_average,
image: result.cover_edition_key ? `https://covers.openlibrary.org/b/OLID/` + result.cover_edition_key + `-L.jpg` : undefined, image: cover_i ? `https://covers.openlibrary.org/b/id/` + cover_i + `-L.jpg` : undefined,
released: true, released: true,