192 lines
8.8 KiB
JavaScript
192 lines
8.8 KiB
JavaScript
// ebay_core.js V1.1 - Shared Parsing & Extraction Logic
|
|
// Added itemCount and sizePerItemTB to output.
|
|
(function (root, factory) {
|
|
if (typeof module === 'object' && module.exports) {
|
|
module.exports = factory();
|
|
} else {
|
|
root.EbayParser = factory();
|
|
}
|
|
}(typeof self !== 'undefined' ? self : this, function () {
|
|
'use strict';
|
|
|
|
const EbayParser = {};
|
|
|
|
EbayParser.parseSizeAndQuantity = function(title) {
|
|
title = title ? title.toUpperCase() : "";
|
|
let totalTB = 0;
|
|
let quantity = 1;
|
|
let needed_description_check = false;
|
|
let individualSizeTB = 0; // Will hold the size per item
|
|
|
|
const explicitQtyPatterns = [
|
|
/\b(?:LOT\s+OF|LOT)\s*\(?\s*(\d+)\s*\)?/i,
|
|
/\b(?:LOT\s+OF|LOT)\s*\*\s*(\d+)/i,
|
|
/\b(?:PACK\s+OF|PACK|BULK)\s*\(?\s*(\d+)\s*\)?/i,
|
|
/\b(\d+)\s*-\s*PACK\b/i,
|
|
/\b(\d+)\s*COUNT\b/i
|
|
];
|
|
|
|
for (const pattern of explicitQtyPatterns) {
|
|
const qtyMatch = title.match(pattern);
|
|
if (qtyMatch && qtyMatch[1]) {
|
|
const parsedQty = parseInt(qtyMatch[1], 10);
|
|
if (parsedQty > 0 && parsedQty < 500) {
|
|
quantity = parsedQty;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
const sizeMatches = [];
|
|
const sizeRegex = /(\d+(?:\.\d+)?)\s*(TB|GB)(?:\b|(?=\s|-|,|\(|\)|$))/g;
|
|
let match;
|
|
while ((match = sizeRegex.exec(title)) !== null) {
|
|
sizeMatches.push({ value: parseFloat(match[1]), unit: match[2].toUpperCase() });
|
|
}
|
|
|
|
if (sizeMatches.length > 0) {
|
|
const uniqueSizesTB = [...new Set(
|
|
sizeMatches.map(sm => sm.unit === 'GB' ? sm.value / 1000 : sm.value)
|
|
)].sort((a, b) => a - b);
|
|
if (uniqueSizesTB.length > 0) {
|
|
individualSizeTB = uniqueSizesTB[0]; // Set individual size
|
|
if (uniqueSizesTB.length > 1) needed_description_check = true;
|
|
}
|
|
}
|
|
|
|
if (title.match(/\d+(?:\.\d+)?\s*(?:GB|TB)\s*(?:-|&|OR|TO)\s*\d+(?:\.\d+)?\s*(?:GB|TB)/i)) {
|
|
needed_description_check = true;
|
|
}
|
|
if (quantity > 1 && title.includes("MIXED")) {
|
|
needed_description_check = true;
|
|
}
|
|
if (title.includes("CHECK THE DESCRIPTION") || title.includes("CHECK DESCRIPTION") || title.includes("SEE DESCRIPTION")) {
|
|
if (quantity > 1 || sizeMatches.length === 0 || sizeMatches.length > 1) {
|
|
needed_description_check = true;
|
|
}
|
|
}
|
|
if (individualSizeTB > 0) {
|
|
totalTB = individualSizeTB * quantity;
|
|
}
|
|
if (quantity > 1 && totalTB === 0) {
|
|
needed_description_check = true;
|
|
}
|
|
if (quantity === 1 && sizeMatches.length === 1 && !needed_description_check) {
|
|
needed_description_check = false;
|
|
}
|
|
|
|
return {
|
|
totalTB: parseFloat(totalTB.toFixed(4)),
|
|
quantity: quantity, // Renamed to 'quantity' internally, maps to 'itemCount'
|
|
needed_description_check: needed_description_check,
|
|
individualSizeTB: parseFloat(individualSizeTB.toFixed(4)) // Added size per item
|
|
};
|
|
};
|
|
|
|
EbayParser.parsePrice = function(priceText) { /* ... (Keep existing parsePrice function) ... */
|
|
priceText = priceText || "";
|
|
if (priceText.toLowerCase().includes(' to ')) {
|
|
return null;
|
|
}
|
|
const priceMatch = priceText.match(/\$?([\d,]+\.?\d*)/);
|
|
if (priceMatch) {
|
|
return parseFloat(priceMatch[1].replace(/,/g, ''));
|
|
}
|
|
return null;
|
|
};
|
|
|
|
EbayParser.runUnitTests = function() { /* ... (Keep existing runUnitTests function) ... */
|
|
// Ensure console exists (for Node vs Browser safety, though Node has it)
|
|
const log = typeof console !== 'undefined' ? console.log : function() {};
|
|
const error = typeof console !== 'undefined' ? console.error : function() {};
|
|
|
|
log("Ebay Cost/TB: --- Running Unit Tests ---");
|
|
const testCases = [
|
|
// Add expected individualSizeTB to tests
|
|
{ title: "LOT OF (9) MAJOR BRAND 2.5\" 7MM SSD * Kingston, Samsung, SanDisk& PNY*120-250GB", expected: { totalTB: 1.080, quantity: 9, individualSizeTB: 0.120, needed_description_check: true } },
|
|
{ title: "Lot of 10 Intel 256 GB 2.5\" SATA SSD different Model check the Description", expected: { totalTB: 2.560, quantity: 10, individualSizeTB: 0.256, needed_description_check: true } },
|
|
{ title: "Bulk 5 Lot Samsung 870 EVO 500GB SSD SATA - Used - Tested Passed Smart Test", expected: { totalTB: 2.500, quantity: 5, individualSizeTB: 0.500, needed_description_check: false } },
|
|
{ title: "Samsung 1.6TB NVME PCIe 3.0 x8 2.75\" SSD MZPLK1T6HCHP PM1725 Series TLC", expected: { totalTB: 1.6, quantity: 1, individualSizeTB: 1.6, needed_description_check: false } },
|
|
{ title: "Micron 5100 MAX 1.84TB SATA 6Gb/s 2.5\" SSD MTFDDAK1T9TCC-1AR1ZABYY", expected: { totalTB: 1.84, quantity: 1, individualSizeTB: 1.84, needed_description_check: false } },
|
|
{ title: "10-PACK 1TB SSD", expected: { totalTB: 10.0, quantity: 10, individualSizeTB: 1.0, needed_description_check: false } },
|
|
];
|
|
|
|
let testsPassed = 0;
|
|
let testsFailed = 0;
|
|
|
|
testCases.forEach((test, index) => {
|
|
const result = EbayParser.parseSizeAndQuantity(test.title);
|
|
const tbCheck = Math.abs(result.totalTB - test.expected.totalTB) < 0.0001;
|
|
const qCheck = result.quantity === test.expected.quantity;
|
|
const sizeCheck = Math.abs(result.individualSizeTB - test.expected.individualSizeTB) < 0.0001;
|
|
const needCheck = result.needed_description_check === test.expected.needed_description_check;
|
|
|
|
if (tbCheck && qCheck && sizeCheck && needCheck) {
|
|
testsPassed++;
|
|
} else {
|
|
error(`Test ${index + 1}: FAILED - "${test.title}"`);
|
|
error(` Expected: TTB=${test.expected.totalTB.toFixed(4)}, Q=${test.expected.quantity}, STB=${test.expected.individualSizeTB.toFixed(4)}, Check=${test.expected.needed_description_check}`);
|
|
error(` Actual: TTB=${result.totalTB.toFixed(4)}, Q=${result.quantity}, STB=${result.individualSizeTB.toFixed(4)}, Check=${result.needed_description_check}`);
|
|
testsFailed++;
|
|
}
|
|
});
|
|
|
|
log(`--- Unit Test Summary: ${testsPassed} Passed, ${testsFailed} Failed ---`);
|
|
return testsFailed === 0;
|
|
};
|
|
|
|
// Updated to include itemCount and sizePerItemTB
|
|
EbayParser.extractDataFromPage = function() {
|
|
const itemSelector = 'li.s-item, li.srp-results__item, div.s-item[role="listitem"]';
|
|
const itemElements = document.querySelectorAll(itemSelector);
|
|
const items = [];
|
|
const today = new Date().toISOString();
|
|
|
|
itemElements.forEach(item => {
|
|
const titleElement = item.querySelector('.s-item__title, .srp-results__title');
|
|
const priceElement = item.querySelector('.s-item__price, .srp-results__price');
|
|
const linkElement = item.querySelector('.s-item__link, a[href*="/itm/"]');
|
|
|
|
const title = titleElement ? titleElement.innerText.trim() : null;
|
|
const priceText = priceElement ? priceElement.innerText.trim() : null;
|
|
const itemUrl = linkElement ? linkElement.href : null;
|
|
|
|
if (!title || !priceText || !itemUrl) return;
|
|
|
|
const listingPrice = EbayParser.parsePrice(priceText);
|
|
const parsedInfo = EbayParser.parseSizeAndQuantity(title);
|
|
const totalTB = parsedInfo.totalTB;
|
|
const quantity = parsedInfo.quantity; // Get quantity
|
|
const individualSizeTB = parsedInfo.individualSizeTB; // Get individual size
|
|
const needed_description_check = parsedInfo.needed_description_check;
|
|
|
|
let costPerTB = null;
|
|
if (listingPrice !== null && totalTB > 0) {
|
|
costPerTB = listingPrice / totalTB;
|
|
}
|
|
|
|
let itemId = null;
|
|
const itemMatch = itemUrl.match(/\/itm\/(\d+)/);
|
|
if (itemMatch && itemMatch[1]) {
|
|
itemId = itemMatch[1];
|
|
}
|
|
|
|
items.push({
|
|
title,
|
|
itemId,
|
|
dateFound: today,
|
|
listingPrice,
|
|
itemCount: quantity, // <-- Added
|
|
sizePerItemTB: individualSizeTB > 0 ? parseFloat(individualSizeTB.toFixed(3)) : null, // <-- Added
|
|
totalTB: totalTB > 0 ? parseFloat(totalTB.toFixed(3)) : null,
|
|
costPerTB: costPerTB !== null ? parseFloat(costPerTB.toFixed(2)) : null,
|
|
needed_description_check,
|
|
itemUrl
|
|
});
|
|
});
|
|
return items;
|
|
};
|
|
|
|
return EbayParser;
|
|
}));
|