place.controller.js 2.71 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
import { time } from 'console';
baesangjune's avatar
.    
baesangjune committed
6

baesangjune's avatar
.    
baesangjune committed
7
const searchPlace = async (req, res) => {
baesangjune's avatar
.    
baesangjune committed
8

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

            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())

baesangjune's avatar
.    
baesangjune committed
25

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

baesangjune's avatar
.    
baesangjune committed
38
    axios.get(imgUrl)
baesangjune's avatar
.    
baesangjune committed
39
        .then(async (response) => {
baesangjune's avatar
.    
baesangjune committed
40
            const html = response.data
baesangjune's avatar
.    
baesangjune committed
41
            let name = req.params.search
baesangjune's avatar
.    
baesangjune committed
42
43
44
45
46
47
48
49
50
51
52
            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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
            // 여기서 있는건 찾아와서 추가를 시켜야한다.

            //사진만 업데이트
            let Place = await Places.findOne({ name: req.params.search })
            Place.times.push(new Date().toLocaleString())
            await Places.updateOne({ name: req.params.search }, { img: images, times : Place.times })
            await Places.createIndex({address : "text", name : "text"})
            
            

            console.log("************************************************", await Places.find({$text:{$search:"\"경남\""}}))
            // console.log(await Places.find())
            const newPlaces = await new Places(
            ).save()
baesangjune's avatar
.    
baesangjune committed
67
        })
baesangjune's avatar
.    
baesangjune committed
68

baesangjune's avatar
.    
baesangjune committed
69
70
}

baesangjune's avatar
.    
baesangjune committed
71
72

export default { searchImg, searchPlace }