review.controller copy.js 3.17 KB
Newer Older
Kim, Chaerin's avatar
Kim, Chaerin committed
1
2
3
4
5
6
7
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import Review from '../models/Review.js'
import cheerio, { html } from "cheerio";
import jschardet from 'jschardet'
import iconv from 'iconv'
import fs from 'fs'
import axios from 'axios';
import { nextTick } from 'process';
const Iconv = iconv.Iconv

const search = async (req, res, next) => {
    // const url = "https://www.google.com/search?q=" + encodeURI(req.params.search) + "+site%3Atistory.com&page_no=1"
    const url = "https://www.google.com/search?q=" + encodeURI("한라산") + "+site%3Atistory.com&page_no=1"
    let reviews = []
    try {
        axios.get(url)
            // .then(anyToUtf8)
            .then((html) => {
                const $1 = cheerio.load(html.data);
                $1('.kCrYT').each(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 response = axios.get(link)
                    console.log(response)
                    // const $2 = cheerio.load(toString(response.data));
                    let content = "없음"
                    // if ($2('.tt_article_useless_p_margin').text()) {
                    //     content = $2('.tt_article_useless_p_margin').text()
                    // }
                    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)
                    }

                    // reviews.forEach((review, i) => {
                    //     axios.get(review.link)
                    //         .then((html) => {
                    //             const $2 = cheerio.load(html.data);
                    //             let content = '없음'
                    //             if ($2('.tt_article_useless_p_margin').text()) {
                    //                 content = $2('.tt_article_useless_p_margin').text()
                    //             }
                    //             // fs.writeFileSync(`tistory${i}.txt`, '\ufeff' + html.data, { encoding: 'utf8' });
                    //             // console.log(`${i}번째, ${review.content}`)
                    //             review["content"] = content
                    //             // console.log(review)
                    //         })
                    //     })
                    //     // const review = new Review(reviews).save()
                })
                res.send(reviews)
            })
    } catch (error) {
        console.log(error)
        res.status(500).send('리뷰 저장 에러')
    }
}

const getReview = (link) => {
    let content = '없음'
    const res = axios.get(link)
    console.log(res)
    // 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)
    return content
}

export default { search, getReview }