Skip to content

Commit c87cb09

Browse files
Merge pull request #192 from ltctceplrm/openlibrary
Added plot/description to openlibraryAPI when available
2 parents bb663cd + 1259657 commit c87cb09

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

src/api/apis/OpenLibraryAPI.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type MediaDbPlugin from '../../main';
33
import type { MediaTypeModel } from '../../models/MediaTypeModel';
44
import { MediaType } from '../../utils/MediaType';
55
import { APIModel } from '../APIModel';
6+
import { requestUrl } from 'obsidian';
67

78
export class OpenLibraryAPI extends APIModel {
89
plugin: MediaDbPlugin;
@@ -53,14 +54,24 @@ export class OpenLibraryAPI extends APIModel {
5354
console.log(`MDB | api "${this.apiName}" queried by ID`);
5455

5556
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`;
56-
const fetchData = await fetch(searchUrl);
57+
const fetchData = await requestUrl({
58+
url: searchUrl,
59+
});
60+
const descriptionUrl = `https://openlibrary.org/${encodeURIComponent(id)}.json`;
61+
const fetchDescription = await requestUrl({
62+
url: descriptionUrl,
63+
});
5764
// console.debug(fetchData);
5865

5966
if (fetchData.status !== 200) {
6067
throw Error(`MDB | Received status code ${fetchData.status} from ${this.apiName}.`);
6168
}
69+
if (fetchDescription.status !== 200) {
70+
throw Error(`MDB | Received status code ${fetchDescription.status} from ${this.apiName}.`);
71+
}
6272

63-
const data = await fetchData.json();
73+
const data = await fetchData.json;
74+
const resultdesc = await fetchDescription.json;
6475
// console.debug(data);
6576
const result = data.docs[0];
6677

@@ -75,7 +86,7 @@ export class OpenLibraryAPI extends APIModel {
7586
englishTitle: result.title_english ?? result.title,
7687

7788
author: result.author_name ?? 'unknown',
78-
plot: result.description ?? 'unknown',
89+
plot: resultdesc.description?.value ?? 'unknown',
7990
pages: result.number_of_pages_median ?? 'unknown',
8091
onlineRating: Number.parseFloat(Number(result.ratings_average ?? 0).toFixed(2)),
8192
image: `https://covers.openlibrary.org/b/OLID/` + result.cover_edition_key + `-L.jpg`,

0 commit comments

Comments
 (0)