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

Kim, Chaerin's avatar
Kim, Chaerin committed
9
    const url = "https://search.naver.com/search.naver?sm=tab_hty.top&where=nexearch&query=" + encodeURI(req.query.keyword)
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
            res.send(places)
        })
}

const searchImg = async (req, res) => {
Kim, Chaerin's avatar
Kim, Chaerin committed
38
    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
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
Kim, Chaerin's avatar
Kim, Chaerin committed
43
            let name = req.query.keyword
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
            // 여기서 있는건 찾아와서 추가를 시켜야한다.

            //사진만 업데이트
Kim, Chaerin's avatar
Kim, Chaerin committed
58
            let Place = await Places.findOne({ name: req.query.keyword })
baesangjune's avatar
.    
baesangjune 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
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
const searchAssociation = async (req, res) => {
Kim, Chaerin's avatar
Kim, Chaerin committed
71
    let Place = await Places.findOne({ name: req.query.keyword })
baesangjune's avatar
.    
baesangjune committed
72

Kim, Chaerin's avatar
Kim, Chaerin committed
73
74
75
76
    if (!Place) {
        res.send([])
    }
    let addresse = Place.address.split(' ')[0]
baesangjune's avatar
.    
baesangjune committed
77
78
79

    // let AssociationsId = []

Kim, Chaerin's avatar
Kim, Chaerin committed
80
    let addressPlaces = new RegExp(`${addresse}`)
baesangjune's avatar
.    
baesangjune committed
81
82
    let responsePlaces = await Places.find({ address: addressPlaces })
    res.send(responsePlaces)
Kim, Chaerin's avatar
Kim, Chaerin committed
83

baesangjune's avatar
.    
baesangjune committed
84
85
86
87
88
89
90
    // responsePlaces.map(Association => {
    //     AssociationsId.push(Association._id)
    // })
    // console.log("Associations = ", Associations)
    // res.send(AssociationsId)
}

baesangjune's avatar
.    
baesangjune committed
91

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