← back to Dw Cowtan Recrawl

probe5.js

38 lines

#!/usr/bin/env node
// Verify the NEW cowtan API: /api/Product/Detail/<code> and /api/ProductSearch
const https = require('https');
function req(method, path, body) {
  return new Promise((resolve, reject) => {
    const opts = { hostname: 'designs.cowtan.com', port: 443, path, method, timeout: 30000,
      headers: { 'User-Agent': 'Mozilla/5.0', 'Accept': 'application/json, */*', 'X-Requested-With': 'XMLHttpRequest' } };
    if (body) { opts.headers['Content-Type'] = 'application/json'; opts.headers['Content-Length'] = Buffer.byteLength(body); }
    const r = https.request(opts, res => { let d = ''; res.on('data', c => d += c); res.on('end', () => resolve({ status: res.statusCode, ct: res.headers['content-type'], body: d })); });
    r.on('error', reject); r.on('timeout', () => { r.destroy(); reject(new Error('timeout')); });
    if (body) r.write(body); r.end();
  });
}
function imgUrls(obj) {
  const out = new Set(); JSON.stringify(obj).replace(/https?:\/\/[^"'\\\s]+\.(?:jpg|jpeg|png|webp)/gi, m => { out.add(m); return m; });
  return [...out];
}
(async () => {
  for (const code of ['1020-01', '20527-01']) {
    const r = await req('GET', '/api/Product/Detail/' + encodeURIComponent(code));
    console.log('\n=== GET /api/Product/Detail/' + code + ' ->', r.status, (r.ct || '').split(';')[0], 'len=' + r.body.length);
    if (r.status == 200 && /json/.test(r.ct || '')) {
      let j; try { j = JSON.parse(r.body); } catch (e) { console.log('  parse err', e.message); continue; }
      console.log('  top keys:', Object.keys(j).join(', '));
      const imgs = imgUrls(j);
      console.log('  image urls found:', imgs.length);
      imgs.slice(0, 20).forEach(u => console.log('    ', u));
    } else { console.log('  body head:', r.body.slice(0, 200).replace(/\n/g, ' ')); }
  }
  // ProductSearch POST
  for (const body of [JSON.stringify({ Keywords: '', PageIndex: 0 }), 'Keywords=&PageIndex=0']) {
    const isJson = body.startsWith('{');
    const r = await req('POST', '/api/ProductSearch', body);
    console.log('\n=== POST /api/ProductSearch (' + (isJson ? 'json' : 'form') + ') ->', r.status, (r.ct || '').split(';')[0], 'len=' + r.body.length);
    console.log('  head:', r.body.slice(0, 240).replace(/\n/g, ' '));
  }
})().catch(e => console.error('FATAL', e.message));