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

Kim, Chaerin's avatar
Kim, Chaerin committed
6
const search = async (req, res, next) => {
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
7
    //**************************구글 크롤링 할 때************************/
Kim, Chaerin's avatar
Kim, Chaerin committed
8
    try {
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
9
        let reviews = [] 
10
        let content = []
baesangjune's avatar
baesangjune committed
11
        // Review.find()
12
        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
13
        const response1 = await axios.get(url)
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
14
15
16

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

17
        // console.log(response1.data)
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
18
        const $1 = cheerio.load(response1.data); //cheerio? reponse1의 데이터를 로드하는거
Kim, Chaerin's avatar
Kim, Chaerin committed
19
        $1('.kCrYT').each(async function (i) {
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
20
            const title = $1(this).find('h3').text() //타이틀에 텍스트 안에 h3값을 찾는다?
21
            const searchParams = new URLSearchParams($1(this).find('a').attr('href'));
Kim, Chaerin's avatar
Kim, Chaerin committed
22
23
            const link = searchParams.get("/url?q")
            const summary = $1(this).find('.s3v9rd').find('.s3v9rd').text()
Kim, Chaerin's avatar
상준    
Kim, Chaerin committed
24
            content.push(getReview(link))
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
25
            if (title) { //타이틀일때 리뷰에 [i] 를 넣었을 때 타이틀, 링크값 설정
26
                reviews[i] = { title: title, link: link }
Kim, Chaerin's avatar
Kim, Chaerin committed
27
28
29
30
            } else if (summary) {
                reviews[i - 1] = { ...reviews[i - 1], summary: summary }
                reviews = reviews.filter(e => e)
            }
31
        })
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
32
        const promiseReview = await Promise.all(content)
baesangjune's avatar
.    
baesangjune committed
33
        reviews.forEach(async (review, i) => {
34
35
            review["content"] = promiseReview[i]
        })
Kim, Chaerin's avatar
Kim, Chaerin committed
36
37
        res.send(reviews)
    } catch (error) {
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
38
        // console.log(error)
baesangjune's avatar
baesangjune committed
39
        // res.send(error)
40
41
42
    }
}

Lee SeoYeon's avatar
.    
Lee SeoYeon committed
43
44


baesangjune's avatar
.    
baesangjune committed
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//***************네이버 크롤링 할 때 ********************* */
//     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
63
const getReview = async (link) => {
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
64
    let content = '없음' 
65
66
    if (link) {
        const res = await axios.get(link)
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
67
        const $2 = cheerio.load(res.data); // cheerio 의미
68
69
70
        if ($2('.tt_article_useless_p_margin').text()) {
            content = $2('.tt_article_useless_p_margin').text()
        }
Kim, Chaerin's avatar
Kim, Chaerin committed
71
    }
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
72
    return content
Kim, Chaerin's avatar
Kim, Chaerin committed
73
}
Kim, Chaerin's avatar
?    
Kim, Chaerin committed
74

Lee SeoYeon's avatar
.    
Lee SeoYeon committed
75
export default { search, getReview }
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
76