place.controller.js 2 KB
Newer Older
baesangjune's avatar
.    
baesangjune committed
1
import Places from '../models/Place.js'
baesangjune's avatar
.    
baesangjune committed
2
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
const searchPlace = async (req, res) => {
baesangjune's avatar
.    
baesangjune committed
7

baesangjune's avatar
.    
baesangjune committed
8
    const url = "https://search.naver.com/search.naver?sm=tab_hty.top&where=nexearch&query=" + encodeURI(req.params.search)
baesangjune's avatar
.    
baesangjune committed
9
10
    const editUrl = /(http(s)?:\/\/)([a-z0-9\w]+\.*)+[a-z0-9]{2,4}/gi
    axios.get(url)
baesangjune's avatar
.    
baesangjune committed
11
        .then( async (response) => {
baesangjune's avatar
.    
baesangjune committed
12
13
            const html = response.data
            let $1 = cheerio.load(html);
baesangjune's avatar
.    
baesangjune committed
14
15
16
17
18
19
20
21
22
23

            let places = {}
            $1('.ct_box_area').each(function (i) {
                // console.log($1('.biz_name').text())
                // console.log($1('.category').text())
                // console.log($1('.addr').text())
                // console.log($1('.t?ime highlight').text())

                
                places[i] = { name: $1('.biz_name').text(), category: $1('.category').text(), address: $1('.addr').text() }
baesangjune's avatar
.    
baesangjune committed
24
            })
baesangjune's avatar
.    
baesangjune committed
25
26
27
            // 값이 비어있거나 에러가 생겼을 때를 대비해 try catch를 해야함
            const newPlaces = await new Places(places[0]
            ).save()
baesangjune's avatar
.    
baesangjune committed
28
29
30
31
32
33
            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
34

baesangjune's avatar
.    
baesangjune committed
35
    axios.get(imgUrl)
baesangjune's avatar
.    
baesangjune committed
36
        .then(async (response) => {
baesangjune's avatar
.    
baesangjune committed
37
            const html = response.data
baesangjune's avatar
.    
baesangjune committed
38
            let name = req.params.search
baesangjune's avatar
.    
baesangjune committed
39
40
41
42
43
44
45
46
47
48
49
            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
50
51
            const newPlaces = await new Places(name,images
                ).save()
baesangjune's avatar
.    
baesangjune committed
52
        })
baesangjune's avatar
.    
baesangjune committed
53

baesangjune's avatar
.    
baesangjune committed
54
55
}

baesangjune's avatar
.    
baesangjune committed
56
57

export default { searchImg, searchPlace }