Commit 7af468a7 authored by Lee SeoYeon's avatar Lee SeoYeon
Browse files

.

parent b8fb805c
...@@ -18,28 +18,31 @@ function Search(props) { ...@@ -18,28 +18,31 @@ function Search(props) {
const [imgUrl, setImgUrl] = useState([]) const [imgUrl, setImgUrl] = useState([])
const [association, setAssociation] = useState([]) const [association, setAssociation] = useState([])
const getImg = () => { // console.log("search###############", search)
axios.get(`/api/search/imges?keyword=${search}`) const getPlace = () => {
axios.get(`/api/search/places?keyword=${search}`)
.then(res => { .then(res => {
console.log("images=", res.data) console.log("places=", res.data)
setImgUrl(res.data) setPlace(res.data)
}) })
.catch(err => { .catch(err => {
console.log('search.images 에러 발생', err) console.log('search.places 에러 발생', err)
}) })
} }
const getPlace = () => { const getImg = () => {
axios.get(`/api/search/places?keyword=${search}`) axios.get(`/api/search/imges?keyword=${search}`)
.then(res => { .then(res => {
console.log("places=", res.data) console.log("images=", res.data)
setPlace(res.data) setImgUrl(res.data)
}) })
.catch(err => { .catch(err => {
console.log('search.places 에러 발생', err) console.log('search.images 에러 발생', err)
}) })
} }
const getAssociation = () => { const getAssociation = () => {
axios.get(`/api/search/association?keyword=${search}`) axios.get(`/api/search/association?keyword=${search}`)
.then(res => { .then(res => {
......
...@@ -37,14 +37,20 @@ const searchImg = async (req, res) => { ...@@ -37,14 +37,20 @@ const searchImg = async (req, res) => {
let DuplicateCheckImg = await Places.findOne({ name: req.query.keyword }) let DuplicateCheckImg = await Places.findOne({ name: req.query.keyword })
if (DuplicateCheckImg) {
if (DuplicateCheckImg.img !== "https://t1.daumcdn.net/thumb/R600x0/?fname=http%3A%2F%2Ft1.daumcdn.net%2Fqna%2Fimage%2F4b035cdf8372d67108f7e8d339660479dfb41bbd") { if (DuplicateCheckImg.img !== "https://t1.daumcdn.net/thumb/R600x0/?fname=http%3A%2F%2Ft1.daumcdn.net%2Fqna%2Fimage%2F4b035cdf8372d67108f7e8d339660479dfb41bbd") {
res.send(DuplicateCheckImg) res.send(DuplicateCheckImg)
console.log("333333333333333333333333333IMG@@@@@@@@@@@@@@@@@@@ 기존이미지줄력중") console.log("333333333333333333333333333IMG@@@@@@@@@@@@@@@@@@@ 기존이미지줄력중")
} }
else if (DuplicateCheckImg.img === "https://t1.daumcdn.net/thumb/R600x0/?fname=http%3A%2F%2Ft1.daumcdn.net%2Fqna%2Fimage%2F4b035cdf8372d67108f7e8d339660479dfb41bbd") { else if (DuplicateCheckImg.img === "https://t1.daumcdn.net/thumb/R600x0/?fname=http%3A%2F%2Ft1.daumcdn.net%2Fqna%2Fimage%2F4b035cdf8372d67108f7e8d339660479dfb41bbd") {
console.log("4444444444444444444444444444444444444444444444444") console.log("4444444444444444444444444444444444444444444444444")
const imgUrl = "https://www.google.com/search?q=" + encodeURI(req.query.keyword) + "+site:tistory.com/&sxsrf=ALeKk023Dv08KQDodRmpB5222lQuzw2Vaw:1610612821100&source=lnms&tbm=isch" const imgUrl = "https://www.google.com/search?q=" + encodeURI(req.query.keyword) + "+site:tistory.com/&sxsrf=ALeKk023Dv08KQDodRmpB5222lQuzw2Vaw:1610612821100&source=lnms&tbm=isch"
axios.get(imgUrl)
.then(async (response) => {
const html = response.data
let name = req.query.keyword
let $1 = cheerio.load(html);
axios.get(imgUrl) axios.get(imgUrl)
.then(async (response) => { .then(async (response) => {
...@@ -52,19 +58,15 @@ const searchImg = async (req, res) => { ...@@ -52,19 +58,15 @@ const searchImg = async (req, res) => {
let name = req.query.keyword let name = req.query.keyword
let $1 = cheerio.load(html); let $1 = cheerio.load(html);
let images = $1('.RAyV4b').find('img').attr('src') //사진만 업데이트
let Place = await Places.findOne({ name: req.query.keyword })
//사진만 업데이트 Place.times.push(new Date().toLocaleString())
let Place = await Places.findOne({ name: req.query.keyword }) await Places.updateOne({ name: req.query.keyword }, { img: images, times: Place.times })
Place.times.push(new Date().toLocaleString()) res.send(images)
await Places.updateOne({ name: req.query.keyword }, { img: images, times: Place.times }) })
res.send(images)
})
} else { } else {
console.log("IMG에러") console.log("IMG에러")
}
} }
...@@ -77,11 +79,12 @@ const searchAssociation = async (req, res) => { ...@@ -77,11 +79,12 @@ const searchAssociation = async (req, res) => {
// if (!Place) { // if (!Place) {
// res.send([]) // res.send([])
// } // }
if (!Place) { let addresse = Place.address.split(' ')[0]
res.status(404).send({ error: "Place.address is null" })
} let AssociationsId = []
else { let addressPlaces = new RegExp(`${addresse}`)
let addresse = Place.address.split(' ')[0] let responsePlaces = await Places.find({ address: addressPlaces })
res.send(responsePlaces)
let AssociationsId = [] let AssociationsId = []
let addressPlaces = new RegExp(`${addresse}`) let addressPlaces = new RegExp(`${addresse}`)
......
import Review from '../models/Review.js' import Review from '../models/Review.js'
import cheerio from "cheerio"; import cheerio from "cheerio";
import fs from 'fs'
import axios from 'axios'; import axios from 'axios';
const search = async (req, res, next) => { const search = async (req, res, next) => {
//**************************구글 크롤링 할 때************************/ console.log("1")
try { for (let i = 1; (req.reviews).length > 10; i++) {
let reviews = [] try {
let content = [] console.log("2")
// Review.find() let reviews = []
const url = "https://www.google.com/search?q=" + encodeURI(req.params.search) + "+site%3Atistory.com&page_no=1" let content = []
const response1 = await axios.get(url) const url = "https://www.google.com/search?q=" + encodeURI(req.query.keyword) + "+site%3Atistory.com&page_no=" + i
const response1 = await axios.get(url)
fs.writeFileSync("googleSearch", response1.data, { encoding: 'utf-8' }) // console.log(response1.data)
const $1 = cheerio.load(response1.data);
// console.log(response1.data) $1('.kCrYT').each(async function (i) {
const $1 = cheerio.load(response1.data); //cheerio? reponse1의 데이터를 로드하는거 console.log("3")
$1('.kCrYT').each(async function (i) { const title = $1(this).find('h3').text()
const title = $1(this).find('h3').text() //타이틀에 텍스트 안에 h3값을 찾는다? const searchParams = new URLSearchParams($1(this).find('a').attr('href'));
const searchParams = new URLSearchParams($1(this).find('a').attr('href')); const link = searchParams.get("/url?q")
const link = searchParams.get("/url?q") const summary = $1(this).find('.s3v9rd').find('.s3v9rd').text()
const summary = $1(this).find('.s3v9rd').find('.s3v9rd').text() Review.find({ link: link }, function (err, review) {
content.push(getReview(link)) if (!review) {
if (title) { //타이틀일때 리뷰에 [i] 를 넣었을 때 타이틀, 링크값 설정 content.push(getReview(link))
reviews[i] = { title: title, link: link } if (title) {
} else if (summary) { reviews[i] = { title: title, link: link }
reviews[i - 1] = { ...reviews[i - 1], summary: summary } } else if (summary) {
reviews = reviews.filter(e => e) reviews[i - 1] = { ...reviews[i - 1], summary: summary }
} reviews = reviews.filter(e => e)
}) }
const promiseReview = await Promise.all(content) }
reviews.forEach(async (review, i) => { })
review["content"] = promiseReview[i] })
}) let promiseReview = await Promise.all(content)
res.send(reviews) promiseReview = promiseReview.filter(e => typeof (e) === 'string')
} catch (error) { reviews.forEach(async (review, i) => {
// console.log(error) console.log("4")
// res.send(error) review["content"] = promiseReview[i]
const reviews = new Review(review).save()
reviews.keyword.push(req.query.keyword)
req.reviews.push(reviews)
})
console.log("5")
res.send(reviews)
} catch (error) {
console.log("6")
console.log(error)
res.send(error)
}
} }
} }
...@@ -72,5 +82,19 @@ const getReview = async (link) => { ...@@ -72,5 +82,19 @@ const getReview = async (link) => {
return content return content
} }
export default { search, getReview } const find = (req, res, next) => {
res.send("안녕")
// console.log("7", req.body)
// if (req.body.db) {
// next()
// }
// Review.find({ keyword:{ $elemMatch: res.query.keyword } }, function (err, reviews) {
// if (reviews) {
// if (reviews.length > 10)
// res.send(reviews, { db: true })
// req.reviews = reviews
// }
// next()
// })
}
export default { search, find }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment