From 8a392e5e4e274d28175cd78008b757e6709e40ce Mon Sep 17 00:00:00 2001 From: ltctceplrm <14954927+ltctceplrm@users.noreply.github.com> Date: Fri, 27 Jun 2025 00:59:45 +0200 Subject: [PATCH] Improved openlibrary API + updated readme Fixed missing ratings, page numbers and also added an option to add specific editions using search by ID, this method also enables you to search by isbn ID. --- README.md | 6 ++++-- src/api/apis/OpenLibraryAPI.ts | 6 +++--- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index dd48ea0..0ffd6aa 100644 --- a/README.md +++ b/README.md @@ -162,9 +162,11 @@ Now you select the result you want and the plugin will cast it's magic and creat - you can find this ID in the URL - e.g. for "Factorio" the URL looks like this `https://store.steampowered.com/app/427520/Factorio/` so the ID is `427520` - [Open Library](https://openlibrary.org) - - The ID you need is the "work" ID and not the "book" ID, it needs to start with `/works/`. You can find this ID in the URL - - e.g. for "Fantastic Mr. Fox" the URL looks like this `https://openlibrary.org/works/OL45804W` so the ID is `/works/OL45804W` + - The ID can either be the "/work/" ID, the "/book/" ID, or the "/isbn/" ID it needs to start with `/works/`. You can find this ID in the URL + - e.g. for "Fantastic Mr. Fox" the "/works/" URL looks like this `https://openlibrary.org/works/OL45804W` so the ID is `/works/OL45804W` - This URL is located near the top of the page above the title, see `An edition of Fantastic Mr Fox (1970) ` + - For a specific edition of "Fantastic Mr. Fox" the "/books/" URL looks like this `https://openlibrary.org/books/OL3567303M/` so the ID is `/books/OL3567303M` + - This URL is located in the editions section` - [Moby Games](https://www.mobygames.com) - you can find this ID in the URL - e.g. for "Bioshock 2" the URL looks like this `https://www.mobygames.com/game/45089/bioshock-2/` so the ID is `45089` diff --git a/src/api/apis/OpenLibraryAPI.ts b/src/api/apis/OpenLibraryAPI.ts index 78eb301..45383f1 100644 --- a/src/api/apis/OpenLibraryAPI.ts +++ b/src/api/apis/OpenLibraryAPI.ts @@ -52,7 +52,7 @@ export class OpenLibraryAPI extends APIModel { async getById(id: string): Promise { console.log(`MDB | api "${this.apiName}" queried by ID`); - const searchUrl = `https://openlibrary.org/search.json?q=key:${encodeURIComponent(id)}`; + const searchUrl = `https://openlibrary.org/search.json?q=${encodeURIComponent(id)}&fields=key,title,author_name,number_of_pages_median,first_publish_year,isbn,ratings_score,first_sentence,title_suggest,rating*,cover_edition_key`; const fetchData = await fetch(searchUrl); // console.debug(fetchData); @@ -68,8 +68,8 @@ export class OpenLibraryAPI extends APIModel { title: result.title, year: result.first_publish_year, dataSource: this.apiName, - url: `https://openlibrary.org` + result.key, - id: result.key, + url: `https://openlibrary.org` + data.q, + id: data.q, isbn: (result.isbn ?? []).find((el: string | any[]) => el.length <= 10) ?? 'unknown', isbn13: (result.isbn ?? []).find((el: string | any[]) => el.length == 13) ?? 'unknown', englishTitle: result.title_english ?? result.title,