place.controller.js 1.28 KB
Newer Older
baesangjune's avatar
.    
baesangjune committed
1
2
import Place from '../models/Place.js'
import cheerio from 'cheerio'
baesangjune's avatar
.    
baesangjune committed
3
4
import fs from 'fs'
import axios from 'axios';
baesangjune's avatar
.    
baesangjune committed
5
6
7

const search = async (req, res) => {

baesangjune's avatar
.    
baesangjune committed
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
    console.log("여기까지 도착합니다.@@@@@@@@@@@@@@@@@@@@@@@@@@@@@")
    console.log(req.params.search)
    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)
        .then((response) => {
            const html = response.data
            fs.writeFileSync("googlez.txt", html, { encoding: 'utf8' });
            let $1 = cheerio.load(html);
            let reviews = []
            $1('.kCrYT').each(function (i) {

                if ($1(this).find('h3').text()) {
                    reviews[i] = { name: $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], address: $1(this).find('.s3v9rd').find('.s3v9rd').text() }
                    reviews = reviews.filter(e => e)
                }
            })

            console.log("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@",reviews)
            res.send(reviews)
        })
baesangjune's avatar
.    
baesangjune committed
31
32
}

baesangjune's avatar
.    
baesangjune committed
33
export default { search }