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
This commit is contained in:
ltctceplrm 2023-09-22 23:10:52 +02:00
parent 503b387168
commit cf36d19e11
2 changed files with 22 additions and 14 deletions

View file

@ -3,6 +3,7 @@ import { MediaTypeModel } from '../../models/MediaTypeModel';
import MediaDbPlugin from '../../main'; import MediaDbPlugin from '../../main';
import { BookModel } from 'src/models/BookModel'; import { BookModel } from 'src/models/BookModel';
import { requestUrl } from 'obsidian'; import { requestUrl } from 'obsidian';
import { contactEmail, mediaDbVersion, pluginName } from '../../utils/Utils';
import { MediaType } from '../../utils/MediaType'; import { MediaType } from '../../utils/MediaType';
export class OpenLibraryAPI extends APIModel { export class OpenLibraryAPI extends APIModel {
@ -21,7 +22,7 @@ export class OpenLibraryAPI extends APIModel {
async searchByTitle(title: string): Promise<MediaTypeModel[]> { async searchByTitle(title: string): Promise<MediaTypeModel[]> {
console.log(`MDB | api "${this.apiName}" queried by Title`); 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); const fetchData = await fetch(searchUrl);
console.debug(fetchData); console.debug(fetchData);
@ -37,12 +38,11 @@ async searchByTitle(title: string): Promise<MediaTypeModel[]> {
for (const result of data.docs) { for (const result of data.docs) {
ret.push( ret.push(
new BookModel({ new BookModel({
subType: '',
title: result.title, title: result.title,
englishTitle: result.title_english ?? result.title, englishTitle: result.title_english ?? result.title,
year: result.year ?? result.aired?.prop?.from?.year ?? '', year: result.first_publish_year,
dataSource: this.apiName, dataSource: this.apiName,
id: result.mal_id, id: result.cover_edition_key,
} as BookModel) } as BookModel)
); );
} }
@ -53,28 +53,34 @@ async searchByTitle(title: string): Promise<MediaTypeModel[]> {
async getById(id: string): Promise<MediaTypeModel> { async getById(id: string): Promise<MediaTypeModel> {
console.log(`MDB | api "${this.apiName}" queried by ID`); console.log(`MDB | api "${this.apiName}" queried by ID`);
const searchUrl = `https://openlibrary.org/isbn/${encodeURIComponent(id)}.json`; const searchUrl = `https://openlibrary.org/books/${encodeURIComponent(id)}.json`;
const fetchData = await fetch(searchUrl); const fetchData = await requestUrl({
url: searchUrl,
headers: {
'User-Agent': `${pluginName}/${mediaDbVersion} (${contactEmail})`,
},
});
console.debug(fetchData);
if (fetchData.status !== 200) { if (fetchData.status !== 200) {
throw Error(`MDB | Received status code ${fetchData.status} from an API.`); throw Error(`MDB | Received status code ${fetchData.status} from an API.`);
} }
const data = await fetchData.json(); const result = await fetchData.json;
console.debug(data);
const result = data.data;
const model = new BookModel({ const model = new BookModel({
subType: '',
title: result.title, title: result.title,
year: new Date(result.publish_date.date).getFullYear().toString(), year: new Date(result.publish_date).getFullYear().toString(),
dataSource: this.apiName, dataSource: this.apiName,
url: `https://openlibrary.org` + result.key, 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', author: result.authors.key ?? 'unknown',
pages: result.number_of_pages ?? '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, released: true,
@ -85,6 +91,6 @@ async getById(id: string): Promise<MediaTypeModel> {
}, },
} as BookModel); } as BookModel);
return; return model;
} }
} }

View file

@ -5,7 +5,9 @@ import { MediaType } from '../utils/MediaType';
export class BookModel extends MediaTypeModel { export class BookModel extends MediaTypeModel {
author: string; author: string;
pages: string; pages: string;
isbn10: string;
image: string; image: string;
english_title: string;
released: boolean; released: boolean;