import Review from '../models/Review.js'
import cheerio from "cheerio";
import jschardet from 'jschardet'
import fs from 'fs'
import axios from 'axios';

const search = async (req, res) => {
    const url = "https://www.google.com/search?q=" + encodeURI(req.params.search) + "+site%3Atistory.com&page_no=1"
    const editUrl = /(http(s)?:\/\/)([a-z0-9\w]+\.*)+[a-z0-9]{2,4}/gi
    axios.get(url)
        .then((response) => {
            const html = response.data
            fs.writeFileSync("googlez.txt", html, { encoding: 'utf8' });
            let $1 = cheerio.load(html);
            let reviews = []
            $1('.kCrYT').each(function (i) {

                if ($1(this).find('h3').text()) {
                    reviews[i] = { title: $1(this).find('h3').text(), link: ($1(this).find('a').attr('href')) }
                } else if ($1(this).find('.s3v9rd').find('.s3v9rd').text()) {
                    reviews[i - 1] = { ...reviews[i - 1], summary: $1(this).find('.s3v9rd').find('.s3v9rd').text() }
                    reviews = reviews.filter(e => e)
                }
            })

            console.log("*******************************************",reviews)
            res.send(reviews)
        })
}


export default { search }
