Files
amazon-book-search/lib/index.js
2025-01-21 00:39:42 +03:00

91 lines
2.5 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
const modules = require("./module");
const config = require("../config");
const axios = require("axios");
class AmazonBookSearch {
constructor(location) {
if (location !== "tr" && location !== "en") {
throw new Error("Yanlış konum!");
}
this.url = location === "tr" ? config.tr_base_url : config.en_base_url;
const fetchBookId = async (isbn) => {
const headers = config.headers;
try {
const url = encodeURI(this.url + isbn);
const response = await axios.get(url, { headers });
if (!response.data) {
throw new Error("Kitap bilgisi bulunamadı!");
}
const bookId = modules.extractBookId(response.data);
return bookId;
} catch (error) {
throw new Error(error.message);
}
};
this.getBookDetails = async (isbn, geminiApiKey) => {
const headers = config.headers;
try {
const bookId = await fetchBookId(isbn);
const url = encodeURI(
location == "tr"
? config.tr_detail_url + bookId
: config.en_detail_url + bookId
);
return new Promise((resolve, reject) => {
setTimeout(async () => {
try {
const response = await axios.get(url, { headers });
if (!response.data) {
throw new Error("Detay bilgisi bulunamadı!");
}
const details = await modules.extractBookDetails(
response.data,
isbn,
geminiApiKey,
location
);
resolve(details);
} catch (error) {
reject(error);
}
}, config.fetchTimeout);
});
} catch (error) {
throw new Error(error.message);
}
};
}
}
module.exports = AmazonBookSearch;
// Gemini API Key girilirse, amazon'da bulunan kitap açıklaması gemini tarafından yeniden oluşturulur.
// (async () => {
// try {
// const BookSearch = new AmazonBookSearch("tr");
// const bookDetails = await BookSearch.getBookDetails("9786257746168", "AIzaSyAY15XJcK1VIxzMRe48dyDEeNPqeqhQt2I");
// console.log(bookDetails);
// } catch (error) {
// console.log(error.message);
// }
// })();
// //
(async () => {
try {
const BookSearch = new AmazonBookSearch("en");
const bookDetails = await BookSearch.getBookDetails("0593724283");
console.log(bookDetails);
} catch (error) {
console.log(error.message);
}
})();