place.controller.js 2.75 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';
baesangjune's avatar
.    
baesangjune committed
5

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

Kim, Chaerin's avatar
.    
Kim, Chaerin committed
8
    let DuplicateCheckPlace = await Places.findOne({ name: req.query.keyword })
Lee SeoYeon's avatar
merge    
Lee SeoYeon committed
9
    req.places = DuplicateCheckPlace
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
10
11
12
13
14
    if (DuplicateCheckPlace) {
    }
    else {
        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
15
16
17
18
19
20
21
22
23
24
25
        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
26
    }
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
27

Lee SeoYeon's avatar
merge    
Lee SeoYeon committed
28
    next()
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
29

Lee SeoYeon's avatar
merge    
Lee SeoYeon committed
30
}
baesangjune's avatar
.    
baesangjune committed
31

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

baesangjune's avatar
.    
baesangjune committed
34

35
    if (req.places.img !== "https://t1.daumcdn.net/thumb/R600x0/?fname=http%3A%2F%2Ft1.daumcdn.net%2Fqna%2Fimage%2F4b035cdf8372d67108f7e8d339660479dfb41bbd") {
Lee SeoYeon's avatar
merge    
Lee SeoYeon committed
36
37
38
        req.places.times.push(new Date().toLocaleString())
        const newPlaces = await new Places(req.places).save()
        next()
39
    } else if (req.places.img === "https://t1.daumcdn.net/thumb/R600x0/?fname=http%3A%2F%2Ft1.daumcdn.net%2Fqna%2Fimage%2F4b035cdf8372d67108f7e8d339660479dfb41bbd") {
Lee SeoYeon's avatar
merge    
Lee SeoYeon committed
40
        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
41

Lee SeoYeon's avatar
merge    
Lee SeoYeon committed
42
43
44
45
46
47
48
49
50
51
        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
                req.places.times.push(new Date().toLocaleString())
                const newPlaces = await new Places(req.places).save()
                next()
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
52

Lee SeoYeon's avatar
merge    
Lee SeoYeon committed
53
            })
54
55
    } else {
        console.log("이미지 생성 오류발생!!")
Lee SeoYeon's avatar
merge    
Lee SeoYeon committed
56
    }
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
57

Lee SeoYeon's avatar
merge    
Lee SeoYeon committed
58
}
baesangjune's avatar
.    
baesangjune committed
59
const searchAssociation = async (req, res) => {
Lee SeoYeon's avatar
merge    
Lee SeoYeon committed
60
61
62
    let Place3 = req.places.address
    if (!Place3) {
        res.send({ error: "Place.address is null" })
Lee SeoYeon's avatar
..    
Lee SeoYeon committed
63
64
    }
    else {
Lee SeoYeon's avatar
merge    
Lee SeoYeon committed
65
        let addresse = Place3.split(' ')[0]
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
66
        let addressPlaces = new RegExp(`${addresse}`)
67
        let responsePlaces = await Places.find({ address: addressPlaces }).sort({ updatedAt: -1 })
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
68
69
        res.send(responsePlaces)
    }
baesangjune's avatar
.    
baesangjune committed
70
71
}

baesangjune's avatar
.    
baesangjune committed
72

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