2024-05-25 23:32:02 +02:00
|
|
|
const axios = require('axios').default
|
2021-08-18 00:01:11 +02:00
|
|
|
|
|
|
|
class OpenLibrary {
|
2024-05-25 23:32:02 +02:00
|
|
|
#responseTimeout = 30000
|
|
|
|
|
2021-08-18 00:01:11 +02:00
|
|
|
constructor() {
|
|
|
|
this.baseUrl = 'https://openlibrary.org'
|
|
|
|
}
|
|
|
|
|
2024-05-25 23:32:02 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {string} uri
|
|
|
|
* @param {number} timeout
|
|
|
|
* @returns {Promise<Object>}
|
|
|
|
*/
|
|
|
|
get(uri, timeout = this.#responseTimeout) {
|
|
|
|
if (!timeout || isNaN(timeout)) timeout = this.#responseTimeout
|
|
|
|
return axios
|
|
|
|
.get(`${this.baseUrl}/${uri}`, {
|
|
|
|
timeout
|
|
|
|
})
|
|
|
|
.then((res) => {
|
|
|
|
return res.data
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
console.error('Failed', error)
|
|
|
|
return null
|
|
|
|
})
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async isbnLookup(isbn) {
|
|
|
|
var lookupData = await this.get(`/isbn/${isbn}`)
|
|
|
|
if (!lookupData) {
|
|
|
|
return {
|
|
|
|
errorCode: 404
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return lookupData
|
|
|
|
}
|
|
|
|
|
|
|
|
async getWorksData(worksKey) {
|
|
|
|
var worksData = await this.get(`${worksKey}.json`)
|
2021-08-26 00:36:54 +02:00
|
|
|
if (!worksData) {
|
|
|
|
return {
|
|
|
|
errorMsg: 'Works Data Request failed',
|
|
|
|
errorCode: 500
|
|
|
|
}
|
|
|
|
}
|
2021-08-18 00:01:11 +02:00
|
|
|
if (!worksData.covers) worksData.covers = []
|
2024-05-25 23:32:02 +02:00
|
|
|
var coverImages = worksData.covers.filter((c) => c > 0).map((c) => `https://covers.openlibrary.org/b/id/${c}-L.jpg`)
|
2021-08-18 00:01:11 +02:00
|
|
|
var description = null
|
|
|
|
if (worksData.description) {
|
|
|
|
if (typeof worksData.description === 'string') {
|
|
|
|
description = worksData.description
|
|
|
|
} else {
|
|
|
|
description = worksData.description.value || null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
id: worksKey.split('/').pop(),
|
|
|
|
key: worksKey,
|
|
|
|
covers: coverImages,
|
|
|
|
first_publish_date: worksData.first_publish_date,
|
|
|
|
description: description
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-28 21:41:42 +02:00
|
|
|
parsePublishYear(doc, worksData) {
|
|
|
|
if (doc.first_publish_year && !isNaN(doc.first_publish_year)) return doc.first_publish_year
|
|
|
|
if (worksData.first_publish_date) {
|
|
|
|
var year = worksData.first_publish_date.split('-')[0]
|
|
|
|
if (!isNaN(year)) return year
|
|
|
|
}
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
2021-08-18 00:01:11 +02:00
|
|
|
async cleanSearchDoc(doc) {
|
|
|
|
var worksData = await this.getWorksData(doc.key)
|
|
|
|
return {
|
|
|
|
title: doc.title,
|
|
|
|
author: doc.author_name ? doc.author_name.join(', ') : null,
|
2022-03-14 01:34:31 +01:00
|
|
|
publishedYear: this.parsePublishYear(doc, worksData),
|
2021-08-18 00:01:11 +02:00
|
|
|
edition: doc.cover_edition_key,
|
|
|
|
cover: doc.cover_edition_key ? `https://covers.openlibrary.org/b/OLID/${doc.cover_edition_key}-L.jpg` : null,
|
|
|
|
...worksData
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async search(query) {
|
2024-05-25 23:32:02 +02:00
|
|
|
var queryString = Object.keys(query)
|
|
|
|
.map((key) => key + '=' + query[key])
|
|
|
|
.join('&')
|
2021-08-18 00:01:11 +02:00
|
|
|
var lookupData = await this.get(`/search.json?${queryString}`)
|
|
|
|
if (!lookupData) {
|
|
|
|
return {
|
|
|
|
errorCode: 404
|
|
|
|
}
|
|
|
|
}
|
2024-05-25 23:32:02 +02:00
|
|
|
var searchDocs = await Promise.all(lookupData.docs.map((d) => this.cleanSearchDoc(d)))
|
2021-08-18 00:01:11 +02:00
|
|
|
return searchDocs
|
|
|
|
}
|
2021-08-21 16:15:44 +02:00
|
|
|
|
2024-05-25 23:32:02 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {string} title
|
|
|
|
* @param {number} timeout
|
|
|
|
* @returns {Promise<Object[]>}
|
|
|
|
*/
|
|
|
|
async searchTitle(title, timeout = this.#responseTimeout) {
|
|
|
|
title = encodeURIComponent(title)
|
|
|
|
var lookupData = await this.get(`/search.json?title=${title}`, timeout)
|
2021-08-21 16:15:44 +02:00
|
|
|
if (!lookupData) {
|
|
|
|
return {
|
|
|
|
errorCode: 404
|
|
|
|
}
|
|
|
|
}
|
2024-05-25 23:32:02 +02:00
|
|
|
var searchDocs = await Promise.all(lookupData.docs.map((d) => this.cleanSearchDoc(d)))
|
2021-08-21 16:15:44 +02:00
|
|
|
return searchDocs
|
|
|
|
}
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
2024-05-25 23:32:02 +02:00
|
|
|
module.exports = OpenLibrary
|