review.controller.js 2.59 KB
Newer Older
1
2
import Review from '../models/Review.js'
import cheerio from "cheerio";
Kim, Chaerin's avatar
서연    
Kim, Chaerin committed
3
import axios from 'axios';
4

Kim, Chaerin's avatar
Kim, Chaerin committed
5
const search = async (req, res, next) => {
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
6
    console.log(req.reviews, "1")
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
7
8
    try {
        for (let j = 1; j < 2; j++) {
Kim, Chaerin's avatar
Kim, Chaerin committed
9
            let reviews = []
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
10
11
12
13
            // console.log(req.reviews, "제발")/
            let url = "https://www.google.com/search?q=" + encodeURI(req.query.keyword) + "+site%3Atistory.com&page_no=" + j
            let response1 = await axios.get(url)
            let $1 = cheerio.load(response1.data);
Kim, Chaerin's avatar
Kim, Chaerin committed
14
15
            $1('.kCrYT').each(async function (i) {
                console.log("3")
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
                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((review, i) => {
                Review.findOne({ link: review.link }, async function (err, reviewss) {
                    console.log("4")

                    if (err) { return res.status(404).send({ error: err }) }
                    if (reviewss) {
                        reviewss.keyword = [...reviewss.keyword, req.query.keyword]
                        reviewss.save()
                    }
                    if (!reviewss) {
                        reviews["keyword"] = [req.query.keyword]
                        let Reviews = await new Review(review).save()
                        // console.log(Reviews,"1")
                        req.reviews.push(Reviews)
Kim, Chaerin's avatar
Kim, Chaerin committed
41
42
                    }
                })
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
43

Kim, Chaerin's avatar
Kim, Chaerin committed
44
45
            })
        }
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
46
47
48
49
50
51
        const a = await Promise.all(req.reviews)
        console.log(a, "2")
        res.status(200).send({ review: a, db: true })
    } catch (error) {
        console.log("6")
        console.log(error)
52
53
54
    }
}

Kim, Chaerin's avatar
?    
Kim, Chaerin committed
55

Kim, Chaerin's avatar
Kim, Chaerin committed
56
const find = (req, res, next) => {
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
57
58
    console.log("7", req.query.keyword)
    const keyword = req.query.keyword
Kim, Chaerin's avatar
Kim, Chaerin committed
59
60
61
    if (req.body.db) {
        next()
    }
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
62
63
    Review.find({ keyword: keyword }, function (err, reviews) {
        req.reviews = []
Kim, Chaerin's avatar
Kim, Chaerin committed
64
        if (reviews) {
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
65
66
67
            if (reviews.length > 10) {
                res.status(200).send({ review: reviews, db: true })
            }
Kim, Chaerin's avatar
Kim, Chaerin committed
68
69
70
71
            req.reviews = reviews
        }
        next()
    })
72
}
73
export default { search, find }