← back to Handbag Authentication

crawler/prada-specialist.js

159 lines

const puppeteer = require('puppeteer');

class PradaSpecialistScraper {
  constructor() {
    this.baseUrl = 'https://auctions.yahoo.co.jp/search/search';

    // Specific Prada bag models with Japanese names
    this.pradaModels = [
      'プラダ ガレリア',              // Galleria
      'プラダ カナパ',                // Canapa
      'プラダ サフィアーノ',          // Saffiano
      'プラダ ナイロン',              // Nylon
      'プラダ カハイエ',              // Cahier
      'プラダ シビル',                // Sidonie
      'プラダ トート',                // Tote
      'プラダ 2way',
      'プラダ ショルダーバッグ',      // Shoulder Bag
      'Prada Galleria',
      'Prada Saffiano',
      'Prada Cahier',
      'Prada Nylon'
    ];

    this.maxPages = 10;
  }

  async scrapeSearch(model, page = 1) {
    try {
      const browser = await puppeteer.launch({
        headless: 'new',
        args: ['--no-sandbox', '--disable-setuid-sandbox']
      });

      const browserPage = await browser.newPage();
      await browserPage.setUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36');

      const searchUrl = `${this.baseUrl}?p=${encodeURIComponent(model)}&b=${(page - 1) * 50 + 1}`;
      console.log(`Scraping Prada specialist: ${searchUrl}`);

      await browserPage.goto(searchUrl, { waitUntil: 'networkidle2', timeout: 30000 });
      await browserPage.waitForSelector('.Product, .Product__item', { timeout: 10000 }).catch(() => {});

      const listings = await browserPage.evaluate(() => {
        const items = [];
        const productElements = document.querySelectorAll('.Product, .Product__item');

        productElements.forEach((item) => {
          try {
            const titleEl = item.querySelector('.Product__title, a');
            const title = titleEl?.textContent?.trim() || '';

            const priceEl = item.querySelector('.Product__price, .u-tax');
            const priceText = priceEl?.textContent?.trim() || '';
            const priceMatch = priceText.match(/[\d,]+/);
            const price = priceMatch ? parseInt(priceMatch[0].replace(/,/g, '')) : 0;

            const imgEl = item.querySelector('img');
            const image = imgEl?.src || '';

            const linkEl = item.querySelector('a');
            const url = linkEl?.href || '';

            const idMatch = url.match(/\/([a-z0-9]+)$/);
            const id = idMatch?.[1] || url;

            const timeEl = item.querySelector('.Product__time');
            const endTime = timeEl?.textContent?.trim() || '';

            // Extract size
            const sizeMatch = title.match(/(\d+)\s*cm|ミニ|Mini|Small|Medium|Large/i);
            const size = sizeMatch ? sizeMatch[0] : '';

            // Extract material
            let material = '';
            if (title.match(/サフィアーノ|Saffiano/i)) material = 'Saffiano';
            else if (title.match(/ナイロン|Nylon/i)) material = 'Nylon';
            else if (title.match(/レザー|Leather/i)) material = 'Leather';
            else if (title.match(/キャンバス|Canvas/i)) material = 'Canvas';

            // Extract color
            const colorMatch = title.match(/(ブラック|ホワイト|レッド|ブルー|グリーン|ピンク|ベージュ|ブラウン|ネイビー|Black|White|Red|Blue|Green|Pink|Beige|Brown|Navy)/i);
            const color = colorMatch ? colorMatch[1] : '';

            // Detect condition
            let condition = 'Used';
            if (title.match(/未使用|新品|UNUSED|NEW/i)) {
              condition = 'New/Unused';
            } else if (title.match(/美品|極美品|EXCELLENT/i)) {
              condition = 'Excellent';
            } else if (title.match(/良品|GOOD/i)) {
              condition = 'Good';
            } else if (title.match(/難あり|ジャンク|JUNK|DAMAGED/i)) {
              condition = 'Fair/Damaged';
            }

            if (title && price > 0) {
              items.push({
                external_id: id,
                title,
                price_jpy: price,
                image_url: image,
                thumbnail_url: image,
                product_url: url,
                listing_type: 'auction',
                auction_end_date: endTime,
                condition,
                size,
                color,
                material
              });
            }
          } catch (e) {
            console.error('Error parsing Prada item:', e);
          }
        });

        return items;
      });

      await browser.close();
      return listings;

    } catch (error) {
      console.error(`Error scraping Prada specialist:`, error.message);
      return [];
    }
  }

  async scrapeAllModels() {
    const allListings = [];

    for (const model of this.pradaModels) {
      console.log(`\nScraping Prada specialist for ${model}...`);

      for (let page = 1; page <= this.maxPages; page++) {
        const listings = await this.scrapeSearch(model, page);

        if (listings.length === 0) break;

        listings.forEach(listing => {
          listing.brand = 'Prada';
          listing.source = 'yahoo_prada_specialist';
          listing.model = model;
        });

        allListings.push(...listings);
        console.log(`Found ${listings.length} Prada items on page ${page}`);

        await new Promise(resolve => setTimeout(resolve, 3500));
      }
    }

    console.log(`\n✨ Prada specialist crawl complete: ${allListings.length} total items`);
    return allListings;
  }
}

module.exports = PradaSpecialistScraper;