← back to Handbag Authentication

api/rebag-clair-api.js

102 lines

const axios = require('axios');

/**
 * Rebag Clair API Integration
 * Rebag has a public pricing tool called "Clair" that gives instant valuations
 * This can be used as a price database for historical resale values
 */
class RebagClairAPI {
  constructor() {
    this.baseUrl = 'https://www.rebag.com';
  }

  /**
   * Get estimated resale value from Rebag Clair
   */
  async getEstimatedValue(brand, model, condition = 'excellent') {
    try {
      // Rebag Clair search
      const searchUrl = `${this.baseUrl}/shop?q=${encodeURIComponent(brand + ' ' + model)}`;

      const response = await axios.get(searchUrl, {
        headers: {
          'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
        },
        timeout: 15000
      });

      // Parse response for pricing data
      const priceMatches = response.data.match(/"price":\s*(\d+)/g);
      const prices = priceMatches ? priceMatches.map(m => parseInt(m.match(/\d+/)[0])) : [];

      if (prices.length === 0) {
        console.log(`No Rebag pricing found for ${brand} ${model}`);
        return null;
      }

      // Calculate average, high, low
      const avgPrice = Math.round(prices.reduce((a, b) => a + b, 0) / prices.length);
      const highPrice = Math.max(...prices);
      const lowPrice = Math.min(...prices);

      return {
        source: 'rebag_clair',
        brand,
        model,
        condition,
        avgPrice,
        highPrice,
        lowPrice,
        sampleSize: prices.length,
        lastUpdated: new Date().toISOString()
      };

    } catch (error) {
      console.error(`Error fetching Rebag Clair data:`, error.message);
      return null;
    }
  }

  /**
   * Get market trends from Rebag
   */
  async getMarketTrends(brand) {
    try {
      const url = `${this.baseUrl}/shop?q=${encodeURIComponent(brand)}`;

      const response = await axios.get(url, {
        headers: {
          'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
        },
        timeout: 15000
      });

      // Extract trending models and prices
      const modelMatches = response.data.match(/"title":"([^"]+)"/g);
      const priceMatches = response.data.match(/"price":(\d+)/g);

      const trending = [];
      if (modelMatches && priceMatches) {
        for (let i = 0; i < Math.min(modelMatches.length, priceMatches.length, 10); i++) {
          const model = modelMatches[i].match(/"title":"([^"]+)"/)[1];
          const price = parseInt(priceMatches[i].match(/\d+/)[0]);

          trending.push({ model, price });
        }
      }

      return {
        brand,
        trending,
        lastUpdated: new Date().toISOString()
      };

    } catch (error) {
      console.error(`Error fetching Rebag trends:`, error.message);
      return null;
    }
  }
}

module.exports = RebagClairAPI;