review.controller.js 2.9 KB
Newer Older
1
2
import Review from '../models/Review.js'
import cheerio from "cheerio";
3
<<<<<<< HEAD
Lee SeoYeon's avatar
0113    
Lee SeoYeon committed
4
5
6
// import jschardet from 'jschardet'
// import iconv from 'iconv'
// import fs from 'fs'
Kim, Chaerin's avatar
서연    
Kim, Chaerin committed
7
import axios from 'axios';
8
=======
9
10
import jschardet from 'jschardet'
import fs from 'fs'
Kim, Chaerin's avatar
서연    
Kim, Chaerin committed
11
import axios from 'axios';
Kim, Chaerin's avatar
Kim, Chaerin committed
12
import { nextTick } from 'process';
13
>>>>>>> origin/Sangjune
Lee SeoYeon's avatar
0113    
Lee SeoYeon committed
14
// const Iconv = iconv.Iconv
15

Kim, Chaerin's avatar
Kim, Chaerin committed
16
const search = async (req, res, next) => {
baesangjune's avatar
.    
baesangjune committed
17
    //**************************구글 크롤링 할 때************************/
Kim, Chaerin's avatar
Kim, Chaerin committed
18
19
    try {
        let reviews = []
20
        let content = []
baesangjune's avatar
.    
baesangjune committed
21

22
        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
23
        const response1 = await axios.get(url)
baesangjune's avatar
.    
baesangjune committed
24
25
26

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

27
        // console.log(response1.data)
Kim, Chaerin's avatar
Kim, Chaerin committed
28
29
30
        const $1 = cheerio.load(response1.data);
        $1('.kCrYT').each(async function (i) {
            const title = $1(this).find('h3').text()
31
            const searchParams = new URLSearchParams($1(this).find('a').attr('href'));
Kim, Chaerin's avatar
Kim, Chaerin committed
32
33
            const link = searchParams.get("/url?q")
            const summary = $1(this).find('.s3v9rd').find('.s3v9rd').text()
Kim, Chaerin's avatar
상준    
Kim, Chaerin committed
34
            content.push(getReview(link))
Kim, Chaerin's avatar
Kim, Chaerin committed
35
            if (title) {
36
                reviews[i] = { title: title, link: link }
Kim, Chaerin's avatar
Kim, Chaerin committed
37
38
39
40
            } else if (summary) {
                reviews[i - 1] = { ...reviews[i - 1], summary: summary }
                reviews = reviews.filter(e => e)
            }
41
        })
42
        const promiseReview = await Promise.all(content)
baesangjune's avatar
.    
baesangjune committed
43
        reviews.forEach(async (review, i) => {
44
45
            review["content"] = promiseReview[i]
        })
Kim, Chaerin's avatar
Kim, Chaerin committed
46
47
        res.send(reviews)
    } catch (error) {
baesangjune's avatar
.    
baesangjune committed
48
        // console.log(error)
Kim, Chaerin's avatar
Kim, Chaerin committed
49
        res.send(error)
50
51
52
    }
}

baesangjune's avatar
.    
baesangjune committed
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72


//***************네이버 크롤링 할 때 ********************* */
//     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
73
const getReview = async (link) => {
Kim, Chaerin's avatar
Kim, Chaerin committed
74
    let content = '없음'
75
76
77
78
79
80
    if (link) {
        const res = await axios.get(link)
        const $2 = cheerio.load(res.data);
        if ($2('.tt_article_useless_p_margin').text()) {
            content = $2('.tt_article_useless_p_margin').text()
        }
Kim, Chaerin's avatar
Kim, Chaerin committed
81
82
83
    }
    return content
}
Kim, Chaerin's avatar
?    
Kim, Chaerin committed
84

85
export default { search, getReview }
baesangjune's avatar
.    
baesangjune committed
86
// export default { search, }