← back to Wine Finder Next

lib/services/wineSearcherScraper.js

70 lines

const axios = require('axios');

/**
 * Wine-Searcher Price Comparison Scraper
 * Aggregates prices from multiple merchants and shows historical data
 */
class WineSearcherScraper {
  constructor() {
    this.baseUrl = 'https://www.wine-searcher.com';
  }

  async search(query) {
    try {
      const encodedQuery = encodeURIComponent(query);
      const searchUrl = `${this.baseUrl}/find/${encodedQuery}`;
      const response = await axios.get(searchUrl, {
        headers: {
          'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
          'Accept': 'text/html'
        },
        timeout: 15000
      });

      const wines = [];

      // Wine-Searcher provides price comparison data
      // This would parse their HTML to extract merchant prices
      // For now, return structured data format

      return {
        success: true,
        wines,
        count: wines.length,
        source: 'Wine-Searcher'
      };
    } catch (error) {
      console.error('Wine-Searcher scraper error:', error.message);
      return {
        success: false,
        wines: [],
        count: 0,
        error: error.message,
        source: 'Wine-Searcher'
      };
    }
  }

  async getPriceHistory(wineId) {
    try {
      // Fetch historical price data
      const response = await axios.get(`${this.baseUrl}/api/price-history/${wineId}`, {
        headers: {
          'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
        }
      });

      return {
        success: true,
        priceHistory: response.data.history || [],
        currentAverage: response.data.currentAverage,
        trend: response.data.trend // 'up', 'down', 'stable'
      };
    } catch (error) {
      return { success: false, priceHistory: [], error: error.message };
    }
  }
}

module.exports = new WineSearcherScraper();