place.controller.js 1.86 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

baesangjune's avatar
.    
baesangjune committed
6
7
const searchPlace = async (req, res) => {
    console.log("여기까지는 옵니다~")
baesangjune's avatar
.    
baesangjune committed
8

baesangjune's avatar
.    
baesangjune committed
9
10
11
12
13
14
    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
            let $1 = cheerio.load(html);
baesangjune's avatar
.    
baesangjune committed
15
16
            
            let places = []
baesangjune's avatar
.    
baesangjune committed
17
18
19
            $1('.kCrYT').each(function (i) {

                if ($1(this).find('h3').text()) {
baesangjune's avatar
.    
baesangjune committed
20
                    places[i] = { name: $1(this).find('h3').text(), link: ($1(this).find('a').attr('href')) }
baesangjune's avatar
.    
baesangjune committed
21
                } else if ($1(this).find('.s3v9rd').find('.s3v9rd').text()) {
baesangjune's avatar
.    
baesangjune committed
22
23
                    places[i - 1] = { ...places[i - 1], address: $1(this).find('.s3v9rd').find('.s3v9rd').text() }
                    places = places.filter(e => e)
baesangjune's avatar
.    
baesangjune committed
24
25
                }
            })
baesangjune's avatar
.    
baesangjune committed
26
27
28
29
30
31
32
            console.log("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@", places)
            res.send(places)
        })
}

const searchImg = async (req, res) => {
    const imgUrl = "https://www.google.com/search?q=" + encodeURI(req.params.search) + "+site:tistory.com/&sxsrf=ALeKk023Dv08KQDodRmpB5222lQuzw2Vaw:1610612821100&source=lnms&tbm=isch"
baesangjune's avatar
.    
baesangjune committed
33

baesangjune's avatar
.    
baesangjune committed
34
35
36
37
38
39
40
41
42
43
44
45
46
47
    axios.get(imgUrl)
        .then((response) => {
            const html = response.data
            let $1 = cheerio.load(html);

            let images = $1('.RAyV4b').find('img').attr('src')

            // let images = []

            // $1('.RAyV4b').each(function (i) {
            //     images[i] = { imgUrl: $(this).find('img').atrr('src') }
            // })
            // console.log('%%%%%%%%%%%%%%%%%%%%%%%%%%%', images)
            res.send(images)
baesangjune's avatar
.    
baesangjune committed
48
        })
baesangjune's avatar
.    
baesangjune committed
49

baesangjune's avatar
.    
baesangjune committed
50
51
}

baesangjune's avatar
.    
baesangjune committed
52
53

export default { searchImg, searchPlace }