New scrapers (8): - BlueOptics (EUR, every 4h) - ShopFiber24 (EUR, every 4h) - T&S Communication (USD, every 4h) - SmartOptics (catalog, every 8h) - HUBER+SUHNER (catalog, every 8h) - Skylane Optics (USD, every 4h) - AscentOptics (USD, every 4h) - GAO Tek (USD, every 4h) Scheduler: nightly window → 24/7 continuous (42 jobs total) - Playwright scrapers: every 8h (FS.com, 10Gtek, ATGBICS, ProLabs) - Fetch/Cheerio: every 4h (11 lightweight vendors) - Flexoptix catalog: every 2h (primary price source) - eBay enrichment: every 6h - Compatibility matrices: every 12h - Compute jobs: every 4h Pi fleet: scripts/pi-scraper-setup.sh for one-command Pi node setup
261 lines
9.1 KiB
TypeScript
261 lines
9.1 KiB
TypeScript
/**
|
|
* T&S Communication Scraper — Chinese compatible transceiver manufacturer
|
|
*
|
|
* www.china-tscom.com — product catalog with USD prices.
|
|
* Paginated: /products/fiber-optic-transceivers/?page=N
|
|
*
|
|
* Rate limited: 1 req/2sec.
|
|
*/
|
|
import { pool, findOrCreateScrapedTransceiver, ensureVendor, upsertPriceObservation } from "../utils/db";
|
|
import { contentHash } from "../utils/hash";
|
|
import * as cheerio from "cheerio";
|
|
|
|
const BASE = "https://www.china-tscom.com";
|
|
const CATALOG_PATH = "/products/fiber-optic-transceivers/";
|
|
const MAX_PAGES = 15;
|
|
const HEADERS = {
|
|
"User-Agent": "Mozilla/5.0 (compatible; TIP-Bot/1.0; research)",
|
|
Accept: "text/html,application/xhtml+xml",
|
|
"Accept-Language": "en-US,en;q=0.9",
|
|
};
|
|
|
|
interface Product {
|
|
partNumber: string;
|
|
name: string;
|
|
url: string;
|
|
price?: number;
|
|
formFactor: string;
|
|
speed: string;
|
|
speedGbps: number;
|
|
reachLabel?: string;
|
|
reachMeters?: number;
|
|
fiberType?: string;
|
|
wavelength?: string;
|
|
}
|
|
|
|
function sleep(ms: number): Promise<void> {
|
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
}
|
|
|
|
function detectFormFactor(text: string): { formFactor: string; speed: string; speedGbps: number } {
|
|
const lower = text.toLowerCase();
|
|
if (lower.includes("osfp") && !lower.includes("qsfp")) return { formFactor: "OSFP", speed: "400G", speedGbps: 400 };
|
|
if (lower.includes("qsfp-dd")) return { formFactor: "QSFP-DD", speed: "400G", speedGbps: 400 };
|
|
if (lower.includes("qsfp28")) return { formFactor: "QSFP28", speed: "100G", speedGbps: 100 };
|
|
if (lower.includes("qsfp+") || lower.includes("qsfp plus")) return { formFactor: "QSFP+", speed: "40G", speedGbps: 40 };
|
|
if (lower.includes("sfp56")) return { formFactor: "SFP56", speed: "50G", speedGbps: 50 };
|
|
if (lower.includes("sfp28") || lower.includes("25g")) return { formFactor: "SFP28", speed: "25G", speedGbps: 25 };
|
|
if (lower.includes("sfp+") || lower.includes("10gbase") || lower.includes("10g")) return { formFactor: "SFP+", speed: "10G", speedGbps: 10 };
|
|
if (lower.includes("xfp")) return { formFactor: "XFP", speed: "10G", speedGbps: 10 };
|
|
if (lower.includes("1000base") || lower.includes("1g")) return { formFactor: "SFP", speed: "1G", speedGbps: 1 };
|
|
if (lower.includes("sfp") && !lower.includes("qsfp")) return { formFactor: "SFP", speed: "1G", speedGbps: 1 };
|
|
return { formFactor: "SFP+", speed: "10G", speedGbps: 10 };
|
|
}
|
|
|
|
function detectReach(text: string): { label: string; meters: number } | undefined {
|
|
const patterns: [RegExp, string, number][] = [
|
|
[/\b80\s*km\b/i, "80km", 80000],
|
|
[/\b40\s*km\b/i, "40km", 40000],
|
|
[/\b20\s*km\b/i, "20km", 20000],
|
|
[/\b10\s*km\b/i, "10km", 10000],
|
|
[/\b2\s*km\b/i, "2km", 2000],
|
|
[/\b550\s*m\b/i, "550m", 550],
|
|
[/\b300\s*m\b/i, "300m", 300],
|
|
[/\b100\s*m\b/i, "100m", 100],
|
|
[/\bLR4\b/, "10km", 10000], [/\bLR\b/, "10km", 10000],
|
|
[/\bER4?\b/, "40km", 40000], [/\bZR4?\b/, "80km", 80000],
|
|
[/\bSR4?\b/, "300m", 300], [/\bDR4?\b/, "500m", 500], [/\bFR4?\b/, "2km", 2000],
|
|
];
|
|
for (const [regex, label, meters] of patterns) {
|
|
if (regex.test(text)) return { label, meters };
|
|
}
|
|
return undefined;
|
|
}
|
|
|
|
function detectFiber(text: string): string {
|
|
if (/single.?mode|smf|[^a-z]lx[^a-z]|[^a-z]lr[^a-z]|[^a-z]er[^a-z]|[^a-z]zr[^a-z]|bidi|cwdm|dwdm/i.test(text)) return "SMF";
|
|
if (/multi.?mode|mmf|[^a-z]sx[^a-z]|[^a-z]sr[^a-z]/i.test(text)) return "MMF";
|
|
if (/copper|dac|twinax|rj45|base-t/i.test(text)) return "Copper";
|
|
return "SMF";
|
|
}
|
|
|
|
function detectWavelength(text: string): string {
|
|
const match = text.match(/(\d{3,4})\s*nm/i);
|
|
return match ? match[1] : "";
|
|
}
|
|
|
|
function parseProductList(html: string): Product[] {
|
|
const $ = cheerio.load(html);
|
|
const products: Product[] = [];
|
|
|
|
// Try common product grid/list selectors
|
|
const cardSelectors = [
|
|
".product-item", ".product-card", ".item", "li.product",
|
|
"article", ".product", ".col-product", ".entry",
|
|
];
|
|
|
|
let found = false;
|
|
for (const sel of cardSelectors) {
|
|
if ($(sel).length >= 2) {
|
|
$(sel).each((_i, el) => {
|
|
const nameEl = $(el).find("h2, h3, h4, .product-name, .entry-title, a").first();
|
|
const name = nameEl.text().trim();
|
|
if (!name || name.length < 5 || !/sfp|qsfp|xfp|transceiver|optic/i.test(name)) return;
|
|
|
|
const linkEl = $(el).find("a[href]").first();
|
|
const href = linkEl.attr("href") || "";
|
|
const url = href.startsWith("http") ? href : BASE + href;
|
|
|
|
// USD price patterns
|
|
const priceText = $(el).find(".price, .product-price, .amount, [data-price]").text();
|
|
const priceMatch = priceText.match(/\$\s*([\d,]+\.?\d{0,2})/) ||
|
|
priceText.match(/([\d,]+\.?\d{0,2})\s*USD/i);
|
|
let price: number | undefined;
|
|
if (priceMatch) {
|
|
const parsed = parseFloat(priceMatch[1].replace(",", ""));
|
|
if (parsed > 0 && parsed < 50000) price = parsed;
|
|
}
|
|
|
|
const skuText = $(el).find(".sku, [data-sku], .model, .part-number").text().trim();
|
|
const partNumber = skuText ||
|
|
name.match(/[A-Z0-9][-A-Z0-9]{5,}/)?.[0] ||
|
|
name.replace(/\s+/g, "-").slice(0, 60);
|
|
|
|
const ff = detectFormFactor(name);
|
|
const reach = detectReach(name);
|
|
|
|
products.push({
|
|
partNumber,
|
|
name,
|
|
url,
|
|
price,
|
|
...ff,
|
|
reachLabel: reach?.label,
|
|
reachMeters: reach?.meters,
|
|
fiberType: detectFiber(name),
|
|
wavelength: detectWavelength(name),
|
|
});
|
|
});
|
|
if (products.length > 0) { found = true; break; }
|
|
}
|
|
}
|
|
|
|
// Fallback: anchor-based extraction
|
|
if (!found) {
|
|
$("a[href]").each((_i, el) => {
|
|
const name = $(el).text().trim();
|
|
const href = $(el).attr("href") || "";
|
|
if (name.length < 8 || name.length > 200 || !/sfp|qsfp|transceiver/i.test(name)) return;
|
|
const url = href.startsWith("http") ? href : BASE + href;
|
|
const ff = detectFormFactor(name);
|
|
const reach = detectReach(name);
|
|
products.push({
|
|
partNumber: name.match(/[A-Z0-9][-A-Z0-9]{5,}/)?.[0] || name.replace(/\s+/g, "-").slice(0, 60),
|
|
name, url, ...ff,
|
|
reachLabel: reach?.label, reachMeters: reach?.meters,
|
|
fiberType: detectFiber(name), wavelength: detectWavelength(name),
|
|
});
|
|
});
|
|
}
|
|
|
|
const seen = new Set<string>();
|
|
return products.filter((p) => {
|
|
if (!p.url || seen.has(p.url)) return false;
|
|
seen.add(p.url);
|
|
return true;
|
|
});
|
|
}
|
|
|
|
async function fetchPage(url: string): Promise<string> {
|
|
const resp = await fetch(url, { headers: HEADERS, signal: AbortSignal.timeout(30000) });
|
|
if (!resp.ok) throw new Error(`HTTP ${resp.status} for ${url}`);
|
|
return resp.text();
|
|
}
|
|
|
|
export async function scrapeTsCom(): Promise<void> {
|
|
console.log("=== T&S Communication Scraper Starting ===\n");
|
|
|
|
const vendorId = await ensureVendor(
|
|
"T&S Communication",
|
|
"compatible",
|
|
"https://www.china-tscom.com",
|
|
BASE + CATALOG_PATH,
|
|
);
|
|
|
|
let allProducts: Product[] = [];
|
|
|
|
for (let page = 1; page <= MAX_PAGES; page++) {
|
|
try {
|
|
const url = page === 1
|
|
? BASE + CATALOG_PATH
|
|
: `${BASE}${CATALOG_PATH}?page=${page}`;
|
|
const html = await fetchPage(url);
|
|
const pageProducts = parseProductList(html);
|
|
allProducts.push(...pageProducts);
|
|
console.log(` Page ${page}: ${pageProducts.length} products`);
|
|
if (pageProducts.length === 0) {
|
|
console.log(` Empty page ${page}, stopping pagination.`);
|
|
break;
|
|
}
|
|
if (page < MAX_PAGES) await sleep(2000);
|
|
} catch (err) {
|
|
console.warn(` Page ${page} failed: ${(err as Error).message}`);
|
|
break;
|
|
}
|
|
}
|
|
|
|
const seen = new Set<string>();
|
|
allProducts = allProducts.filter((p) => {
|
|
if (seen.has(p.url)) return false;
|
|
seen.add(p.url);
|
|
return true;
|
|
});
|
|
|
|
console.log(`\nTotal unique products: ${allProducts.length}`);
|
|
|
|
let totalProducts = 0;
|
|
let priceUpdates = 0;
|
|
|
|
for (const product of allProducts) {
|
|
try {
|
|
const txId = await findOrCreateScrapedTransceiver({
|
|
partNumber: product.partNumber,
|
|
vendorId,
|
|
formFactor: product.formFactor,
|
|
speedGbps: product.speedGbps,
|
|
speed: product.speed,
|
|
reachMeters: product.reachMeters,
|
|
reachLabel: product.reachLabel,
|
|
fiberType: product.fiberType,
|
|
wavelengths: product.wavelength,
|
|
category: "DataCenter",
|
|
});
|
|
|
|
if (product.price && product.price > 0) {
|
|
const hash = contentHash(JSON.stringify({ price: product.price, part: product.partNumber }));
|
|
const updated = await upsertPriceObservation({
|
|
transceiverId: txId,
|
|
sourceVendorId: vendorId,
|
|
price: product.price,
|
|
currency: "USD",
|
|
stockLevel: "in_stock",
|
|
url: product.url,
|
|
contentHash: hash,
|
|
});
|
|
if (updated) priceUpdates++;
|
|
}
|
|
totalProducts++;
|
|
} catch (err) {
|
|
console.warn(` Error saving ${product.partNumber}: ${(err as Error).message.slice(0, 80)}`);
|
|
}
|
|
}
|
|
|
|
console.log(`\n=== T&S Communication Complete: ${totalProducts} products, ${priceUpdates} prices ===`);
|
|
}
|
|
|
|
if (require.main === module) {
|
|
scrapeTsCom()
|
|
.then(() => pool.end())
|
|
.catch((err) => { console.error("Fatal:", err); pool.end(); process.exit(1); });
|
|
}
|