first commit
This commit is contained in:
106
lib/module.js
Normal file
106
lib/module.js
Normal 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 };
|
||||
Reference in New Issue
Block a user