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

.

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