Commit 11ea8e32 authored by Kim, Chaerin's avatar Kim, Chaerin
Browse files

쿼리스트링

parent 768b5382
...@@ -3,9 +3,10 @@ import React, { useEffect, useState } from 'react'; ...@@ -3,9 +3,10 @@ import React, { useEffect, useState } from 'react';
import { Modal, Container, Row, Col, Button, Badge, Card, Accordion, Carousel } from 'react-bootstrap'; import { Modal, Container, Row, Col, Button, Badge, Card, Accordion, Carousel } from 'react-bootstrap';
function Place(props) { function Place(props) {
const [reviews, setReviews] = useState([]) const [reviews, setReviews] = useState()
const [db, setDb] = useState(false)
const getReview = () => { const getReview = () => {
axios.get(`/api/review/${props.search.name}`) 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)
...@@ -29,12 +30,12 @@ function Place(props) { ...@@ -29,12 +30,12 @@ function Place(props) {
aria-labelledby="example-modal-sizes-title-lg"> aria-labelledby="example-modal-sizes-title-lg">
<Modal.Header closeButton> <Modal.Header closeButton>
<Modal.Title id="contained-modal-title-vcenter" style={{ fontSize: '40px' }}> <Modal.Title id="contained-modal-title-vcenter" style={{ fontSize: '40px' }}>
{props.index + 1}. {props.search.name} {props.index + 1}. {props.place.name}
</Modal.Title> </Modal.Title>
</Modal.Header> </Modal.Header>
<Modal.Body className="show-grid"> <Modal.Body className="show-grid">
<Container style={{ fontSize: '40px' }}> <Container style={{ fontSize: '40px' }}>
{reviews.map((review, index) => { {Array.isArray(reviews) ? reviews.map((review, index) => {
return ( return (
<Row className="mt-4"> <Row className="mt-4">
<a href={review.link}>{review.title}</a> <a href={review.link}>{review.title}</a>
...@@ -42,7 +43,8 @@ function Place(props) { ...@@ -42,7 +43,8 @@ function Place(props) {
<div>{review.content}</div> <div>{review.content}</div>
</Row> </Row>
) )
})} })
: "리뷰가 없습니다."}
</Container> </Container>
</Modal.Body> </Modal.Body>
......
...@@ -8,10 +8,7 @@ function App() { ...@@ -8,10 +8,7 @@ function App() {
const [search, setSearch] = useState(""); const [search, setSearch] = useState("");
if (state !== false) { if (state !== false) {
return <Redirect to={{ return <Redirect to={`/search?keyword=${search}`} />;
pathname: `/search/${search}`,
state: { id: search },
}} />;
} }
const handleChange = (e) => { const handleChange = (e) => {
......
...@@ -5,20 +5,20 @@ import Place from '../Components/Place'; ...@@ -5,20 +5,20 @@ import Place from '../Components/Place';
import { Container, Form, Row, Col, Card, Image, InputGroup, FormControl, Button, Pagination } from 'react-bootstrap'; import { Container, Form, Row, Col, Card, Image, InputGroup, FormControl, Button, Pagination } from 'react-bootstrap';
import Paginations from '../Components/Paginations'; import Paginations from '../Components/Paginations';
import axios from 'axios'; import axios from 'axios';
import queryString from 'query-string'
function Search(props) { function Search(props) {
const endPage = 10; const endPage = 10;
const [state, setState] = useState(false); const [state, setState] = useState(false);
const [index, setIndex] = useState(1); const [index, setIndex] = useState(1);
const [showSet, setShowSet] = useState([false, false, false, false]); const [showSet, setShowSet] = useState([false, false, false, false]);
const [search, setSearch] = useState(props.location.state.id); const [search, setSearch] = useState(queryString.parse(props.location.search).keyword);
const [mobile, setMobile] = useState(); const [mobile, setMobile] = useState();
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/${search}`) axios.get(`/api/search/imges?keyword=${search}`)
.then(res => { .then(res => {
console.log("images=", res.data) console.log("images=", res.data)
setImgUrl(res.data) setImgUrl(res.data)
...@@ -29,7 +29,7 @@ function Search(props) { ...@@ -29,7 +29,7 @@ function Search(props) {
} }
const getPlace = () => { const getPlace = () => {
axios.get(`/api/search/places/${search}`) axios.get(`/api/search/places?keyword=${search}`)
.then(res => { .then(res => {
console.log("places=", res.data) console.log("places=", res.data)
setPlace(res.data) setPlace(res.data)
...@@ -40,7 +40,7 @@ function Search(props) { ...@@ -40,7 +40,7 @@ function Search(props) {
} }
const getAssociation =() => { const getAssociation =() => {
axios.get(`/api/search/association/${search}`) axios.get(`/api/search/association?keyword=${search}`)
.then(res => { .then(res => {
console.log("Associations = ", res.data) console.log("Associations = ", res.data)
setAssociation(res.data) setAssociation(res.data)
...@@ -92,7 +92,7 @@ function Search(props) { ...@@ -92,7 +92,7 @@ function Search(props) {
if (state !== false) { if (state !== false) {
return <Redirect to={{ return <Redirect to={{
pathname: `/search/${search}`, pathname: `/search?keyword=${search}`,
state: { id: search }, state: { id: search },
}} />; }} />;
} }
......
...@@ -13,17 +13,13 @@ import { ...@@ -13,17 +13,13 @@ import {
Redirect, Redirect,
} from "react-router-dom"; } from "react-router-dom";
axios.defaults.validateStatus = function (status) {
return status < 500; // default
}
ReactDOM.render( ReactDOM.render(
<React.StrictMode> <React.StrictMode>
<Router> <Router>
<Switch> <Switch>
<Route exact path="/" component={App} /> <Route exact path="/" component={App} />
<Route path="/search" component={Search} /> <Route path="/search" component={Search} />
<Redirect path="/search/:search" to="/search" /> <Redirect path="/search" to="/search" />
</Switch> </Switch>
</Router> </Router>
</React.StrictMode>, </React.StrictMode>,
......
This source diff could not be displayed because it is too large. You can view the blob instead.
...@@ -6,7 +6,7 @@ import { time } from 'console'; ...@@ -6,7 +6,7 @@ import { time } from 'console';
const searchPlace = async (req, res) => { const searchPlace = async (req, res) => {
const url = "https://search.naver.com/search.naver?sm=tab_hty.top&where=nexearch&query=" + encodeURI(req.params.search) 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 editUrl = /(http(s)?:\/\/)([a-z0-9\w]+\.*)+[a-z0-9]{2,4}/gi
axios.get(url) axios.get(url)
.then(async (response) => { .then(async (response) => {
...@@ -35,12 +35,12 @@ const searchPlace = async (req, res) => { ...@@ -35,12 +35,12 @@ const searchPlace = async (req, res) => {
} }
const searchImg = async (req, res) => { const searchImg = async (req, res) => {
const imgUrl = "https://www.google.com/search?q=" + encodeURI(req.params.search) + "+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) axios.get(imgUrl)
.then(async (response) => { .then(async (response) => {
const html = response.data const html = response.data
let name = req.params.search let name = req.query.keyword
let $1 = cheerio.load(html); let $1 = cheerio.load(html);
let images = $1('.RAyV4b').find('img').attr('src') let images = $1('.RAyV4b').find('img').attr('src')
...@@ -55,9 +55,9 @@ const searchImg = async (req, res) => { ...@@ -55,9 +55,9 @@ const searchImg = async (req, res) => {
// 여기서 있는건 찾아와서 추가를 시켜야한다. // 여기서 있는건 찾아와서 추가를 시켜야한다.
//사진만 업데이트 //사진만 업데이트
let Place = await Places.findOne({ name: req.params.search }) let Place = await Places.findOne({ name: req.query.keyword })
Place.times.push(new Date().toLocaleString()) Place.times.push(new Date().toLocaleString())
await Places.updateOne({ name: req.params.search }, { img: images, times: Place.times }) await Places.updateOne({ name: req.query.keyword }, { img: images, times: Place.times })
const newPlaces = await new Places( const newPlaces = await new Places(
).save() ).save()
...@@ -68,15 +68,19 @@ const searchImg = async (req, res) => { ...@@ -68,15 +68,19 @@ const searchImg = async (req, res) => {
} }
const searchAssociation = async (req, res) => { const searchAssociation = async (req, res) => {
let Place = await Places.findOne({ name: req.params.search }) let Place = await Places.findOne({ name: req.query.keyword })
let address = Place.address.split(' ')[0]
if (!Place) {
res.send([])
}
let addresse = Place.address.split(' ')[0]
// let AssociationsId = [] // let AssociationsId = []
let addressPlaces = new RegExp(`${address}`) let addressPlaces = new RegExp(`${addresse}`)
let responsePlaces = await Places.find({ address: addressPlaces }) let responsePlaces = await Places.find({ address: addressPlaces })
res.send(responsePlaces) res.send(responsePlaces)
// responsePlaces.map(Association => { // responsePlaces.map(Association => {
// AssociationsId.push(Association._id) // AssociationsId.push(Association._id)
// }) // })
......
import Review from '../models/Review.js' import Review from '../models/Review.js'
import cheerio from "cheerio"; import cheerio from "cheerio";
import iconv from 'iconv'
import fs from 'fs'
import axios from 'axios'; import axios from 'axios';
const Iconv = iconv.Iconv
const search = async (req, res, next) => { const search = async (req, res, next) => {
try { console.log("1")
let reviews = [] for (let i = 1; (req.reviews).length > 10; i++) {
let content = [] try {
Review.find() console.log("2")
const url = "https://www.google.com/search?q=" + encodeURI(req.params.search) + "+site%3Atistory.com&page_no=1" let reviews = []
const response1 = await axios.get(url) let content = []
// console.log(response1.data) const url = "https://www.google.com/search?q=" + encodeURI(req.query.keyword) + "+site%3Atistory.com&page_no=" + i
const $1 = cheerio.load(response1.data); const response1 = await axios.get(url)
$1('.kCrYT').each(async function (i) { // console.log(response1.data)
const title = $1(this).find('h3').text() const $1 = cheerio.load(response1.data);
const searchParams = new URLSearchParams($1(this).find('a').attr('href')); $1('.kCrYT').each(async function (i) {
const link = searchParams.get("/url?q") console.log("3")
const summary = $1(this).find('.s3v9rd').find('.s3v9rd').text() const title = $1(this).find('h3').text()
content.push(getReview(link)) const searchParams = new URLSearchParams($1(this).find('a').attr('href'));
if (title) { const link = searchParams.get("/url?q")
reviews[i] = { title: title, link: link } const summary = $1(this).find('.s3v9rd').find('.s3v9rd').text()
} else if (summary) { Review.find({ link: link }, function (err, review) {
reviews[i - 1] = { ...reviews[i - 1], summary: summary } if (!review) {
reviews = reviews.filter(e => e) content.push(getReview(link))
} if (title) {
}) reviews[i] = { title: title, link: link }
let promiseReview = await Promise.all(content) } else if (summary) {
promiseReview = promiseReview.filter(e => typeof (e) === 'string') reviews[i - 1] = { ...reviews[i - 1], summary: summary }
reviews.forEach(async (review, i) => { reviews = reviews.filter(e => e)
review["content"] = promiseReview[i] }
const reviews = new Review(review).save() }
}) })
})
res.send(reviews) let promiseReview = await Promise.all(content)
} catch (error) { promiseReview = promiseReview.filter(e => typeof (e) === 'string')
console.log(error) reviews.forEach(async (review, i) => {
res.send(error) 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)
}
} }
} }
...@@ -71,8 +80,18 @@ const getReview = async (link) => { ...@@ -71,8 +80,18 @@ const getReview = async (link) => {
} }
} }
const find = (res,req,next) => { const find = (req, res, next) => {
Review.find({address: }) 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 } export default { search, find }
\ No newline at end of file
...@@ -3,13 +3,13 @@ import place from '../controllers/place.controller.js' ...@@ -3,13 +3,13 @@ import place from '../controllers/place.controller.js'
const router = express.Router() const router = express.Router()
router.route('/api/search/imges/:search') router.route('/api/search/imges')
.get(place.searchImg) .get(place.searchImg)
router.route('/api/search/places/:search') router.route('/api/search/places')
.get(place.searchPlace) .get(place.searchPlace)
router.route('/api/search/association/:search') router.route('/api/search/association')
.get(place.searchAssociation) .get(place.searchAssociation)
export default router export default router
\ No newline at end of file
...@@ -3,7 +3,7 @@ import review from '../controllers/review.controller.js' ...@@ -3,7 +3,7 @@ import review from '../controllers/review.controller.js'
const router = express.Router() const router = express.Router()
router.route('/api/review/:search') router.route('/api/review')
.get(review.search) .post(review.find, review.search)
export default router export default router
\ No newline at end of file
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