import express from 'express';
import fetch from 'node-fetch';
import axios from 'axios';
import * as cheerio from 'cheerio';
const app = express();
const port = 3000;

app.use((req, res, next) => {
  res.header('Access-Control-Allow-Origin', '*');
  res.header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
  res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization');
  next();
});

// 通用代理路由
app.get('/proxy', async (req, res) => {
  const targetUrl = req.query.url; // 从查询参数中获取URL
  if (!targetUrl) {
    return res.status(400).json({ error: 'Missing URL parameter' });
  }
  try {
    console.log(`Fetching URL: ${targetUrl}`); // 打印日志信息
    const response = await fetch(targetUrl, {
      method: 'GET',
      headers: {
        // 您可能需要转发一些特定的请求头
        'User-Agent': req.headers['User-Agent'], // 例如，转发User-Agent头
        'Referer': req.headers['Referer'], // 例如，转发Referer头
      },
    });

    const text = await response.text(); // 获取文本内容
    console.log(text); // 输出文本内容以便调试（可选）

    res.status(response.status).json(await response.json());
  } catch (error) {
    console.error('Error fetching target URL:', error); // 打印错误信息
    res.status(500).json({ error: 'Failed to fetch target URL', details: error.message });
  }
});

//小红书代理路由
app.get('/scrape', async (req, res) => {
  const keyword = req.query.keyword;
  const url = `https://www.xiaohongshu.com/search_result?keyword=${keyword}`;

  try {
    const response = await axios.get(url);
    console.log(response.data);
    const $ = cheerio.load(response.data);
    const posts = [];

    $('div.post-class').each((index, element) => {
      const title = $(element).find('h2').text();
      const link = $(element).find('a').attr('href');
      posts.push({ title, link });
      console.log(title, link);
    });

    res.json(posts);
  } catch (error) {
    res.status(500).send('Error occurred while scraping');
  }
});


app.listen(port, () => {
  console.log(`Proxy server is running on http://localhost:${port}`);
});