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

            let places = {}
            $1('.ct_box_area').each(function (i) {
baesangjune's avatar
.    
baesangjune committed
20
21


baesangjune's avatar
.    
baesangjune committed
22
23
24
25
26
                // 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
27

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

baesangjune's avatar
.    
baesangjune committed
40
    axios.get(imgUrl)
baesangjune's avatar
.    
baesangjune committed
41
        .then(async (response) => {
baesangjune's avatar
.    
baesangjune committed
42
            const html = response.data
baesangjune's avatar
.    
baesangjune committed
43
            let name = req.params.search
baesangjune's avatar
.    
baesangjune committed
44
45
46
47
48
49
50
51
52
53
            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)
baesangjune's avatar
.    
baesangjune committed
54

baesangjune's avatar
.    
baesangjune committed
55
56
57
58
59
            // 여기서 있는건 찾아와서 추가를 시켜야한다.

            //사진만 업데이트
            let Place = await Places.findOne({ name: req.params.search })
            Place.times.push(new Date().toLocaleString())
baesangjune's avatar
.    
baesangjune committed
60
            await Places.updateOne({ name: req.params.search }, { img: images, times: Place.times })
baesangjune's avatar
.    
baesangjune committed
61
62
63

            const newPlaces = await new Places(
            ).save()
baesangjune's avatar
.    
baesangjune committed
64
65

            res.send(images)
baesangjune's avatar
.    
baesangjune committed
66
        })
baesangjune's avatar
.    
baesangjune committed
67

baesangjune's avatar
.    
baesangjune committed
68
69
}

baesangjune's avatar
.    
baesangjune committed
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
const searchAssociation = async (req, res) => {
    let Place = await Places.findOne({ name: req.params.search })
    let address = Place.address.split(' ')[0]


    // let AssociationsId = []

    let addressPlaces = new RegExp(`${address}`)
    let responsePlaces = await Places.find({ address: addressPlaces })
    res.send(responsePlaces)
    // responsePlaces.map(Association => {
    //     AssociationsId.push(Association._id)
    // })
    // console.log("Associations = ", Associations)
    // res.send(AssociationsId)
}

baesangjune's avatar
.    
baesangjune committed
87

baesangjune's avatar
.    
baesangjune committed
88
export default { searchImg, searchPlace, searchAssociation }