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

const search = async (req, res) => {
Kim, Chaerin's avatar
서연    
Kim, Chaerin committed
10
11
12
    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)  
baesangjune's avatar
review    
baesangjune committed
13
        // .then(anyToUtf8)
baesangjune's avatar
.    
baesangjune committed
14
15
        .then((html) => {
            // fs.writeFileSync("googlez.txt", '\ufeff' + html, { encoding: 'utf8' });
Kim, Chaerin's avatar
서연    
Kim, Chaerin committed
16
17
18
19
20
21
            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)
Kim, Chaerin's avatar
?    
Kim, Chaerin committed
22
                }
Kim, Chaerin's avatar
서연    
Kim, Chaerin committed
23
24
25
26
                reviews[i] = {
                    title: $1(this).find('h3').text(),
                    link: link,
                    summary: $1(this).find('.s3v9rd').text(),
27
28
                }
            })
Kim, Chaerin's avatar
서연    
Kim, Chaerin committed
29
30
31
32
33
34
35
36
37
            // 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()))
            //             })
            //         })
            // })
baesangjune's avatar
review    
baesangjune committed
38

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

    //     })
    // }
}

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

58
export default { search }