first commit
This commit is contained in:
85
lib/index.js
Normal file
85
lib/index.js
Normal file
@@ -0,0 +1,85 @@
|
||||
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("Hata: " + 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("Hata: " + 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);
|
||||
// }
|
||||
// })();
|
||||
Reference in New Issue
Block a user