Commit 2ebe6356 authored by baesangjune's avatar baesangjune
Browse files

.

parent a72ca192
This diff is collapsed.
This diff is collapsed.
import Place from '../models/Place.js'
import cheerio from 'cheerio'
const signup = async (req, res) => {
const { name, email, password } = req.body
console.log(name, email, password)
try {
if (!isLength(name, { min: 3, max: 10 })) {
return res.status(422).send('Name must be 3-10 characters')
}
const newUser = await new User({
name,
email,
password
}).save()
console.log(newUser)
res.json(newUser)
} catch (error) {
console.log(error)
res.status(500).send('User signup error')
}
}
const search = async (req, res) => {
// 정보들 크롤링 해오고 아래에 넣어주기
const url = "https://section.blog.naver.com/Search/Post.nhn?keyword=" + keyword
request(url, function (err, res, html) { // URL로부터 가져온 페이지 소스가 html이란 변수에 담긴다.
if (!err) {
var $ = cheerio.load(html);
// 블로그 title 정보 가져오기
$(".entry-title > a").each(function () {
var post = { "name": "", "address": "", "img": "" };
var data = $(this);
post["title"] = data.text();
post["link"] = data.attr("href");
});
}
})
// try {
// const newPlace = await new Place({
// name: req.params.search,
// address,
// img,
// })
// }
}
export default { signup, search }
...@@ -3,62 +3,48 @@ import cheerio from "cheerio"; ...@@ -3,62 +3,48 @@ import cheerio from "cheerio";
import express from 'express'; import express from 'express';
import request from 'request-promise' import request from 'request-promise'
import jschardet from 'jschardet' import jschardet from 'jschardet'
// import iconv from 'iconv' import iconv from 'iconv'
import fs from 'fs' import fs from 'fs'
import axios from 'axios'; const Iconv = iconv.Iconv
import { CLIENT_RENEG_LIMIT } from 'tls';
// const Iconv = iconv.Iconv
const signup = async (req, res) => { const signup = async (req, res) => {
res.send("안녕하세요") res.send("안녕하세요")
} }
const search = async (req, res) => { const search = async (req, res) => {
// console.log(req.params.search) const url = "https://www.google.com/search?q=" + encodeURI(req.params.search) + "+site%3Atistory.com" + '&page_no=1'
// console.log("req", req)
// const url = "https://www.google.com/search?q=%ED%95%9C%EB%9D%BC%EC%82%B0%20site%3Atistory.com&oq=tistory&aqs=chrome..69i57j0l4j69i60l3.1746j0j4&sourceid=chrome&ie=UTF-8&ved=2ahUKEwis_bSFz4buAhWVdXAKHU0tBaoQ2wF6BAgIEAE&ei=T1D1X-yZD5XrwQPN2pTQCg"
const url = "https://www.google.com/search?q=%ED%95%9C%EB%9D%BC%EC%82%B0%20site%3Atistory.com"
axios.get(url).then(html => { request(url)
// console.log(response.data) .then(anyToUtf8)
fs.writeFile('google.txt', html.data, 'utf8', (err) => console.log(err)) .then((html) => {
let $ = cheerio.load(html, null, false); // fs.writeFileSync("googlez.txt", '\ufeff' + html, { encoding: 'utf8' });
let $ = cheerio.load(html, null, false);
let places = [] let places = []
$('.kCrYT').each(function (i) { $('.kCrYT').each(function (i) {
const review = await Review.findOne({ link: $(this).find('a').attr('href') })
if (!user) {
const newReview = await new Review({
name,
email,
password: hash
}).save()
}
places[i] = { places[i] = {
title: $(this).find('h3').text(), title: $(this).find('h3').text(),
link: $(this).find('a').attr('href'), link: $(this).find('a').attr('href'),
summary: $(this).find('.s3v9rd ').text(), summary: $(this).find('.s3v9rd').text(),
} }
}) })
// console.log(places)
res.send(places)
})
// request(url)
// // .then(anyToUtf8)
// .then((html) => {
// // fs.writeFileSync("googlez.txt", '\ufeff' + html, { encoding: 'utf8' });
// let $ = cheerio.load(html, null, false);
// let places = []
// $('.kCrYT').each(function (i) {
// places[i] = {
// title: $(this).find('h3').text(),
// link: $(this).find('a').attr('href'),
// summary: $(this).find('.s3v9rd ').text(),
// }
// })
// // console.log(places)
// res.send(places)
// })
// function anyToUtf8(str) {
// const { encoding } = jschardet.detect(str);
// console.log("source encoding = " + encoding);
// const iconv = new Iconv(encoding, "utf-8//translit//ignore");
// return iconv.convert(str).toString();
// }
// console.log(places)
res.send(places)
})
function anyToUtf8(str) {
const { encoding } = jschardet.detect(str);
console.log("source encoding = " + encoding);
const iconv = new Iconv(encoding, "utf-8//translit//ignore");
return iconv.convert(str).toString();
}
// try { // try {
// const newPlace = await new Place({ // const newPlace = await new Place({
// name: req.params.search, // name: req.params.search,
...@@ -69,4 +55,6 @@ const search = async (req, res) => { ...@@ -69,4 +55,6 @@ const search = async (req, res) => {
// } // }
} }
export default { signup, search } const
\ No newline at end of file
export default { signup, search }
...@@ -29,4 +29,4 @@ const PlaceSchema = new mongoose.Schema({ ...@@ -29,4 +29,4 @@ const PlaceSchema = new mongoose.Schema({
timestamps: true timestamps: true
}) })
export default mongoose.models.Place || mongoose.model('Place', PlaceSchema) export default mongoose.models.Place || mongoose.model('Place', PlaceSchema)
\ No newline at end of file
...@@ -3,14 +3,17 @@ import mongoose from 'mongoose' ...@@ -3,14 +3,17 @@ import mongoose from 'mongoose'
const { String } = mongoose.Schema.Types const { String } = mongoose.Schema.Types
const ReviewSchema = new mongoose.Schema({ const ReviewSchema = new mongoose.Schema({
title: {
type: String,
},
link: { link: {
type: String, type: String,
required: true, required: true,
unique: true,
}, },
content: { summary: {
type: String, type: String,
required: true, required: true,
unique: true,
}, },
keyword: { keyword: {
type: Array, type: Array,
...@@ -24,4 +27,4 @@ const ReviewSchema = new mongoose.Schema({ ...@@ -24,4 +27,4 @@ const ReviewSchema = new mongoose.Schema({
timestamps: true timestamps: true
}) })
export default mongoose.models.Review || mongoose.model('Review', ReviewSchema) export default mongoose.models.Review || mongoose.model('Review', ReviewSchema)
\ No newline at end of file
...@@ -18,4 +18,4 @@ async function connectDb() { ...@@ -18,4 +18,4 @@ async function connectDb() {
connection.isConnected = db.connections[0].readyState connection.isConnected = db.connections[0].readyState
} }
export default connectDb export default connectDb
\ 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