review.controller.js 3.7 KB
Newer Older
1
2
import Review from '../models/Review.js'
import cheerio from "cheerio";
Lee SeoYeon's avatar
0113    
Lee SeoYeon committed
3
4
5
// import jschardet from 'jschardet'
// import iconv from 'iconv'
// import fs from 'fs'
Kim, Chaerin's avatar
서연    
Kim, Chaerin committed
6
import axios from 'axios';
Lee SeoYeon's avatar
0113    
Lee SeoYeon committed
7
// const Iconv = iconv.Iconv
8

9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// const search = async (req, res) => {
//     const url = "https://www.google.com/search?q=" + encodeURI(req.params.search) + "+site%3Atistory.com&page_no=1"
//     const editUrl = /(http(s)?:\/\/)([a-z0-9\w]+\.*)+[a-z0-9]{2,4}/gi
//     axios.get(url)
//         // .then(anyToUtf8)
//         .then((html) => {
//             // fs.writeFileSync("googlez.txt", '\ufeff' + html, { encoding: 'utf8' });
//             let $1 = cheerio.load(html.data);
//             let reviews = []
//             $1('.kCrYT').each(function (i) {
//                 let link = ""
//                 if ($1(this).find('a').attr('href')) {
//                     link = ($1(this).find('a').attr('href')).match(editUrl)
//                 }
//                 reviews[i] = {
//                     title: $1(this).find('h3').text(),
//                     link: link,
//                     summary: $1(this).find('.s3v9rd').text(),
//                 }
//             })
//             // reviews.forEach((review, i) => {
//             //     axios.get(review.link)
//             //         .then((html) => {
//             //             let $2 = cheerio.load(html.data);
//             //             $2('').each(function(i){
//             //                 review.content($2(this).find(''.text()))
//             //             })
//             //         })
//             // })
Kim, Chaerin's avatar
?    
Kim, Chaerin committed
38

39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//             console.log(reviews)
//             res.send(reviews)
//         })
//     // function anyToUtf8(str) {
//     //     const { encoding } = jschardet.detect(str); // 웹페이지 문서의 인코딩 타입을 확인
//     //     const iconv = new Iconv(encoding, "utf-8//translit//ignore"); // euc-kr 인코딩변환
//     //     return iconv.convert(str).toString();
//     // }
//     // try {
//     //     const newPlace = await new Place({
//     //         name: req.params.search,
//     //         address,
//     //         img,

//     //     })
//     // }
55

Kim, Chaerin's avatar
Kim, Chaerin committed
56
57
58
const search = async (req, res, next) => {
    try {
        let reviews = []
59
60
        let content = []
        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
61
        const response1 = await axios.get(url)
62
        // console.log(response1.data)
Kim, Chaerin's avatar
Kim, Chaerin committed
63
64
65
        const $1 = cheerio.load(response1.data);
        $1('.kCrYT').each(async function (i) {
            const title = $1(this).find('h3').text()
66
            const searchParams = new URLSearchParams($1(this).find('a').attr('href'));
Kim, Chaerin's avatar
Kim, Chaerin committed
67
68
            const link = searchParams.get("/url?q")
            const summary = $1(this).find('.s3v9rd').find('.s3v9rd').text()
Kim, Chaerin's avatar
상준    
Kim, Chaerin committed
69
            content.push(getReview(link))
Kim, Chaerin's avatar
Kim, Chaerin committed
70
            if (title) {
71
                reviews[i] = { title: title, link: link }
Kim, Chaerin's avatar
Kim, Chaerin committed
72
73
74
75
            } else if (summary) {
                reviews[i - 1] = { ...reviews[i - 1], summary: summary }
                reviews = reviews.filter(e => e)
            }
76
        })
77
        const promiseReview = await Promise.all(content)
Kim, Chaerin's avatar
상준    
Kim, Chaerin committed
78
        reviews.forEach(async(review, i) => {
79
80
            review["content"] = promiseReview[i]
        })
Kim, Chaerin's avatar
Kim, Chaerin committed
81
82
83
84
        res.send(reviews)
    } catch (error) {
        console.log(error)
        res.send(error)
85
86
87
    }
}

Kim, Chaerin's avatar
상준    
Kim, Chaerin committed
88
const getReview = async (link) => {
Kim, Chaerin's avatar
Kim, Chaerin committed
89
    let content = '없음'
90
91
92
93
94
95
    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
96
97
98
    }
    return content
}
Kim, Chaerin's avatar
?    
Kim, Chaerin committed
99

Kim, Chaerin's avatar
Kim, Chaerin committed
100
export default { search, getReview }