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";
import express from 'express';
import request from 'request-promise'
import jschardet from 'jschardet'
// import iconv from 'iconv'
import iconv from 'iconv'
import fs from 'fs'
import axios from 'axios';
import { CLIENT_RENEG_LIMIT } from 'tls';
// const Iconv = iconv.Iconv
const Iconv = iconv.Iconv
const signup = async (req, res) => {
res.send("안녕하세요")
}
const search = async (req, res) => {
// console.log(req.params.search)
// 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"
const url = "https://www.google.com/search?q=" + encodeURI(req.params.search) + "+site%3Atistory.com" + '&page_no=1'
axios.get(url).then(html => {
// console.log(response.data)
fs.writeFile('google.txt', html.data, 'utf8', (err) => console.log(err))
let $ = cheerio.load(html, null, false);
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) {
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] = {
title: $(this).find('h3').text(),
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 {
// const newPlace = await new Place({
// name: req.params.search,
......@@ -69,4 +55,6 @@ const search = async (req, res) => {
// }
}
export default { signup, search }
\ No newline at end of file
const
export default { signup, search }
......@@ -29,4 +29,4 @@ const PlaceSchema = new mongoose.Schema({
timestamps: true
})
export default mongoose.models.Place || mongoose.model('Place', PlaceSchema)
\ No newline at end of file
export default mongoose.models.Place || mongoose.model('Place', PlaceSchema)
......@@ -3,14 +3,17 @@ import mongoose from 'mongoose'
const { String } = mongoose.Schema.Types
const ReviewSchema = new mongoose.Schema({
title: {
type: String,
},
link: {
type: String,
required: true,
unique: true,
},
content: {
summary: {
type: String,
required: true,
unique: true,
},
keyword: {
type: Array,
......@@ -24,4 +27,4 @@ const ReviewSchema = new mongoose.Schema({
timestamps: true
})
export default mongoose.models.Review || mongoose.model('Review', ReviewSchema)
\ No newline at end of file
export default mongoose.models.Review || mongoose.model('Review', ReviewSchema)
......@@ -18,4 +18,4 @@ async function connectDb() {
connection.isConnected = db.connections[0].readyState
}
export default connectDb
\ No newline at end of file
export default connectDb
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