review.controller.js 5.15 KB
Newer Older
1
2
import Review from '../models/Review.js'
import cheerio from "cheerio";
3
<<<<<<< HEAD
4
import fs from 'fs'
Kim, Chaerin's avatar
서연    
Kim, Chaerin committed
5
import axios from 'axios';
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
6

Kim, Chaerin's avatar
Kim, Chaerin committed
7
const search = async (req, res, next) => {
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
8
    //**************************구글 크롤링 할 때************************/
Kim, Chaerin's avatar
Kim, Chaerin committed
9
    try {
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
10
        let reviews = [] 
11
        let content = []
baesangjune's avatar
baesangjune committed
12
        // Review.find()
13
        const url = "https://www.google.com/search?q=" + encodeURI(req.params.search) + "+site%3Atistory.com&page_no=1"
Kim, Chaerin's avatar
Kim, Chaerin committed
14
        const response1 = await axios.get(url)
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
15
16
17

        fs.writeFileSync("googleSearch", response1.data, { encoding: 'utf-8' })

18
        // console.log(response1.data)
Kim, Chaerin's avatar
Kim, Chaerin committed
19
20
21
        const $1 = cheerio.load(response1.data);
        $1('.kCrYT').each(async function (i) {
            const title = $1(this).find('h3').text()
22
            const searchParams = new URLSearchParams($1(this).find('a').attr('href'));
Kim, Chaerin's avatar
Kim, Chaerin committed
23
24
            const link = searchParams.get("/url?q")
            const summary = $1(this).find('.s3v9rd').find('.s3v9rd').text()
Kim, Chaerin's avatar
상준    
Kim, Chaerin committed
25
            content.push(getReview(link))
Kim, Chaerin's avatar
Kim, Chaerin committed
26
            if (title) {
27
                reviews[i] = { title: title, link: link }
Kim, Chaerin's avatar
Kim, Chaerin committed
28
29
30
31
            } else if (summary) {
                reviews[i - 1] = { ...reviews[i - 1], summary: summary }
                reviews = reviews.filter(e => e)
            }
32
        })
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
33
        const promiseReview = await Promise.all(content)
baesangjune's avatar
.    
baesangjune committed
34
        reviews.forEach(async (review, i) => {
35
36
            review["content"] = promiseReview[i]
        })
Kim, Chaerin's avatar
Kim, Chaerin committed
37
38
        res.send(reviews)
    } catch (error) {
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
39
        // console.log(error)
baesangjune's avatar
baesangjune committed
40
        // res.send(error)
41
=======
Kim, Chaerin's avatar
서연    
Kim, Chaerin committed
42
import axios from 'axios';
43

Kim, Chaerin's avatar
Kim, Chaerin committed
44
const search = async (req, res, next) => {
Kim, Chaerin's avatar
Kim, Chaerin committed
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
    console.log("1")
    for (let i = 1; (req.reviews).length > 10; i++) {
        try {
            console.log("2")
            let reviews = []
            let content = []
            const url = "https://www.google.com/search?q=" + encodeURI(req.query.keyword) + "+site%3Atistory.com&page_no=" + i
            const response1 = await axios.get(url)
            // console.log(response1.data)
            const $1 = cheerio.load(response1.data);
            $1('.kCrYT').each(async function (i) {
                console.log("3")
                const title = $1(this).find('h3').text()
                const searchParams = new URLSearchParams($1(this).find('a').attr('href'));
                const link = searchParams.get("/url?q")
                const summary = $1(this).find('.s3v9rd').find('.s3v9rd').text()
                Review.find({ link: link }, function (err, review) {
                    if (!review) {
                        content.push(getReview(link))
                        if (title) {
                            reviews[i] = { title: title, link: link }
                        } else if (summary) {
                            reviews[i - 1] = { ...reviews[i - 1], summary: summary }
                            reviews = reviews.filter(e => e)
                        }
                    }
                })
            })
            let promiseReview = await Promise.all(content)
            promiseReview = promiseReview.filter(e => typeof (e) === 'string')
            reviews.forEach(async (review, i) => {
                console.log("4")
                review["content"] = promiseReview[i]
                const reviews = new Review(review).save()
                reviews.keyword.push(req.query.keyword)
                req.reviews.push(reviews)
            })
            console.log("5")
            res.send(reviews)
        } catch (error) {
            console.log("6")
            console.log(error)
            res.send(error)
        }
89
>>>>>>> origin/cherry
90
91
92
    }
}

Lee SeoYeon's avatar
.    
Lee SeoYeon committed
93
94


baesangjune's avatar
.    
baesangjune committed
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
//***************네이버 크롤링 할 때 ********************* */
//     try {
//         let reviews = []
//         let content = []
//         const url = "https://search.naver.com/search.naver?where=view&sm=tab_jum&query=" + encodeURI('한라산')
//         const response1 = await axios.get(url)
//         // console.log(response1.data)
//         const $1 = cheerio.load(response1.data);
//         $1('._list').find('.total_wrap').each(async function (i) {
//             reviews[i] = { name: $1(this).find('.total_tit').text(), summery: $1(this).find('.dsc_txt').text(), link: $1(this).find('.total_tit').attr('href') }
//         })
//         res.send(reviews)
//     } catch (error) {
//         console.log(error)
//         res.send(error)
//     }
// }

Kim, Chaerin's avatar
상준    
Kim, Chaerin committed
113
const getReview = async (link) => {
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
114
    let content = '없음' 
115
116
    if (link) {
        const res = await axios.get(link)
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
117
        const $2 = cheerio.load(res.data); // cheerio 의미
118
119
120
        if ($2('.tt_article_useless_p_margin').text()) {
            content = $2('.tt_article_useless_p_margin').text()
        }
Kim, Chaerin's avatar
Kim, Chaerin committed
121
    }
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
122
    return content
Kim, Chaerin's avatar
Kim, Chaerin committed
123
}
Kim, Chaerin's avatar
?    
Kim, Chaerin committed
124

125
<<<<<<< HEAD
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
126
export default { search, getReview }
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
127

128
=======
Kim, Chaerin's avatar
Kim, Chaerin committed
129
130
131
132
133
134
135
136
137
138
139
140
141
const find = (req, res, next) => {
    console.log("7", req.body)
    if (req.body.db) {
        next()
    }
    Review.find({ keyword:{ $elemMatch: res.query.keyword } }, function (err, reviews) {
        if (reviews) {
            if (reviews.length > 10)
                res.send(reviews, { db: true })
            req.reviews = reviews
        }
        next()
    })
142
}
143
144
export default { search, find }
>>>>>>> origin/cherry