Changed api search to use works rather than individual editions
This commit is contained in:
parent
a15af42d85
commit
03d1c1dc7d
1 changed files with 58 additions and 63 deletions
|
|
@ -19,78 +19,73 @@ export class OpenLibraryAPI extends APIModel {
|
||||||
this.types = [MediaType.Book];
|
this.types = [MediaType.Book];
|
||||||
}
|
}
|
||||||
|
|
||||||
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)}`;
|
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);
|
||||||
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 data = await fetchData.json();
|
||||||
|
|
||||||
console.debug(data);
|
console.debug(data);
|
||||||
|
|
||||||
const ret: MediaTypeModel[] = [];
|
const ret: MediaTypeModel[] = [];
|
||||||
|
|
||||||
for (const result of data.docs) {
|
for (const result of data.docs) {
|
||||||
ret.push(
|
ret.push(
|
||||||
new BookModel({
|
new BookModel({
|
||||||
title: result.title,
|
title: result.title,
|
||||||
englishTitle: result.title_english ?? result.title,
|
englishTitle: result.title_english ?? result.title,
|
||||||
year: result.first_publish_year,
|
year: result.first_publish_year,
|
||||||
dataSource: this.apiName,
|
dataSource: this.apiName,
|
||||||
id: result.cover_edition_key ?? result.edition_key,
|
id: result.key,
|
||||||
} as BookModel)
|
} as BookModel)
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret;
|
async getById(id: string): Promise<MediaTypeModel> {
|
||||||
}
|
console.log(`MDB | api "${this.apiName}" queried by ID`);
|
||||||
|
|
||||||
async getById(id: string): Promise<MediaTypeModel> {
|
const searchUrl = `https://openlibrary.org/search.json?q=key:${encodeURIComponent(id)}`;
|
||||||
console.log(`MDB | api "${this.apiName}" queried by ID`);
|
const fetchData = await fetch(searchUrl);
|
||||||
|
console.debug(fetchData);
|
||||||
|
|
||||||
const searchUrl = `https://openlibrary.org/books/${encodeURIComponent(id)}.json`;
|
if (fetchData.status !== 200) {
|
||||||
const fetchData = await requestUrl({
|
throw Error(`MDB | Received status code ${fetchData.status} from an API.`);
|
||||||
url: searchUrl,
|
}
|
||||||
headers: {
|
|
||||||
'User-Agent': `${pluginName}/${mediaDbVersion} (${contactEmail})`,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
console.debug(fetchData);
|
const data = await fetchData.json();
|
||||||
|
console.debug(data);
|
||||||
|
const result = data.docs[0];
|
||||||
|
|
||||||
if (fetchData.status !== 200) {
|
const model = new BookModel({
|
||||||
throw Error(`MDB | Received status code ${fetchData.status} from an API.`);
|
title: result.title,
|
||||||
|
year: result.first_publish_year,
|
||||||
|
dataSource: this.apiName,
|
||||||
|
url: `https://openlibrary.org` + result.key,
|
||||||
|
id: result.key,
|
||||||
|
englishTitle: result.title_english ?? result.title,
|
||||||
|
|
||||||
|
author: result.author_name ?? 'unknown',
|
||||||
|
pages: result.number_of_pages_median ?? 'unknown',
|
||||||
|
image: `https://covers.openlibrary.org/b/OLID/` + result.cover_edition_key + `-L.jpg` ?? '',
|
||||||
|
|
||||||
|
released: true,
|
||||||
|
|
||||||
|
userData: {
|
||||||
|
read: false,
|
||||||
|
lastRead: '',
|
||||||
|
personalRating: 0,
|
||||||
|
},
|
||||||
|
} as BookModel);
|
||||||
|
|
||||||
|
return model;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
const result = await fetchData.json;
|
|
||||||
|
|
||||||
const model = new BookModel({
|
|
||||||
title: result.title,
|
|
||||||
year: new Date(result.publish_date).getFullYear().toString(),
|
|
||||||
dataSource: this.apiName,
|
|
||||||
url: `https://openlibrary.org` + result.key,
|
|
||||||
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/OLID/` + result.key.slice(7) + `-L.jpg` ?? '',
|
|
||||||
|
|
||||||
released: true,
|
|
||||||
|
|
||||||
userData: {
|
|
||||||
read: false,
|
|
||||||
lastRead: '',
|
|
||||||
personalRating: 0,
|
|
||||||
},
|
|
||||||
} as BookModel);
|
|
||||||
|
|
||||||
return model;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue