From cf36d19e11b1e129c0ba9e887bdce6229a9e3e72 Mon Sep 17 00:00:00 2001 From: ltctceplrm <14954927+ltctceplrm@users.noreply.github.com> Date: Fri, 22 Sep 2023 23:10:52 +0200 Subject: [PATCH] Fixed bugs Added a header so it doesn't limit the rate of requests as fast Fixed year missing from title Replaced ID from isbn to OpenLibrary ID Added ISBN10 as separate field --- src/api/apis/OpenLibraryAPI.ts | 34 ++++++++++++++++++++-------------- src/models/BookModel.ts | 2 ++ 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/src/api/apis/OpenLibraryAPI.ts b/src/api/apis/OpenLibraryAPI.ts index f46018d..e00c121 100644 --- a/src/api/apis/OpenLibraryAPI.ts +++ b/src/api/apis/OpenLibraryAPI.ts @@ -3,6 +3,7 @@ import { MediaTypeModel } from '../../models/MediaTypeModel'; import MediaDbPlugin from '../../main'; import { BookModel } from 'src/models/BookModel'; import { requestUrl } from 'obsidian'; +import { contactEmail, mediaDbVersion, pluginName } from '../../utils/Utils'; import { MediaType } from '../../utils/MediaType'; export class OpenLibraryAPI extends APIModel { @@ -21,7 +22,7 @@ export class OpenLibraryAPI extends APIModel { async searchByTitle(title: string): Promise { console.log(`MDB | api "${this.apiName}" queried by Title`); - const searchUrl = `https://openlibrary.org/search.json?title=${encodeURIComponent(title)}&limit=20}`; + const searchUrl = `https://openlibrary.org/search.json?title=${encodeURIComponent(title)}`; const fetchData = await fetch(searchUrl); console.debug(fetchData); @@ -37,12 +38,11 @@ async searchByTitle(title: string): Promise { for (const result of data.docs) { ret.push( new BookModel({ - subType: '', title: result.title, englishTitle: result.title_english ?? result.title, - year: result.year ?? result.aired?.prop?.from?.year ?? '', + year: result.first_publish_year, dataSource: this.apiName, - id: result.mal_id, + id: result.cover_edition_key, } as BookModel) ); } @@ -53,28 +53,34 @@ async searchByTitle(title: string): Promise { async getById(id: string): Promise { console.log(`MDB | api "${this.apiName}" queried by ID`); - const searchUrl = `https://openlibrary.org/isbn/${encodeURIComponent(id)}.json`; - const fetchData = await fetch(searchUrl); + const searchUrl = `https://openlibrary.org/books/${encodeURIComponent(id)}.json`; + const fetchData = await requestUrl({ + url: searchUrl, + headers: { + 'User-Agent': `${pluginName}/${mediaDbVersion} (${contactEmail})`, + }, + }); + + console.debug(fetchData); if (fetchData.status !== 200) { throw Error(`MDB | Received status code ${fetchData.status} from an API.`); } - const data = await fetchData.json(); - console.debug(data); - const result = data.data; + const result = await fetchData.json; const model = new BookModel({ - subType: '', title: result.title, - year: new Date(result.publish_date.date).getFullYear().toString(), + year: new Date(result.publish_date).getFullYear().toString(), dataSource: this.apiName, url: `https://openlibrary.org` + result.key, - id: result.isbn_10, + id: result.key.slice(7), + isbn10: result.isbn_10, + englishTitle: result.title_english ?? result.title, author: result.authors.key ?? 'unknown', pages: result.number_of_pages ?? 'unknown', - image: `https://covers.openlibrary.org/b/isbn/` + result.isbn_10 + `-L.jpg` ?? '', + image: `https://covers.openlibrary.org/b/OLID/` + result.key.slice(7) + `-L.jpg` ?? '', released: true, @@ -85,6 +91,6 @@ async getById(id: string): Promise { }, } as BookModel); - return; + return model; } } diff --git a/src/models/BookModel.ts b/src/models/BookModel.ts index a36ba21..d0f0235 100644 --- a/src/models/BookModel.ts +++ b/src/models/BookModel.ts @@ -5,7 +5,9 @@ import { MediaType } from '../utils/MediaType'; export class BookModel extends MediaTypeModel { author: string; pages: string; + isbn10: string; image: string; + english_title: string; released: boolean;