review.controller.js 1.76 KB
Newer Older
1
import Review from '../models/Review.js'
Kim, Chaerin's avatar
Kim, Chaerin committed
2
import cheerio, { html } from "cheerio";
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';
Kim, Chaerin's avatar
Kim, Chaerin committed
7
import { nextTick } from 'process';
8
9
const Iconv = iconv.Iconv

Kim, Chaerin's avatar
Kim, Chaerin committed
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
const search = async (req, res, next) => {
    // const url = "https://www.google.com/search?q=" + encodeURI(req.params.search) + "+site%3Atistory.com&page_no=1"
    try {
        const url = "https://www.google.com/search?q=kakao+site%3Atistory.com&page_no=1"
        let reviews = []
        const response1 = await axios.get(url)
        const $1 = cheerio.load(response1.data);
        $1('.kCrYT').each(async function (i) {
            const searchParams = new URLSearchParams($1(this).find('a').attr('href'));
            const title = $1(this).find('h3').text()
            const link = searchParams.get("/url?q")
            const summary = $1(this).find('.s3v9rd').find('.s3v9rd').text()
            const content = await getReview(link)
            if (title) {
                reviews[i] = { title: title, link: link, content: content }
            } else if (summary) {
                reviews[i - 1] = { ...reviews[i - 1], summary: summary }
                reviews = reviews.filter(e => e)
            }
            // const review = new Review(reviews).save()
30
        })
Kim, Chaerin's avatar
Kim, Chaerin committed
31
32
33
34
        res.send(reviews)
    } catch (error) {
        console.log(error)
        res.send(error)
35
36
37
    }
}

Kim, Chaerin's avatar
Kim, Chaerin committed
38
39
40
41
42
43
44
45
46
47
const getReview = async (link) => {
    let content = '없음'
    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()
    }
    // console.log(content,"getReiview")
    return content
}
Kim, Chaerin's avatar
?    
Kim, Chaerin committed
48

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