place.controller.js 3.11 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
    const url = "https://search.naver.com/search.naver?sm=tab_hty.top&where=nexearch&query=" + encodeURI(req.query.keyword)
baesangjune's avatar
.    
baesangjune committed
9
10
    const editUrl = /(http(s)?:\/\/)([a-z0-9\w]+\.*)+[a-z0-9]{2,4}/gi
    axios.get(url)
baesangjune's avatar
.    
baesangjune committed
11
        .then(async (response) => {
baesangjune's avatar
.    
baesangjune committed
12
            const html = response.data
baesangjune's avatar
.    
baesangjune committed
13
14
            // console.log(html)
            fs.writeFileSync("googleReview", html, { encoding: 'utf-8' })
baesangjune's avatar
.    
baesangjune committed
15
            let $1 = cheerio.load(html);
baesangjune's avatar
baesangjune committed
16

baesangjune's avatar
.    
baesangjune committed
17
18
            let places = {}
            $1('.ct_box_area').each(function (i) {
baesangjune's avatar
.    
baesangjune committed
19
20


baesangjune's avatar
.    
baesangjune committed
21
22
23
24
25
                // 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
26

baesangjune's avatar
.    
baesangjune committed
27
                places[i] = { name: $1('.biz_name').text(), category: $1('.category').text(), address: $1('.addr').text() }
baesangjune's avatar
.    
baesangjune committed
28
            })
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
29
        })
baesangjune's avatar
.    
baesangjune committed
30
31
32
}

const searchImg = async (req, res) => {
Kim, Chaerin's avatar
Kim, Chaerin committed
33
    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
34

baesangjune's avatar
.    
baesangjune committed
35
    axios.get(imgUrl)
baesangjune's avatar
.    
baesangjune committed
36
        .then(async (response) => {
baesangjune's avatar
.    
baesangjune committed
37
            const html = response.data
Kim, Chaerin's avatar
Kim, Chaerin committed
38
            let name = req.query.keyword
baesangjune's avatar
.    
baesangjune committed
39
40
            let $1 = cheerio.load(html);

Lee SeoYeon's avatar
.    
Lee SeoYeon committed
41
42
43
44
45
            axios.get(imgUrl)
                .then(async (response) => {
                    const html = response.data
                    let name = req.params.search
                    let $1 = cheerio.load(html);
baesangjune's avatar
.    
baesangjune committed
46

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

Lee SeoYeon's avatar
.    
Lee SeoYeon committed
49
50
51
52
                    //사진만 업데이트
                    let Place = await Places.findOne({ name: req.params.search })
                    Place.times.push(new Date().toLocaleString())
                    await Places.updateOne({ name: req.params.search }, { img: images, times: Place.times })
baesangjune's avatar
baesangjune committed
53

Lee SeoYeon's avatar
.    
Lee SeoYeon committed
54
55
                    const newPlaces = await new Places(
                    ).save()
baesangjune's avatar
baesangjune committed
56

Lee SeoYeon's avatar
.    
Lee SeoYeon committed
57
58
                    res.send(images)
                })
baesangjune's avatar
.    
baesangjune committed
59
60

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

baesangjune's avatar
.    
baesangjune committed
65

baesangjune's avatar
.    
baesangjune committed
66

Lee SeoYeon's avatar
.    
Lee SeoYeon committed
67
        })
baesangjune's avatar
.    
baesangjune committed
68

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

Lee SeoYeon's avatar
.    
Lee SeoYeon committed
72
73
74
75
        if (!Place) {
            res.send([])
        }
        let addresse = Place.address.split(' ')[0]
baesangjune's avatar
.    
baesangjune committed
76

Lee SeoYeon's avatar
.    
Lee SeoYeon committed
77
        // let AssociationsId = []
baesangjune's avatar
.    
baesangjune committed
78

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

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

baesangjune's avatar
.    
baesangjune committed
90

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