review.controller.js 1.83 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import Review from '../models/Review.js'
import cheerio from "cheerio";
import express from 'express';
import request from 'request-promise'
import jschardet from 'jschardet'
import iconv from 'iconv'
import fs from 'fs'
const Iconv = iconv.Iconv

const signup = async (req, res) => {
    res.send("안녕하세요")
}

const search = async (req, res) => {
Kim, Chaerin's avatar
?    
Kim, Chaerin committed
15
    const url = "https://www.google.com/search?q=" + encodeURI(req.params.search) + "+site%3Atistory.com" + '&page_no=1'
16
17
18
19
20
21
22
23

    request(url)
        .then(anyToUtf8)
        .then((html) => {
            // fs.writeFileSync("googlez.txt", '\ufeff' + html, { encoding: 'utf8' });
            let $ = cheerio.load(html, null, false);
            let places = []
            $('.kCrYT').each(function (i) {
Kim, Chaerin's avatar
?    
Kim, Chaerin committed
24
25
26
27
28
29
30
31
                const review = await Review.findOne({ link: $(this).find('a').attr('href') })
                if (!user) {
                    const newReview = await new Review({
                        name,
                        email,
                        password: hash
                    }).save()
                }
32
33
34
                places[i] = {
                    title: $(this).find('h3').text(),
                    link: $(this).find('a').attr('href'),
Kim, Chaerin's avatar
?    
Kim, Chaerin committed
35
                    summary: $(this).find('.s3v9rd').text(),
36
37
                }
            })
Kim, Chaerin's avatar
?    
Kim, Chaerin committed
38
39

            // console.log(places)  
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
            res.send(places)
        })
    function anyToUtf8(str) {
        const { encoding } = jschardet.detect(str);
        console.log("source encoding = " + encoding);
        const iconv = new Iconv(encoding, "utf-8//translit//ignore");
        return iconv.convert(str).toString();
    }
    // try {
    //     const newPlace = await new Place({
    //         name: req.params.search,
    //         address,
    //         img,

    //     })
    // }
}

Kim, Chaerin's avatar
?    
Kim, Chaerin committed
58
59
const

60
export default { signup, search }