place.controller.js 3.73 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 { nextTick } from 'process';
baesangjune's avatar
.    
baesangjune committed
6

baesangjune's avatar
baesangjune 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 })
baesangjune's avatar
baesangjune committed
10
    req.places = DuplicateCheckPlace
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
11
12
13
14
15
16
17
    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
18
19
20
21
22
23
24
25
26
27
28
        const { data: html } = await axios.get(url)
        let $1 = cheerio.load(html);
        let places = {}
        $1('.ct_box_area').each(function (i) {
            places = {
                name: $1('.biz_name').text(), category: $1('.category').text(), address: $1('.addr').text(),
                img: "https://t1.daumcdn.net/thumb/R600x0/?fname=http%3A%2F%2Ft1.daumcdn.net%2Fqna%2Fimage%2F4b035cdf8372d67108f7e8d339660479dfb41bbd",
                times: []
            }
        })
        req.places = places
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
29
    }
baesangjune's avatar
.    
baesangjune committed
30

baesangjune's avatar
baesangjune committed
31
    next()
baesangjune's avatar
.    
baesangjune committed
32

baesangjune's avatar
baesangjune committed
33
}
baesangjune's avatar
.    
baesangjune committed
34

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

baesangjune's avatar
.    
baesangjune committed
37

38
    if (req.places.img !== "https://t1.daumcdn.net/thumb/R600x0/?fname=http%3A%2F%2Ft1.daumcdn.net%2Fqna%2Fimage%2F4b035cdf8372d67108f7e8d339660479dfb41bbd") {
baesangjune's avatar
baesangjune committed
39
40
41
42
        console.log("333333333333333333333333333IMG@@@@@@@@@@@@@@@@@@@ 기존이미지줄력중")
        req.places.times.push(new Date().toLocaleString())
        const newPlaces = await new Places(req.places).save()
        next()
43
    } else if (req.places.img === "https://t1.daumcdn.net/thumb/R600x0/?fname=http%3A%2F%2Ft1.daumcdn.net%2Fqna%2Fimage%2F4b035cdf8372d67108f7e8d339660479dfb41bbd") {
baesangjune's avatar
baesangjune committed
44
        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
45

baesangjune's avatar
baesangjune committed
46
47
48
49
50
51
        axios.get(imgUrl)
            .then(async (response) => {
                console.log("4444444444444444444444444444444444444444444444444새로운 이미지 출력중")
                const html = response.data
                let name = req.query.keyword
                let $1 = cheerio.load(html);
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
52

baesangjune's avatar
baesangjune committed
53
                let images = $1('.RAyV4b').find('img').attr('src')
baesangjune's avatar
.    
baesangjune committed
54

baesangjune's avatar
baesangjune committed
55
                req.places.img = images
56
57

                console.log("4141414141414141", req.places)
baesangjune's avatar
baesangjune committed
58
59
60
61
62
63
64
                //사진만 업데이트
                // 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)
                const newPlaces = await new Places(req.places).save()
                next()
baesangjune's avatar
.    
baesangjune committed
65

baesangjune's avatar
baesangjune committed
66
            })
67
68
    } else {
        console.log("이미지 생성 오류발생!!")
baesangjune's avatar
baesangjune committed
69
    }
baesangjune's avatar
.    
baesangjune committed
70

baesangjune's avatar
.    
baesangjune committed
71
}
baesangjune's avatar
.    
baesangjune committed
72
const searchAssociation = async (req, res) => {
baesangjune's avatar
baesangjune committed
73
74
75
76
77
    let Place3 = req.places.address
    if (!Place3) {
        console.log("asdfasdfasdf222222222222222222dsaf2222222222214123q5", Place3)

        res.send({ error: "Place.address is null" })
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
78
79
    }
    else {
baesangjune's avatar
baesangjune committed
80
        let addresse = Place3.split(' ')[0]
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
81
82
83

        let AssociationsId = []
        let addressPlaces = new RegExp(`${addresse}`)
84
85
        let responsePlaces = await Places.find({ address: addressPlaces }).sort({ updatedAt: -1 })
        //몽구스나 몽고디비에 있는 sort 확인해보고 나열하기.
baesangjune's avatar
baesangjune committed
86
        // console.log("$$$$$$$$$$$4", responsePlaces)
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
87
        res.send(responsePlaces)
Kim, Chaerin's avatar
Kim, Chaerin committed
88
    }
baesangjune's avatar
.    
baesangjune committed
89
90
}

baesangjune's avatar
.    
baesangjune committed
91

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