Skip to content

Commit cf36d19

Browse files
committed
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
1 parent 503b387 commit cf36d19

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

src/api/apis/OpenLibraryAPI.ts

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { MediaTypeModel } from '../../models/MediaTypeModel';
33
import MediaDbPlugin from '../../main';
44
import { BookModel } from 'src/models/BookModel';
55
import { requestUrl } from 'obsidian';
6+
import { contactEmail, mediaDbVersion, pluginName } from '../../utils/Utils';
67
import { MediaType } from '../../utils/MediaType';
78

89
export class OpenLibraryAPI extends APIModel {
@@ -21,7 +22,7 @@ export class OpenLibraryAPI extends APIModel {
2122
async searchByTitle(title: string): Promise<MediaTypeModel[]> {
2223
console.log(`MDB | api "${this.apiName}" queried by Title`);
2324

24-
const searchUrl = `https://openlibrary.org/search.json?title=${encodeURIComponent(title)}&limit=20}`;
25+
const searchUrl = `https://openlibrary.org/search.json?title=${encodeURIComponent(title)}`;
2526

2627
const fetchData = await fetch(searchUrl);
2728
console.debug(fetchData);
@@ -37,12 +38,11 @@ async searchByTitle(title: string): Promise<MediaTypeModel[]> {
3738
for (const result of data.docs) {
3839
ret.push(
3940
new BookModel({
40-
subType: '',
4141
title: result.title,
4242
englishTitle: result.title_english ?? result.title,
43-
year: result.year ?? result.aired?.prop?.from?.year ?? '',
43+
year: result.first_publish_year,
4444
dataSource: this.apiName,
45-
id: result.mal_id,
45+
id: result.cover_edition_key,
4646
} as BookModel)
4747
);
4848
}
@@ -53,28 +53,34 @@ async searchByTitle(title: string): Promise<MediaTypeModel[]> {
5353
async getById(id: string): Promise<MediaTypeModel> {
5454
console.log(`MDB | api "${this.apiName}" queried by ID`);
5555

56-
const searchUrl = `https://openlibrary.org/isbn/${encodeURIComponent(id)}.json`;
57-
const fetchData = await fetch(searchUrl);
56+
const searchUrl = `https://openlibrary.org/books/${encodeURIComponent(id)}.json`;
57+
const fetchData = await requestUrl({
58+
url: searchUrl,
59+
headers: {
60+
'User-Agent': `${pluginName}/${mediaDbVersion} (${contactEmail})`,
61+
},
62+
});
63+
64+
console.debug(fetchData);
5865

5966
if (fetchData.status !== 200) {
6067
throw Error(`MDB | Received status code ${fetchData.status} from an API.`);
6168
}
6269

63-
const data = await fetchData.json();
64-
console.debug(data);
65-
const result = data.data;
70+
const result = await fetchData.json;
6671

6772
const model = new BookModel({
68-
subType: '',
6973
title: result.title,
70-
year: new Date(result.publish_date.date).getFullYear().toString(),
74+
year: new Date(result.publish_date).getFullYear().toString(),
7175
dataSource: this.apiName,
7276
url: `https://openlibrary.org` + result.key,
73-
id: result.isbn_10,
77+
id: result.key.slice(7),
78+
isbn10: result.isbn_10,
79+
englishTitle: result.title_english ?? result.title,
7480

7581
author: result.authors.key ?? 'unknown',
7682
pages: result.number_of_pages ?? 'unknown',
77-
image: `https://covers.openlibrary.org/b/isbn/` + result.isbn_10 + `-L.jpg` ?? '',
83+
image: `https://covers.openlibrary.org/b/OLID/` + result.key.slice(7) + `-L.jpg` ?? '',
7884

7985
released: true,
8086

@@ -85,6 +91,6 @@ async getById(id: string): Promise<MediaTypeModel> {
8591
},
8692
} as BookModel);
8793

88-
return;
94+
return model;
8995
}
9096
}

src/models/BookModel.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ import { MediaType } from '../utils/MediaType';
55
export class BookModel extends MediaTypeModel {
66
author: string;
77
pages: string;
8+
isbn10: string;
89
image: string;
10+
english_title: string;
911

1012
released: boolean;
1113

0 commit comments

Comments
 (0)