Commit eba09f3a authored by Kim, Chaerin's avatar Kim, Chaerin
Browse files

.

parent b4b32464
...@@ -9,7 +9,9 @@ function Place(props) { ...@@ -9,7 +9,9 @@ function Place(props) {
axios({ url: `/api/review?keyword=${props.place.name}`, method: 'post', data: { db: db } }) axios({ url: `/api/review?keyword=${props.place.name}`, method: 'post', data: { db: db } })
.then(res => { .then(res => {
console.log("place res.data", res.data) console.log("place res.data", res.data)
setReviews(res.data) setReviews(res.data.review)
setDb(res.data.db)
console.log(db)
}) })
.catch(err => { .catch(err => {
console.log(err) console.log(err)
......
...@@ -17,7 +17,6 @@ function Search(props) { ...@@ -17,7 +17,6 @@ function Search(props) {
const [place, setPlace] = useState([{ name: "", category: "", address: "" }]) const [place, setPlace] = useState([{ name: "", category: "", address: "" }])
const [imgUrl, setImgUrl] = useState([]) const [imgUrl, setImgUrl] = useState([])
const [association, setAssociation] = useState([]) const [association, setAssociation] = useState([])
const getImg = () => { const getImg = () => {
axios.get(`/api/search/imges?keyword=${search}`) axios.get(`/api/search/imges?keyword=${search}`)
.then(res => { .then(res => {
...@@ -57,6 +56,9 @@ function Search(props) { ...@@ -57,6 +56,9 @@ function Search(props) {
getPlace() getPlace()
getImg() getImg()
getAssociation() getAssociation()
getPlace()
getImg()
if (window.innerWidth < 960) { if (window.innerWidth < 960) {
setMobile(true) setMobile(true)
} else { } else {
...@@ -64,11 +66,16 @@ function Search(props) { ...@@ -64,11 +66,16 @@ function Search(props) {
} }
if (state) { if (state) {
// window.location.reload()
// return <Redirect to={{
// pathname: `/search?keyword=${search}`,
// state: { id: search },
// }} />;
props.history.push('/search?keyword=' + search) props.history.push('/search?keyword=' + search)
setState(false) // setState(false)
console.log("search야", search) // console.log("search야", search)
} }
}, [search]); }, [state]);
const places = [{ const places = [{
...@@ -114,7 +121,7 @@ function Search(props) { ...@@ -114,7 +121,7 @@ function Search(props) {
} }
const handleSubmit = (e) => { const handleSubmit = (e) => {
// e.preventDefault() e.preventDefault()
setState(true); setState(true);
} }
......
...@@ -4,91 +4,67 @@ import axios from 'axios'; ...@@ -4,91 +4,67 @@ import axios from 'axios';
const search = async (req, res, next) => { const search = async (req, res, next) => {
console.log("1") console.log("1")
for (let i = 1; (req.reviews).length > 10; i++) { try {
try { for (let j = 1; j < 2; j++) {
console.log("2")
let reviews = [] let reviews = []
let content = [] // console.log(req.reviews, "제발")/
const url = "https://www.google.com/search?q=" + encodeURI(req.query.keyword) + "+site%3Atistory.com&page_no=" + i let url = "https://www.google.com/search?q=" + encodeURI(req.query.keyword) + "+site%3Atistory.com&page_no=" + j
const response1 = await axios.get(url) let response1 = await axios.get(url)
// console.log(response1.data) let $1 = cheerio.load(response1.data);
const $1 = cheerio.load(response1.data);
$1('.kCrYT').each(async function (i) { $1('.kCrYT').each(async function (i) {
console.log("3") console.log("3")
const title = $1(this).find('h3').text() let title = $1(this).find('h3').text()
const searchParams = new URLSearchParams($1(this).find('a').attr('href')); let searchParams = new URLSearchParams($1(this).find('a').attr('href'));
const link = searchParams.get("/url?q") let link = searchParams.get("/url?q")
const summary = $1(this).find('.s3v9rd').find('.s3v9rd').text() let summary = $1(this).find('.s3v9rd').find('.s3v9rd').text()
Review.find({ link: link }, function (err, review) { if (title) {
if (!review) { reviews[i] = { title: title, link: link }
content.push(getReview(link)) } else if (summary) {
if (title) { reviews[i - 1] = { ...reviews[i - 1], summary: summary }
reviews[i] = { title: title, link: link } reviews = reviews.filter(e => e)
} else if (summary) { }
reviews[i - 1] = { ...reviews[i - 1], summary: summary } })
reviews = reviews.filter(e => e) reviews.forEach((review, i) => {
} Review.findOne({ link: review.link }, async function (err, reviewss) {
console.log("4")
if (err) { return res.status(404).send({ error: err }) }
if (reviewss) {
reviewss.keyword = [...reviewss.keyword, req.query.keyword]
reviewss.save()
}
if (!reviewss) {
reviews["keyword"] = [req.query.keyword]
let Reviews = await new Review(review).save()
// console.log(Reviews,"1")
req.reviews.push(Reviews)
} }
}) })
}) })
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)
} }
const a = await Promise.all(req.reviews)
console.log(a, "2")
res.status(200).send({ review: a, db: true })
} catch (error) {
console.log("6")
console.log(error)
} }
} }
//***************네이버 크롤링 할 때 ********************* */
// try {
// let reviews = []
// let content = []
// const url = "https://search.naver.com/search.naver?where=view&sm=tab_jum&query=" + encodeURI('한라산')
// const response1 = await axios.get(url)
// // console.log(response1.data)
// const $1 = cheerio.load(response1.data);
// $1('._list').find('.total_wrap').each(async function (i) {
// reviews[i] = { name: $1(this).find('.total_tit').text(), summery: $1(this).find('.dsc_txt').text(), link: $1(this).find('.total_tit').attr('href') }
// })
// res.send(reviews)
// } catch (error) {
// console.log(error)
// res.send(error)
// }
// }
const getReview = async (link) => {
if (link) {
let content = '없음'
const res = await axios.get(link)
const $2 = cheerio.load(res.data);
if ($2('.tt_article_useless_p_margin').text()) {
content = $2('.tt_article_useless_p_margin').text()
}
return content
}
}
const find = (req, res, next) => { const find = (req, res, next) => {
console.log("7", req.body) console.log("7", req.query.keyword)
const keyword = req.query.keyword
if (req.body.db) { if (req.body.db) {
next() next()
} }
Review.find({ keyword:{ $elemMatch: res.query.keyword } }, function (err, reviews) { Review.find({ keyword: keyword }, function (err, reviews) {
req.reviews = []
if (reviews) { if (reviews) {
if (reviews.length > 10) if (reviews.length > 10) {
res.send(reviews, { db: true }) res.status(200).send({ review: reviews, db: true })
}
req.reviews = reviews req.reviews = reviews
} }
next() next()
......
...@@ -15,9 +15,6 @@ const ReviewSchema = new mongoose.Schema({ ...@@ -15,9 +15,6 @@ const ReviewSchema = new mongoose.Schema({
type: String, type: String,
// required: true, // required: true,
}, },
content: {
type: String,
},
keyword: { keyword: {
type: Array, type: Array,
// required: true, // required: true,
......
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