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

reviews담기 완료+ 블로그별 내용 보완 필요함.

parent aaaf8613
...@@ -8,26 +8,33 @@ import { nextTick } from 'process'; ...@@ -8,26 +8,33 @@ import { nextTick } from 'process';
const Iconv = iconv.Iconv const Iconv = iconv.Iconv
const search = async (req, res, next) => { const search = async (req, res, next) => {
// const url = "https://www.google.com/search?q=" + encodeURI(req.params.search) + "+site%3Atistory.com&page_no=1"
try { try {
const url = "https://www.google.com/search?q=kakao+site%3Atistory.com&page_no=1"
let reviews = [] let reviews = []
let content = []
const url = "https://www.google.com/search?q=" + encodeURI(req.params.search) + "+site%3Atistory.com&page_no=1"
const response1 = await axios.get(url) const response1 = await axios.get(url)
// console.log(response1.data)
const $1 = cheerio.load(response1.data); const $1 = cheerio.load(response1.data);
$1('.kCrYT').each(async function (i) { $1('.kCrYT').each(async function (i) {
const searchParams = new URLSearchParams($1(this).find('a').attr('href'));
const title = $1(this).find('h3').text() const title = $1(this).find('h3').text()
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()
const content = await getReview(link) content.push(getReview(title, link, summary))
if (title) { if (title) {
reviews[i] = { title: title, link: link, content: content } reviews[i] = { title: title, link: link }
} else if (summary) { } else if (summary) {
reviews[i - 1] = { ...reviews[i - 1], summary: summary } reviews[i - 1] = { ...reviews[i - 1], summary: summary }
reviews = reviews.filter(e => e) reviews = reviews.filter(e => e)
} }
// const review = new Review(reviews).save()
}) })
const promiseReview = await Promise.all(content)
reviews.forEach((review, i) => {
review["content"] = promiseReview[i]
})
// reviews = { ...reviews, ...promiseReview }
console.log("reviews", reviews)
// console.log(promiseReview)
res.send(reviews) res.send(reviews)
} catch (error) { } catch (error) {
console.log(error) console.log(error)
...@@ -35,14 +42,15 @@ const search = async (req, res, next) => { ...@@ -35,14 +42,15 @@ const search = async (req, res, next) => {
} }
} }
const getReview = async (link) => { const getReview = async (title, link, summary) => {
let content = '없음' let content = '없음'
if (link) {
const res = await axios.get(link) const res = await axios.get(link)
const $2 = cheerio.load(res.data); const $2 = cheerio.load(res.data);
if ($2('.tt_article_useless_p_margin').text()) { if ($2('.tt_article_useless_p_margin').text()) {
content = $2('.tt_article_useless_p_margin').text() content = $2('.tt_article_useless_p_margin').text()
} }
// console.log(content,"getReiview") }
return content return content
} }
......
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