Even more updates (mostly CLI focused)
This commit is contained in:
145
ebay_core.js
145
ebay_core.js
@ -1,5 +1,7 @@
|
||||
// ebay_core.js V1.1 - Shared Parsing & Extraction Logic
|
||||
// Added itemCount and sizePerItemTB to output.
|
||||
// ebay_core.js V1.4 - Shared Parsing & Extraction Logic
|
||||
// - Restructured JSON output with a "parsed" sub-object.
|
||||
// - Added parser_engine version.
|
||||
// - Removed itemUrl, added image_url.
|
||||
(function (root, factory) {
|
||||
if (typeof module === 'object' && module.exports) {
|
||||
module.exports = factory();
|
||||
@ -10,13 +12,14 @@
|
||||
'use strict';
|
||||
|
||||
const EbayParser = {};
|
||||
const PARSER_ENGINE_VERSION = 1;
|
||||
|
||||
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
|
||||
let individualSizeTB = 0;
|
||||
|
||||
const explicitQtyPatterns = [
|
||||
/\b(?:LOT\s+OF|LOT)\s*\(?\s*(\d+)\s*\)?/i,
|
||||
@ -49,7 +52,7 @@
|
||||
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
|
||||
individualSizeTB = uniqueSizesTB[0];
|
||||
if (uniqueSizesTB.length > 1) needed_description_check = true;
|
||||
}
|
||||
}
|
||||
@ -71,21 +74,26 @@
|
||||
if (quantity > 1 && totalTB === 0) {
|
||||
needed_description_check = true;
|
||||
}
|
||||
if (quantity === 1 && sizeMatches.length === 1 && !needed_description_check) {
|
||||
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'
|
||||
quantity: quantity,
|
||||
needed_description_check: needed_description_check,
|
||||
individualSizeTB: parseFloat(individualSizeTB.toFixed(4)) // Added size per item
|
||||
individualSizeTB: parseFloat(individualSizeTB.toFixed(4))
|
||||
};
|
||||
};
|
||||
|
||||
EbayParser.parsePrice = function(priceText) { /* ... (Keep existing parsePrice function) ... */
|
||||
EbayParser.parsePrice = function(priceText) {
|
||||
priceText = priceText || "";
|
||||
if (priceText.toLowerCase().includes(' to ')) {
|
||||
const rangeParts = priceText.split(/to/i);
|
||||
const firstPriceMatch = rangeParts[0] ? rangeParts[0].match(/\$?([\d,]+\.?\d*)/) : null;
|
||||
if (firstPriceMatch) {
|
||||
return parseFloat(firstPriceMatch[1].replace(/,/g, ''));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
const priceMatch = priceText.match(/\$?([\d,]+\.?\d*)/);
|
||||
@ -95,14 +103,11 @@
|
||||
return null;
|
||||
};
|
||||
|
||||
EbayParser.runUnitTests = function() { /* ... (Keep existing runUnitTests function) ... */
|
||||
// Ensure console exists (for Node vs Browser safety, though Node has it)
|
||||
EbayParser.runUnitTests = function() {
|
||||
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 } },
|
||||
@ -110,32 +115,26 @@
|
||||
{ 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 {
|
||||
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);
|
||||
@ -144,44 +143,104 @@
|
||||
|
||||
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 priceElement = item.querySelector('.s-item__price');
|
||||
// const linkElement = item.querySelector('.s-item__link, a[href*="/itm/"]'); // Not used for itemUrl anymore
|
||||
const imageElement = item.querySelector('.s-item__image-wrapper img.s-item__image-img, .s-item__image img'); // Common image selectors
|
||||
|
||||
const title = titleElement ? titleElement.innerText.trim() : null;
|
||||
let rawTitle = titleElement ? titleElement.innerText.trim() : null;
|
||||
const priceText = priceElement ? priceElement.innerText.trim() : null;
|
||||
const itemUrl = linkElement ? linkElement.href : null;
|
||||
// const itemUrl = linkElement ? linkElement.href : null; // Removed
|
||||
|
||||
if (!title || !priceText || !itemUrl) return;
|
||||
// Try to get image URL, prefer data-src for lazy-loaded images, fallback to src
|
||||
let imageUrl = null;
|
||||
if (imageElement) {
|
||||
imageUrl = imageElement.dataset.src || imageElement.getAttribute('src');
|
||||
}
|
||||
|
||||
const listingPrice = EbayParser.parsePrice(priceText);
|
||||
const parsedInfo = EbayParser.parseSizeAndQuantity(title);
|
||||
|
||||
if (!rawTitle || !priceText) return; // Item ID is now critical, URL was for item ID
|
||||
|
||||
let cleanedTitle = rawTitle;
|
||||
const newListingRegex = /^\s*NEW LISTING\s*[:\-\s]*/i;
|
||||
if (newListingRegex.test(cleanedTitle)) {
|
||||
cleanedTitle = rawTitle.replace(newListingRegex, "").trim();
|
||||
} else if (newListingRegex.test(rawTitle)) {
|
||||
cleanedTitle = rawTitle.replace(newListingRegex, "").trim();
|
||||
}
|
||||
|
||||
const primaryDisplayPrice = EbayParser.parsePrice(priceText);
|
||||
|
||||
let currentBidPrice = null;
|
||||
let finalBuyItNowPrice = null;
|
||||
let hasBestOffer = false;
|
||||
let itemIsAuction = false;
|
||||
|
||||
const bidCountElement = item.querySelector('.s-item__bid-count');
|
||||
if (bidCountElement && bidCountElement.innerText.toLowerCase().includes('bid')) {
|
||||
itemIsAuction = true;
|
||||
}
|
||||
|
||||
const bestOfferElement = item.querySelector('.s-item__purchase-options--bo, .s-item__best-offer');
|
||||
if (bestOfferElement) {
|
||||
hasBestOffer = true;
|
||||
} else {
|
||||
const secondaryInfoElements = item.querySelectorAll('.s-item__subtitle, .s-item__secondary-text, .s-item__detail--secondary');
|
||||
secondaryInfoElements.forEach(el => {
|
||||
if (el.innerText.toLowerCase().includes('or best offer')) {
|
||||
hasBestOffer = true;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (itemIsAuction) {
|
||||
currentBidPrice = primaryDisplayPrice;
|
||||
const auctionBinPriceElement = item.querySelector('.s-item__buy-it-now-price');
|
||||
if (auctionBinPriceElement) {
|
||||
finalBuyItNowPrice = EbayParser.parsePrice(auctionBinPriceElement.innerText);
|
||||
}
|
||||
} else {
|
||||
finalBuyItNowPrice = primaryDisplayPrice;
|
||||
}
|
||||
|
||||
const parsedInfo = EbayParser.parseSizeAndQuantity(cleanedTitle);
|
||||
const totalTB = parsedInfo.totalTB;
|
||||
const quantity = parsedInfo.quantity; // Get quantity
|
||||
const individualSizeTB = parsedInfo.individualSizeTB; // Get individual size
|
||||
const quantity = parsedInfo.quantity;
|
||||
const individualSizeTB = parsedInfo.individualSizeTB;
|
||||
const needed_description_check = parsedInfo.needed_description_check;
|
||||
|
||||
let costPerTB = null;
|
||||
if (listingPrice !== null && totalTB > 0) {
|
||||
costPerTB = listingPrice / totalTB;
|
||||
if (primaryDisplayPrice !== null && totalTB > 0) {
|
||||
costPerTB = primaryDisplayPrice / totalTB;
|
||||
}
|
||||
|
||||
// Extract Item ID from the item's link (still need a link element for this)
|
||||
let itemId = null;
|
||||
const itemMatch = itemUrl.match(/\/itm\/(\d+)/);
|
||||
if (itemMatch && itemMatch[1]) {
|
||||
itemId = itemMatch[1];
|
||||
const linkForIdElement = item.querySelector('a.s-item__link[href*="/itm/"], .s-item__info > a[href*="/itm/"]');
|
||||
if (linkForIdElement && linkForIdElement.href) {
|
||||
const itemMatch = linkForIdElement.href.match(/\/itm\/(\d+)/);
|
||||
if (itemMatch && itemMatch[1]) {
|
||||
itemId = itemMatch[1];
|
||||
}
|
||||
}
|
||||
if(!itemId) return; // Skip if no item ID can be found, as it's crucial
|
||||
|
||||
items.push({
|
||||
title,
|
||||
itemId,
|
||||
title: cleanedTitle,
|
||||
itemId: itemId, // Crucial
|
||||
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
|
||||
currentBidPrice: currentBidPrice,
|
||||
buyItNowPrice: finalBuyItNowPrice,
|
||||
hasBestOffer: hasBestOffer,
|
||||
image_url: imageUrl, // <-- Added
|
||||
parsed: { // <-- Nested object
|
||||
itemCount: quantity,
|
||||
sizePerItemTB: individualSizeTB > 0 ? parseFloat(individualSizeTB.toFixed(3)) : null,
|
||||
totalTB: totalTB > 0 ? parseFloat(totalTB.toFixed(3)) : null,
|
||||
costPerTB: costPerTB !== null ? parseFloat(costPerTB.toFixed(2)) : null,
|
||||
needed_description_check: needed_description_check,
|
||||
parser_engine: PARSER_ENGINE_VERSION // <-- Added
|
||||
}
|
||||
// itemUrl: itemUrl, // <-- Removed
|
||||
});
|
||||
});
|
||||
return items;
|
||||
|
Reference in New Issue
Block a user