place.controller.js 5.88 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'
Lee SeoYeon's avatar
..    
Lee SeoYeon committed
3
// import fs from 'fs'
baesangjune's avatar
.    
baesangjune committed
4
import axios from 'axios';
Lee SeoYeon's avatar
merge    
Lee SeoYeon committed
5
import { nextTick } from 'process';
baesangjune's avatar
.    
baesangjune committed
6

Lee SeoYeon's avatar
merge    
Lee SeoYeon committed
7
const searchPlace = async (req, res, next,) => {
baesangjune's avatar
baesangjune committed
8

Kim, Chaerin's avatar
.    
Kim, Chaerin committed
9
    let DuplicateCheckPlace = await Places.findOne({ name: req.query.keyword })
Lee SeoYeon's avatar
merge    
Lee SeoYeon committed
10
    req.places = DuplicateCheckPlace
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
    if (DuplicateCheckPlace) {
        console.log("11111111111111111111111Place################ 기존플레이스줄력중")
    }
    else {
        console.log("2222222222222222222222222222222222222222222222222222222")
        const url = "https://search.naver.com/search.naver?sm=tab_hty.top&where=nexearch&query=" + encodeURI(req.query.keyword)
        const editUrl = /(http(s)?:\/\/)([a-z0-9\w]+\.*)+[a-z0-9]{2,4}/gi
        axios.get(url)
            .then(async (response) => {
                const html = response.data
                // fs.writeFileSync("googleReview", html, { encoding: 'utf-8' })
                let $1 = cheerio.load(html);

                let places = {}
                $1('.ct_box_area').each(function (i) {
Lee SeoYeon's avatar
merge    
Lee SeoYeon committed
26
27
28
                    places={
                        name: $1('.biz_name').text(), category: $1('.category').text(), address: $1('.addr').text()
                    }
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
29
                })
Lee SeoYeon's avatar
merge    
Lee SeoYeon committed
30

Kim, Chaerin's avatar
.    
Kim, Chaerin committed
31
                // 값이 비어있거나 에러가 생겼을 때를 대비해 try catch를 해야함
Lee SeoYeon's avatar
merge    
Lee SeoYeon committed
32
33
34
35
36
37
                // const newPlaces = await new Places(places[0]
                // ).save()
                // res.send(places)
                req.places = places[0]
                console.log("62168748172", req.places)

baesangjune's avatar
.    
baesangjune committed
38
            })
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
39
    }
baesangjune's avatar
.    
baesangjune committed
40

Lee SeoYeon's avatar
merge    
Lee SeoYeon committed
41
    next()
baesangjune's avatar
.    
baesangjune committed
42

Lee SeoYeon's avatar
merge    
Lee SeoYeon committed
43
}
baesangjune's avatar
.    
baesangjune committed
44

Lee SeoYeon's avatar
merge    
Lee SeoYeon committed
45
const searchImg = async (req, res, next) => {
baesangjune's avatar
.    
baesangjune committed
46

Lee SeoYeon's avatar
merge    
Lee SeoYeon committed
47
    console.log("d213532513212osfnlagm2214124", req.places)
baesangjune's avatar
.    
baesangjune committed
48

Lee SeoYeon's avatar
merge    
Lee SeoYeon committed
49
50
51
52
53
54
55
56
    if (req.places.img) {
        console.log("333333333333333333333333333IMG@@@@@@@@@@@@@@@@@@@ 기존이미지줄력중")
        req.places.times.push(new Date().toLocaleString())
        console.log("세이브 전 111111111111111111", req.places)
        const newPlaces = await new Places(req.places).save()
        next()
    } else {
        const imgUrl = "https://www.google.com/search?q=" + encodeURI(req.query.keyword) + "+site:tistory.com/&sxsrf=ALeKk023Dv08KQDodRmpB5222lQuzw2Vaw:1610612821100&source=lnms&tbm=isch"
Lee SeoYeon's avatar
..    
Lee SeoYeon committed
57

Lee SeoYeon's avatar
merge    
Lee SeoYeon committed
58
59
60
61
62
63
        axios.get(imgUrl)
            .then(async (response) => {
                console.log("4444444444444444444444444444444444444444444444444새로운 이미지 출력중")
                const html = response.data
                let name = req.query.keyword
                let $1 = cheerio.load(html);
Lee SeoYeon's avatar
..    
Lee SeoYeon committed
64

Lee SeoYeon's avatar
merge    
Lee SeoYeon committed
65
                let images = $1('.RAyV4b').find('img').attr('src')
baesangjune's avatar
.    
baesangjune committed
66

Lee SeoYeon's avatar
merge    
Lee SeoYeon committed
67
68
69
70
71
72
73
74
75
                req.places.img = images
                //사진만 업데이트
                // let Place2 = await Places.findOne({ name: req.query.keyword })
                req.places.times.push(new Date().toLocaleString())
                // await Places.updateOne({ name: req.query.keyword }, { img: images, times: Place2.times })
                // res.send(images)
                console.log("세이브 전 222222222222222", req.places)
                const newPlaces = await new Places(req.places).save()
                next()
baesangjune's avatar
.    
baesangjune committed
76

Lee SeoYeon's avatar
merge    
Lee SeoYeon committed
77
78
            })
    }
baesangjune's avatar
.    
baesangjune committed
79

Lee SeoYeon's avatar
merge    
Lee SeoYeon committed
80
}
baesangjune's avatar
.    
baesangjune committed
81

Lee SeoYeon's avatar
merge    
Lee SeoYeon committed
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
//     if (DuplicateCheckImg !== "https://t1.daumcdn.net/thumb/R600x0/?fname=http%3A%2F%2Ft1.daumcdn.net%2Fqna%2Fimage%2F4b035cdf8372d67108f7e8d339660479dfb41bbd") {
//         console.log("333333333333333333333333333IMG@@@@@@@@@@@@@@@@@@@ 기존이미지줄력중")
//     }
//     else if (DuplicateCheckImg === "https://t1.daumcdn.net/thumb/R600x0/?fname=http%3A%2F%2Ft1.daumcdn.net%2Fqna%2Fimage%2F4b035cdf8372d67108f7e8d339660479dfb41bbd") {
//         console.log("4444444444444444444444444444444444444444444444444")
//         const imgUrl = "https://www.google.com/search?q=" + encodeURI(req.query.keyword) + "+site:tistory.com/&sxsrf=ALeKk023Dv08KQDodRmpB5222lQuzw2Vaw:1610612821100&source=lnms&tbm=isch"

//         axios.get(imgUrl)
//             .then(async (response) => {
//                 const html = response.data
//                 let name = req.query.keyword
//                 let $1 = cheerio.load(html);

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

//                 req.places.img = images
//                 //사진만 업데이트
//                 // let Place2 = await Places.findOne({ name: req.query.keyword })
//                 req.places.times.push(new Date().toLocaleString())
//                 // await Places.updateOne({ name: req.query.keyword }, { img: images, times: Place2.times })
//                 // res.send(images)
//                 console.log("세이브 전 111111111111111111", req.places)
//                 const newPlaces = await new Places(req.places).save()
//                 next()
//             })

//     } else {
//         console.log("IMG에러")
//     }
//     req.places.times.push(new Date().toLocaleString())
//     console.log("세이브 전222222222222222222222")
//     next()

// }
//맨 처음 검색하는 지역의 관광지의 경우 association을 받아올 수 없다.
baesangjune's avatar
.    
baesangjune committed
117
const searchAssociation = async (req, res) => {
Lee SeoYeon's avatar
merge    
Lee SeoYeon committed
118
119
    // let Place3 = await Places.findOne({ name: req.query.keyword })
    let Place3 = req.places.address
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
120
121
122
    // if (!Place) {
    //     res.send([])
    // }
Lee SeoYeon's avatar
merge    
Lee SeoYeon committed
123
124
125
126
    if (!Place3) {
        console.log("asdfasdfasdf222222222222222222dsaf2222222222214123q5", Place3)

        res.send({ error: "Place.address is null" })
Lee SeoYeon's avatar
..    
Lee SeoYeon committed
127
128
    }
    else {
Lee SeoYeon's avatar
merge    
Lee SeoYeon committed
129
        let addresse = Place3.split(' ')[0]
baesangjune's avatar
.    
baesangjune committed
130

Kim, Chaerin's avatar
.    
Kim, Chaerin committed
131
        let AssociationsId = []
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
132
133
        let addressPlaces = new RegExp(`${addresse}`)
        let responsePlaces = await Places.find({ address: addressPlaces })
Lee SeoYeon's avatar
merge    
Lee SeoYeon committed
134
        // const newPlaces = await new Places(places[0]).save()
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
135
136
        res.send(responsePlaces)
    }
baesangjune's avatar
.    
baesangjune committed
137
138
139
140
141
142
143
    // responsePlaces.map(Association => {
    //     AssociationsId.push(Association._id)
    // })
    // console.log("Associations = ", Associations)
    // res.send(AssociationsId)
}

baesangjune's avatar
.    
baesangjune committed
144

Lee SeoYeon's avatar
.    
Lee SeoYeon committed
145
 export default { searchImg, searchPlace, searchAssociation }