import Review from '../models/Review.js' import cheerio from "cheerio"; import axios from 'axios'; const search = async (req, res) => { try { let reviews = [] console.log(req.reviews, "제발") let url = "https://www.google.com/search?q=" + encodeURI(req.query.keyword) + `+site%3Atistory.com&start=${Number(req.query.index) * 10}` console.log(url) let response1 = await axios.get(url) let $1 = cheerio.load(response1.data); $1('.kCrYT').each(async function (i) { console.log("3") let title = $1(this).find('h3').text() let searchParams = new URLSearchParams($1(this).find('a').attr('href')); let link = searchParams.get("/url?q") let summary = $1(this).find('.s3v9rd').find('.s3v9rd').text() if (title) { reviews[i] = { title: title, link: link } } else if (summary) { reviews[i - 1] = { ...reviews[i - 1], summary: summary } reviews = reviews.filter(e => e) } }) // reviews.forEach(async (review, i) => { // await Review.updateOne({ link: review.link }, { $push: { bookmark: req.query.keyword } }) // const reviewss = await Review.findOne({ link: review.link }) // if (!reviewss) { // // reviews["keyword"] = [req.query.keyword] let newReviews = [] newReviews = [...newReviews, ...reviews] // await new Review(review).save() // } // }) // console.log(req.reviews, "걸러낸 리뷰즈입니다") // const a = await Promise.all(newReviews) // console.log(a, "2") console.log(req.query.index, "인덱스ㅡㅡ") res.status(200).send({ review: newReviews, db: true, index: Number(req.query.index) + 1 }) } catch (error) { console.log("6") console.log(error) } } const find = async(req, res, next) => { console.log("7", req.query) const keyword = req.query.keyword if (req.body.db) { next() } await Review.find({ keyword: keyword }, function (err, reviews) { req.reviews = [] if (reviews) { if (reviews.length > 5) { res.status(200).send({ review: reviews, db: true, message: "제발용" }) } req.reviews = reviews } next() }) } export default { search, find }