서주원

implement crawling deck.codes

const request=require('request')
const iconv=require('iconv-lite')
const charset=require('charset')
exports.Crawl=(deckCode)=>{
return new Promise((resolve,reject)=>{
request({
url:`https://deck.codes/${deckCode}`,
encoding:null,
method:'GET',
timeout:10000,
followRedirect:true,
maxRedirects:10,
},(err,res,body)=>{
if(!err && res.statusCode===200){
const enc=charset(res.headers,body)
const decodedResult=iconv.decode(body,enc)
resolve(decodedResult)
}
else console.log(`error : ${res.statusCode}`)
})
})
}
const express=require('express')
const router=express.Router()
const newDeck=require('./newDeck')
router.post('/newdeck',newDeck.NewDeck)
module.exports=router
\ No newline at end of file
......
const rp=require('request-promise')
const mysql=require('../../database/mysql')
const crawler=require('./crawler')
const cheerio=require('cheerio')
exports.NewDeck=(req,res)=>{
const deckOwner=req.session.sid
const deckTitle=req.body.deckTitle
const deckClass='paladin'
let deckCode=req.body.deckCode
let cards=[]
console.log(deckOwner,deckTitle,deckCode)
const DataCheck=()=>{
return new Promise((resolve,reject)=>{
if(!deckTitle || !deckCode){
return reject({
code: 'request_body_error',
message: 'request body is not defined'
})
}
else resolve()
})
}
const CrawlPage=()=>{
return crawler.Crawl(deckCode)
}
const DeckCrawl=(result)=>{
//console.log(result)
const $=cheerio.load(result)
let cardCosts=$('div.hs-tile-info').children('.hs-tile-info-left.mdc-list-item__start-detail')
let cardNames=$('div.hs-tile-info').find('span').find('span')
let cardNums=$('div.hs-tile-info').children('.hs-tile-info-right.mdc-list-item__end-detail')
for(let i=0;i<cardNames.length;i++) {
let cardCost=$(cardCosts[i]).text()
let cardName=$(cardNames[i]).text()
let cardNum=$(cardNums[i]).text()
if(cardNum.trim()==='')
cardNum='1'
else
cardNum=cardNum.trim()
cards.push({cardCost: cardCost, cardName: cardName, cardNum: cardNum})
}
console.log(cards)
}
const AddDeck=()=>{
mysql.getConnection((err,connection)=>{
if (err) throw err
connection.query(`insert into deck (deckOwner,deckTitle,deckClass,deckCode) values (\'${deckOwner}\',\'${deckTitle}\',\'${deckClass}\',\'${deckCode}\');`,(err,results,fields)=>{
if (err) throw err
connection.release()
})
})
}
DataCheck()
.then(CrawlPage)
.then(DeckCrawl)
.then(AddDeck)
.then(()=>{
res.status(200).json({message:'Complete Adding Deck'})
})
.catch((err)=>{
console.log(err)
res.status(500).json(err || err.message)
})
}
\ No newline at end of file
const findById=require('../../database/user/findById')
const mysql=require('../../mysql')
const bcrypt=require('bcrypt-nodejs')
const session=require('express-session')
const app=require('express')()
app.use(session({
secret:'ambc@!vsmkv#!&*!#EDNAnsv#!$()_*#@',
resave:false,
saveUninitialized:true
}))
exports.Login=(req,res)=>{
const userId=req.body.userId
......@@ -17,9 +7,7 @@ exports.Login=(req,res)=>{
const DataCheck=()=>{
return new Promise((resolve,reject)=>{
console.log('1')
if(!userId || !password){
console.log('1 err')
return reject({
code: 'request_body_error',
message: 'request body is not defined'
......@@ -45,22 +33,19 @@ exports.Login=(req,res)=>{
const PwCheck=(user)=>{
if (user[0]==null){
console.log('2 err')
return Promise.reject({
code:'id_wrong',
message:'id wrong'
})
}
console.log('3')
if(bcrypt.compareSync(password,user[0].password)){
console.log(`3 success\nLogin : ${userId}`)
console.log(`Login : ${userId}`)
req.session.sid=userId
req.session.save(()=>{
res.status(200).json({userId:userId})
})
}
else{
console.log('3 err')
return Promise.reject({
code:'pw_wrong',
message:'pw wrong'
......
......@@ -3,7 +3,7 @@
const express=require('express')
const session=require('express-session')
const findById=require('../../database/user/findById')
const mysql=require('../../mysql')
const mysql=require('../../database/mysql')
const bcrypt=require('bcrypt-nodejs')
exports.SignUp=(req,res)=>{
......@@ -44,11 +44,9 @@ exports.SignUp=(req,res)=>{
}
const hash=bcrypt.hashSync(password,bcrypt.genSaltSync(10),null)
mysql.getConnection((err,connection)=>{
if(err)
throw err
if(err) throw err
connection.query(`insert into user (userId,password) values (\'${userId}\',\'${hash}\');`,(err,results,fields)=>{
if(err)
throw err
if(err) throw err
connection.release()
})
})
......
const mysql=require('../../mysql')
const mysql=require('../mysql')
exports.findById=(userId)=>{
return new Promise((resolve,reject)=>{
......
......@@ -108,6 +108,16 @@
"qs": "6.5.2",
"raw-body": "2.3.3",
"type-is": "~1.6.16"
},
"dependencies": {
"iconv-lite": {
"version": "0.4.23",
"resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.23.tgz",
"integrity": "sha512-neyTUVFtahjf0mB3dZT77u+8O0QB89jFdnBkd5P1JgYPbPaia3gXXOVL2fq8VyU2gMMD7SaN7QukTB/pmXYvDA==",
"requires": {
"safer-buffer": ">= 2.1.2 < 3"
}
}
}
},
"boolbase": {
......@@ -125,6 +135,11 @@
"resolved": "https://registry.npmjs.org/caseless/-/caseless-0.12.0.tgz",
"integrity": "sha1-G2gcIf+EAzyCZUMJBolCDRhxUdw="
},
"charset": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/charset/-/charset-1.0.1.tgz",
"integrity": "sha512-6dVyOOYjpfFcL1Y4qChrAoQLRHvj2ziyhcm0QJlhOcAhykL/k1kTUPbeo+87MNRTRdk2OIIsIXbuF3x2wi5EXg=="
},
"cheerio": {
"version": "1.0.0-rc.2",
"resolved": "https://registry.npmjs.org/cheerio/-/cheerio-1.0.0-rc.2.tgz",
......@@ -495,9 +510,9 @@
}
},
"iconv-lite": {
"version": "0.4.23",
"resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.23.tgz",
"integrity": "sha512-neyTUVFtahjf0mB3dZT77u+8O0QB89jFdnBkd5P1JgYPbPaia3gXXOVL2fq8VyU2gMMD7SaN7QukTB/pmXYvDA==",
"version": "0.4.24",
"resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.24.tgz",
"integrity": "sha512-v3MXnZAcvnywkTUEZomIActle7RXXeedOR31wwl7VlyoXO4Qi9arvSenNQWne1TcRwhCL1HwLI21bEqdpj8/rA==",
"requires": {
"safer-buffer": ">= 2.1.2 < 3"
}
......@@ -756,6 +771,16 @@
"http-errors": "1.6.3",
"iconv-lite": "0.4.23",
"unpipe": "1.0.0"
},
"dependencies": {
"iconv-lite": {
"version": "0.4.23",
"resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.23.tgz",
"integrity": "sha512-neyTUVFtahjf0mB3dZT77u+8O0QB89jFdnBkd5P1JgYPbPaia3gXXOVL2fq8VyU2gMMD7SaN7QukTB/pmXYvDA==",
"requires": {
"safer-buffer": ">= 2.1.2 < 3"
}
}
}
},
"readable-stream": {
......
......@@ -15,11 +15,13 @@
"dependencies": {
"bcrypt-nodejs": "0.0.3",
"body-parser": "^1.18.3",
"charset": "^1.0.1",
"cheerio": "^1.0.0-rc.2",
"dotenv": "^6.1.0",
"express": "^4.16.4",
"express-session": "^1.15.6",
"fs": "0.0.1-security",
"iconv-lite": "^0.4.24",
"morgan": "^1.9.1",
"mysql": "^2.16.0",
"path": "^0.12.7",
......
......@@ -32,6 +32,30 @@
}
})
})
$('#newDeckButton').click(function(){
var data=new Object()
data.deckTitle=$('#deckTitle').val()
data.deckCode=$('#deckCode').val()
const stringData=JSON.stringify(data)
console.log(stringData)
$.ajax({
type:'POST',
url:'/api/deck/newdeck',
data:stringData,
dataType:'JSON',
contentType:'application/json; charset=utf-8',
traditional:true,
processdata:false,
success:function(result){
alert('덱 생성 성공!')
window.location.href='/decklist'
},
error:function(result){
alert(`덱 생성 실패!\nmessage:${result['message']}`)
return false
}
})
})
})
</script>
</head>
......@@ -61,11 +85,13 @@
<div class="padding" style="width:100px"></div>
<div class="container" style="width:50%;">
<h3 class="form-signin-heading">덱 추가</h3>
<form id="addDeckForm">
<label class="sr-only" for="deckTitle">덱 이름</label>
<input class="form-control" id="deckTitle" autofocus="" required="" type="text" placeholder="덱 이름" name="userId">
<input class="form-control" id="deckTitle" autofocus="" required="" type="text" placeholder="덱 이름" name="deckTitle">
<label class="sr-only" for="deckCode">덱 코드</label>
<textarea class="form-control" id="deckCode" required="" placeholder="덱 코드" name="password" style="resize:none;height:150px;"></textarea>
<input type="button" class="btn btn-primary btn-block" value="확인" />
<textarea class="form-control" id="deckCode" required="" placeholder="덱 코드" style="resize:none;height:150px;" name="deckCode"></textarea>
</form>
<input type="button" class="btn btn-primary btn-block" id="newDeckButton" value="확인" />
</div>
</div>
......