first commit

This commit is contained in:
2025-01-20 23:58:11 +03:00
commit 1d307908c6
11 changed files with 713 additions and 0 deletions

85
lib/index.js Normal file
View 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);
// }
// })();

106
lib/module.js Normal file
View File

@@ -0,0 +1,106 @@
const { GoogleGenerativeAI } = require("@google/generative-ai");
const cheerio = require("cheerio");
const geminiDescriptionEdit = async (description, geminiApiKey, location) => {
if (geminiApiKey != undefined) {
const genAI = new GoogleGenerativeAI(geminiApiKey);
const model = genAI.getGenerativeModel({ model: "gemini-pro" });
let prompt;
if (location == "en") {
prompt =
description +
" based on the text above, provide a detailed book description of similar length. Do not add any comments to the text, remain completely faithful to it. Only provide the book description, without using a title like book description.";
console.log(prompt);
} else {
prompt =
description +
" yukarıdaki metine bağlı kalarak, benzer uzunlukta bir kitap tanımı ver. Metine hiçbir yorum katma, metine tamamen sadık kal. Sadece kitap tanımını ver, Kitap Tanımı şeklinde bir başlık kullanma";
}
const result = await model.generateContent(prompt);
const response = await result.response;
return response.text();
} else {
return description;
}
};
const extractBookId = (html) => {
const $ = cheerio.load(html);
let bookId = null;
$("[data-csa-c-item-id]").each((index, element) => {
const itemId = $(element).attr("data-csa-c-item-id");
if (!itemId.startsWith("amzn1.asin.1.B")) {
bookId = itemId
.replace(/^amzn1\.asin(\.amzn1)?\./, "")
.replace(/^1\./, "");
return false; // Exit loop after finding the first valid bookId
}
});
return bookId;
};
const extractBookPage = ($) => {
const text = $("div.rpi-attribute-value span").text(); // İçeriği al
const numberMatch = text.match(/\d+/); // Yalnızca rakamları al
return numberMatch ? parseInt(numberMatch[0], 10) : null; // Sayıyı döndür
};
const extractBookPublisher = ($) => {
const publisherParentDiv = $(".rpi-icon.book_details-publisher").parent();
const publisherDiv = publisherParentDiv.next();
const spanInsidePublisherSibling = publisherDiv.find("span");
return spanInsidePublisherSibling.text();
};
const extractBookDate = ($) => {
const dateParentDiv = $(".rpi-icon.book_details-publication_date").parent();
const dateDiv = dateParentDiv.next();
const spanInsideDateSibling = dateDiv.find("span");
return spanInsideDateSibling.text();
};
const extractBookDetails = async (html, isbn, geminiApiKey, location) => {
const extractedText = html
.match(
/<div\s[^>]*class="a-expander-content a-expander-partial-collapse-content"[^>]*>(.*?)<\/div>/s
)?.[1]
?.replace(/<[^>]+>/g, "")
.trim();
// Sonucu yazdır
const $ = cheerio.load(html);
const title = $("#imgTagWrapperId img").attr("alt");
const thumbImage = $("#imgTagWrapperId img").attr("src");
const authorName = $(".author a").text();
const descriptionRaw = $("#bookDescription_feature_div .a-expander-content")
.text()
.trim();
const description = await geminiDescriptionEdit(
descriptionRaw,
geminiApiKey,
location
);
const page = extractBookPage($);
const publisher = extractBookPublisher($);
const date = extractBookDate($);
const ratingText = $('i[data-hook="average-star-rating"] .a-icon-alt').text();
const rate = ratingText.match(/[\d.,]+/)[0].replace(",", ".");
return {
title,
thumbImage,
authorName,
description,
page,
publisher,
isbn,
date,
rate
};
};
module.exports = { extractBookId, extractBookDetails };