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

const search = async (req, res) => {
Kim, Chaerin's avatar
서연    
Kim, Chaerin committed
8
9
    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
baesangjune's avatar
baesangjune committed
10
    axios.get(url)
baesangjune's avatar
.    
baesangjune committed
11
12
13
14
        .then((response) => {
            const html = response.data
            fs.writeFileSync("googlez.txt", html, { encoding: 'utf8' });
            let $1 = cheerio.load(html);
Kim, Chaerin's avatar
서연    
Kim, Chaerin committed
15
16
            let reviews = []
            $1('.kCrYT').each(function (i) {
baesangjune's avatar
.    
baesangjune committed
17
18
19
20
21
22

                if ($1(this).find('h3').text()) {
                    reviews[i] = { title: $1(this).find('h3').text(), link: ($1(this).find('a').attr('href')) }
                } else if ($1(this).find('.s3v9rd').find('.s3v9rd').text()) {
                    reviews[i - 1] = { ...reviews[i - 1], summary: $1(this).find('.s3v9rd').find('.s3v9rd').text() }
                    reviews = reviews.filter(e => e)
23
24
                }
            })
baesangjune's avatar
review    
baesangjune committed
25

baesangjune's avatar
.    
baesangjune committed
26
            console.log("*******************************************",reviews)
Kim, Chaerin's avatar
서연    
Kim, Chaerin committed
27
            res.send(reviews)
28
29
30
        })
}

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

32
export default { search }