import Places from '../models/Place.js' import cheerio from 'cheerio' import fs from 'fs' import axios from 'axios'; import { nextTick } from 'process'; const searchPlace = async (req, res, next,) => { let DuplicateCheckPlace = await Places.findOne({ name: req.query.keyword }) req.places = DuplicateCheckPlace 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 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 } next() } const searchImg = async (req, res, next) => { if (req.places.img !== "https://t1.daumcdn.net/thumb/R600x0/?fname=http%3A%2F%2Ft1.daumcdn.net%2Fqna%2Fimage%2F4b035cdf8372d67108f7e8d339660479dfb41bbd") { req.places.times.push(new Date().toLocaleString()) const newPlaces = await new Places(req.places).save() next() } else if (req.places.img === "https://t1.daumcdn.net/thumb/R600x0/?fname=http%3A%2F%2Ft1.daumcdn.net%2Fqna%2Fimage%2F4b035cdf8372d67108f7e8d339660479dfb41bbd") { const imgUrl = "https://www.google.com/search?q=" + encodeURI(req.query.keyword) + "+site:tistory.com/&tbm=isch" 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() }) } else { console.log("이미지 생성 오류발생!!") } } const searchAssociation = async (req, res) => { let Place3 = req.places.address if (!Place3) { res.send({ error: "Place.address is null" }) } else { let addresse = Place3.split(' ')[0] let addressPlaces = new RegExp(`${addresse}`) let responsePlaces = await Places.find({ address: addressPlaces }).sort({ updatedAt: -1 }) res.send(responsePlaces) } } export default { searchImg, searchPlace, searchAssociation }