Skip to content

Commit 03d1c1d

Browse files
committed
Changed api search to use works rather than individual editions
1 parent a15af42 commit 03d1c1d

File tree

1 file changed

+60
-65
lines changed

1 file changed

+60
-65
lines changed

src/api/apis/OpenLibraryAPI.ts

Lines changed: 60 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -19,78 +19,73 @@ export class OpenLibraryAPI extends APIModel {
1919
this.types = [MediaType.Book];
2020
}
2121

22-
async searchByTitle(title: string): Promise<MediaTypeModel[]> {
23-
console.log(`MDB | api "${this.apiName}" queried by Title`);
24-
25-
const searchUrl = `https://openlibrary.org/search.json?title=${encodeURIComponent(title)}`;
26-
27-
const fetchData = await fetch(searchUrl);
28-
console.debug(fetchData);
29-
if (fetchData.status !== 200) {
30-
throw Error(`MDB | Received status code ${fetchData.status} from an API.`);
31-
}
32-
const data = await fetchData.json();
33-
34-
console.debug(data);
35-
36-
const ret: MediaTypeModel[] = [];
37-
38-
for (const result of data.docs) {
39-
ret.push(
40-
new BookModel({
41-
title: result.title,
42-
englishTitle: result.title_english ?? result.title,
43-
year: result.first_publish_year,
44-
dataSource: this.apiName,
45-
id: result.cover_edition_key ?? result.edition_key,
46-
} as BookModel)
47-
);
22+
async searchByTitle(title: string): Promise<MediaTypeModel[]> {
23+
console.log(`MDB | api "${this.apiName}" queried by Title`);
24+
25+
const searchUrl = `https://openlibrary.org/search.json?title=${encodeURIComponent(title)}`;
26+
27+
const fetchData = await fetch(searchUrl);
28+
console.debug(fetchData);
29+
if (fetchData.status !== 200) {
30+
throw Error(`MDB | Received status code ${fetchData.status} from an API.`);
31+
}
32+
const data = await fetchData.json();
33+
34+
console.debug(data);
35+
36+
const ret: MediaTypeModel[] = [];
37+
38+
for (const result of data.docs) {
39+
ret.push(
40+
new BookModel({
41+
title: result.title,
42+
englishTitle: result.title_english ?? result.title,
43+
year: result.first_publish_year,
44+
dataSource: this.apiName,
45+
id: result.key,
46+
} as BookModel)
47+
);
48+
}
49+
50+
return ret;
4851
}
4952

50-
return ret;
51-
}
52-
53-
async getById(id: string): Promise<MediaTypeModel> {
54-
console.log(`MDB | api "${this.apiName}" queried by ID`);
53+
async getById(id: string): Promise<MediaTypeModel> {
54+
console.log(`MDB | api "${this.apiName}" queried by ID`);
5555

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-
});
56+
const searchUrl = `https://openlibrary.org/search.json?q=key:${encodeURIComponent(id)}`;
57+
const fetchData = await fetch(searchUrl);
58+
console.debug(fetchData);
6359

64-
console.debug(fetchData);
60+
if (fetchData.status !== 200) {
61+
throw Error(`MDB | Received status code ${fetchData.status} from an API.`);
62+
}
6563

66-
if (fetchData.status !== 200) {
67-
throw Error(`MDB | Received status code ${fetchData.status} from an API.`);
68-
}
69-
70-
const result = await fetchData.json;
64+
const data = await fetchData.json();
65+
console.debug(data);
66+
const result = data.docs[0];
7167

72-
const model = new BookModel({
73-
title: result.title,
74-
year: new Date(result.publish_date).getFullYear().toString(),
75-
dataSource: this.apiName,
76-
url: `https://openlibrary.org` + result.key,
77-
id: result.key.slice(7),
78-
isbn10: result.isbn_10,
79-
englishTitle: result.title_english ?? result.title,
68+
const model = new BookModel({
69+
title: result.title,
70+
year: result.first_publish_year,
71+
dataSource: this.apiName,
72+
url: `https://openlibrary.org` + result.key,
73+
id: result.key,
74+
englishTitle: result.title_english ?? result.title,
8075

81-
author: result.authors.key ?? 'unknown',
82-
pages: result.number_of_pages ?? 'unknown',
83-
image: `https://covers.openlibrary.org/b/OLID/` + result.key.slice(7) + `-L.jpg` ?? '',
76+
author: result.author_name ?? 'unknown',
77+
pages: result.number_of_pages_median ?? 'unknown',
78+
image: `https://covers.openlibrary.org/b/OLID/` + result.cover_edition_key + `-L.jpg` ?? '',
8479

85-
released: true,
80+
released: true,
8681

87-
userData: {
88-
read: false,
89-
lastRead: '',
90-
personalRating: 0,
91-
},
92-
} as BookModel);
82+
userData: {
83+
read: false,
84+
lastRead: '',
85+
personalRating: 0,
86+
},
87+
} as BookModel);
9388

94-
return model;
95-
}
96-
}
89+
return model;
90+
}
91+
}

0 commit comments

Comments
 (0)