← back to Handbag Authentication

crawler/lv-specialist.js

167 lines

const puppeteer = require('puppeteer');

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

    // Specific Louis Vuitton bag models with Japanese names
    this.lvModels = [
      'ルイヴィトン ネヴァーフル',        // Neverfull
      'ルイヴィトン スピーディ',          // Speedy
      'ルイヴィトン アルマ',              // Alma
      'ルイヴィトン カプシーヌ',          // Capucines
      'ルイヴィトン ポシェット',          // Pochette
      'ルイヴィトン オンザゴー',          // OnTheGo
      'ルイヴィトン ツイスト',            // Twist
      'ルイヴィトン メティス',            // Metis
      'ルイヴィトン ドーフィーヌ',        // Dauphine
      'ルイヴィトン サックプラ',          // Sac Plat
      'ルイヴィトン ノエ',                // Noe
      'ルイヴィトン モンテーニュ',        // Montaigne
      'ルイヴィトン ポンヌフ',            // Pont Neuf
      'ルイヴィトン チュレン',            // Tuileries
      'Louis Vuitton Neverfull',
      'Louis Vuitton Speedy',
      'Louis Vuitton Alma',
      'Louis Vuitton Capucines'
    ];

    this.maxPages = 12; // Many pages for popular LV bags
  }

  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 LV 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 info
            const sizeMatch = title.match(/(\d+)\s*cm|PM|MM|GM/i);
            const size = sizeMatch ? sizeMatch[0] : '';

            // Extract pattern/canvas type
            let pattern = '';
            if (title.match(/モノグラム|Monogram/i)) pattern = 'Monogram';
            else if (title.match(/ダミエ|Damier/i)) pattern = 'Damier';
            else if (title.match(/エピ|Epi/i)) pattern = 'Epi';
            else if (title.match(/タイガ|Taiga/i)) pattern = 'Taiga';
            else if (title.match(/ヴェルニ|Vernis/i)) pattern = 'Vernis';
            else if (title.match(/マヒナ|Mahina/i)) pattern = 'Mahina';
            else if (title.match(/エンプレント|Empreinte/i)) pattern = 'Empreinte';

            // Extract color
            const colorMatch = title.match(/(ブラック|ホワイト|レッド|ブルー|グリーン|ピンク|ベージュ|ブラウン|Black|White|Red|Blue|Green|Pink|Beige|Brown|Noir|Rouge)/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: pattern
              });
            }
          } catch (e) {
            console.error('Error parsing LV item:', e);
          }
        });

        return items;
      });

      await browser.close();
      return listings;

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

  async scrapeAllModels() {
    const allListings = [];

    for (const model of this.lvModels) {
      console.log(`\nScraping LV 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 = 'Louis Vuitton';
          listing.source = 'yahoo_lv_specialist';
          listing.model = model;
        });

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

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

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

module.exports = LouisVuittonSpecialistScraper;