← back to Dw Cowtan Recrawl

probe.js

35 lines

#!/usr/bin/env node
const https = require('https');
function get(u, m = 4) {
  return new Promise((res, rej) => {
    https.get(u, { timeout: 25000, headers: { 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15) AppleWebKit/537.36 Chrome/120 Safari/537.36', 'Accept': 'text/html' } }, r => {
      if ((r.statusCode == 301 || r.statusCode == 302) && r.headers.location && m > 0) {
        r.resume();
        const loc = r.headers.location.startsWith('http') ? r.headers.location : 'https://designs.cowtan.com' + r.headers.location;
        return get(loc, m - 1).then(res).catch(rej);
      }
      if (r.statusCode != 200) { r.resume(); return rej(new Error('HTTP ' + r.statusCode)); }
      let d = ''; r.on('data', c => d += c); r.on('end', () => res(d));
    }).on('error', rej).on('timeout', function () { this.destroy(); rej(new Error('timeout')); });
  });
}
(async () => {
  const urls = ['https://designs.cowtan.com/Design/1020-01', 'https://designs.cowtan.com/Design/20527-01'];
  for (const url of urls) {
    console.log('\n==== ' + url + ' ====');
    try {
      const html = await get(url);
      console.log('HTML len:', html.length);
      const imgs = [...html.matchAll(/(https:\/\/d2mq91o692rj7w\.cloudfront\.net\/[^"'\s)]+)/g)].map(m => m[1]);
      const uniq = [...new Set(imgs)];
      console.log('distinct cloudfront img urls:', uniq.length);
      uniq.slice(0, 30).forEach(u => console.log('   ', u));
      console.log('has .colour-options:', /colour-options/.test(html));
      const co = [...new Set([...html.matchAll(/data-stockcode="([^"]+)"/g)].map(m => m[1]))];
      console.log('colorway stockcodes:', co.length, co.slice(0, 12).join(', '));
      const cm = html.match(/Color:\s*([a-zA-Z][a-zA-Z\s,&]{1,30})/i);
      console.log('Color: match ->', cm ? cm[1].trim() : '(none)');
    } catch (e) { console.error('ERR', e.message); }
  }
})();