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

Kim, Chaerin's avatar
.    
Kim, Chaerin committed
8
    let DuplicateCheckPlace = await Places.findOne({ name: req.query.keyword })
baesangjune's avatar
baesangjune committed
9

Kim, Chaerin's avatar
.    
Kim, Chaerin committed
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
    if (DuplicateCheckPlace) {
        res.send(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) {
                    places[i] = { name: $1('.biz_name').text(), category: $1('.category').text(), address: $1('.addr').text() }
                })
                // 값이 비어있거나 에러가 생겼을 때를 대비해 try catch를 해야함
                const newPlaces = await new Places(places[0]
                ).save()
                res.send(places)
baesangjune's avatar
.    
baesangjune committed
32
            })
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
33
    }
baesangjune's avatar
.    
baesangjune committed
34
35
36
}

const searchImg = async (req, res) => {
baesangjune's avatar
.    
baesangjune committed
37

Kim, Chaerin's avatar
.    
Kim, Chaerin committed
38
    let DuplicateCheckImg = await Places.findOne({ name: req.query.keyword })
baesangjune's avatar
.    
baesangjune committed
39

Kim, Chaerin's avatar
.    
Kim, Chaerin committed
40
41
42
43
44
45
46
47
    if (DuplicateCheckImg) {
        if (DuplicateCheckImg.img !== "https://t1.daumcdn.net/thumb/R600x0/?fname=http%3A%2F%2Ft1.daumcdn.net%2Fqna%2Fimage%2F4b035cdf8372d67108f7e8d339660479dfb41bbd") {
            res.send(DuplicateCheckImg)
            console.log("333333333333333333333333333IMG@@@@@@@@@@@@@@@@@@@ 기존이미지줄력중")
        }
        else if (DuplicateCheckImg.img === "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"
baesangjune's avatar
.    
baesangjune committed
48

Lee SeoYeon's avatar
.    
Lee SeoYeon committed
49
50
51
            axios.get(imgUrl)
                .then(async (response) => {
                    const html = response.data
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
52
                    let name = req.query.keyword
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
53
                    let $1 = cheerio.load(html);
baesangjune's avatar
.    
baesangjune committed
54

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

Lee SeoYeon's avatar
.    
Lee SeoYeon committed
57
                    //사진만 업데이트
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
58
                    let Place = await Places.findOne({ name: req.query.keyword })
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
59
                    Place.times.push(new Date().toLocaleString())
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
60
                    await Places.updateOne({ name: req.query.keyword }, { img: images, times: Place.times })
baesangjune's avatar
baesangjune committed
61

Lee SeoYeon's avatar
.    
Lee SeoYeon committed
62
63
                    res.send(images)
                })
baesangjune's avatar
.    
baesangjune committed
64

Kim, Chaerin's avatar
.    
Kim, Chaerin committed
65
66
67
68
        } else {
            console.log("IMG에러")
        }
    }
baesangjune's avatar
.    
baesangjune committed
69

baesangjune's avatar
.    
baesangjune committed
70

baesangjune's avatar
.    
baesangjune committed
71

72
        }
baesangjune's avatar
.    
baesangjune committed
73

baesangjune's avatar
.    
baesangjune committed
74
const searchAssociation = async (req, res) => {
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
75
        let Place = await Places.findOne({ name: req.query.keyword })
baesangjune's avatar
.    
baesangjune committed
76

Kim, Chaerin's avatar
.    
Kim, Chaerin committed
77
78
79
    // if (!Place) {
    //     res.send([])
    // }
Kim, Chaerin's avatar
Kim, Chaerin committed
80
    if (!Place) {
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
81
82
83
        res.status(404).send({ error: "Place.address is null" })
    }
    else {
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
84
        let addresse = Place.address.split(' ')[0]
baesangjune's avatar
.    
baesangjune committed
85

Kim, Chaerin's avatar
.    
Kim, Chaerin committed
86
        let AssociationsId = []
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
87
        let addressPlaces = new RegExp(`${addresse}`)
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
88
        console.log("여기보세요", addressPlaces)
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
89
90
91
        let responsePlaces = await Places.find({ address: addressPlaces })
        res.send(responsePlaces)
    }
baesangjune's avatar
.    
baesangjune committed
92
93
94
95
96
97
98
    // responsePlaces.map(Association => {
    //     AssociationsId.push(Association._id)
    // })
    // console.log("Associations = ", Associations)
    // res.send(AssociationsId)
}

baesangjune's avatar
.    
baesangjune committed
99

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