서주원

implement crawling deck.codes

1 +const request=require('request')
2 +const iconv=require('iconv-lite')
3 +const charset=require('charset')
4 +
5 +exports.Crawl=(deckCode)=>{
6 + return new Promise((resolve,reject)=>{
7 + request({
8 + url:`https://deck.codes/${deckCode}`,
9 + encoding:null,
10 + method:'GET',
11 + timeout:10000,
12 + followRedirect:true,
13 + maxRedirects:10,
14 + },(err,res,body)=>{
15 + if(!err && res.statusCode===200){
16 + const enc=charset(res.headers,body)
17 + const decodedResult=iconv.decode(body,enc)
18 + resolve(decodedResult)
19 + }
20 + else console.log(`error : ${res.statusCode}`)
21 + })
22 + })
23 +}
24 +
25 +
26 +
27 +
28 +
29 +
30 +
1 const express=require('express') 1 const express=require('express')
2 const router=express.Router() 2 const router=express.Router()
3 3
4 +const newDeck=require('./newDeck')
5 +
6 +router.post('/newdeck',newDeck.NewDeck)
7 +
4 module.exports=router 8 module.exports=router
...\ No newline at end of file ...\ No newline at end of file
......
1 +const rp=require('request-promise')
2 +const mysql=require('../../database/mysql')
3 +const crawler=require('./crawler')
4 +const cheerio=require('cheerio')
5 +
6 +
7 +exports.NewDeck=(req,res)=>{
8 + const deckOwner=req.session.sid
9 + const deckTitle=req.body.deckTitle
10 + const deckClass='paladin'
11 + let deckCode=req.body.deckCode
12 + let cards=[]
13 +
14 + console.log(deckOwner,deckTitle,deckCode)
15 +
16 + const DataCheck=()=>{
17 + return new Promise((resolve,reject)=>{
18 + if(!deckTitle || !deckCode){
19 + return reject({
20 + code: 'request_body_error',
21 + message: 'request body is not defined'
22 + })
23 + }
24 + else resolve()
25 + })
26 + }
27 + const CrawlPage=()=>{
28 + return crawler.Crawl(deckCode)
29 + }
30 + const DeckCrawl=(result)=>{
31 + //console.log(result)
32 + const $=cheerio.load(result)
33 + let cardCosts=$('div.hs-tile-info').children('.hs-tile-info-left.mdc-list-item__start-detail')
34 + let cardNames=$('div.hs-tile-info').find('span').find('span')
35 + let cardNums=$('div.hs-tile-info').children('.hs-tile-info-right.mdc-list-item__end-detail')
36 +
37 + for(let i=0;i<cardNames.length;i++) {
38 + let cardCost=$(cardCosts[i]).text()
39 + let cardName=$(cardNames[i]).text()
40 + let cardNum=$(cardNums[i]).text()
41 + if(cardNum.trim()==='')
42 + cardNum='1'
43 + else
44 + cardNum=cardNum.trim()
45 + cards.push({cardCost: cardCost, cardName: cardName, cardNum: cardNum})
46 + }
47 + console.log(cards)
48 + }
49 +
50 + const AddDeck=()=>{
51 + mysql.getConnection((err,connection)=>{
52 + if (err) throw err
53 + connection.query(`insert into deck (deckOwner,deckTitle,deckClass,deckCode) values (\'${deckOwner}\',\'${deckTitle}\',\'${deckClass}\',\'${deckCode}\');`,(err,results,fields)=>{
54 + if (err) throw err
55 + connection.release()
56 + })
57 + })
58 + }
59 +
60 + DataCheck()
61 + .then(CrawlPage)
62 + .then(DeckCrawl)
63 + .then(AddDeck)
64 + .then(()=>{
65 + res.status(200).json({message:'Complete Adding Deck'})
66 + })
67 + .catch((err)=>{
68 + console.log(err)
69 + res.status(500).json(err || err.message)
70 + })
71 +}
...\ No newline at end of file ...\ No newline at end of file
1 const findById=require('../../database/user/findById') 1 const findById=require('../../database/user/findById')
2 -const mysql=require('../../mysql')
3 const bcrypt=require('bcrypt-nodejs') 2 const bcrypt=require('bcrypt-nodejs')
4 -const session=require('express-session')
5 -const app=require('express')()
6 -
7 -app.use(session({
8 - secret:'ambc@!vsmkv#!&*!#EDNAnsv#!$()_*#@',
9 - resave:false,
10 - saveUninitialized:true
11 -}))
12 -
13 3
14 exports.Login=(req,res)=>{ 4 exports.Login=(req,res)=>{
15 const userId=req.body.userId 5 const userId=req.body.userId
...@@ -17,9 +7,7 @@ exports.Login=(req,res)=>{ ...@@ -17,9 +7,7 @@ exports.Login=(req,res)=>{
17 7
18 const DataCheck=()=>{ 8 const DataCheck=()=>{
19 return new Promise((resolve,reject)=>{ 9 return new Promise((resolve,reject)=>{
20 - console.log('1')
21 if(!userId || !password){ 10 if(!userId || !password){
22 - console.log('1 err')
23 return reject({ 11 return reject({
24 code: 'request_body_error', 12 code: 'request_body_error',
25 message: 'request body is not defined' 13 message: 'request body is not defined'
...@@ -45,22 +33,19 @@ exports.Login=(req,res)=>{ ...@@ -45,22 +33,19 @@ exports.Login=(req,res)=>{
45 33
46 const PwCheck=(user)=>{ 34 const PwCheck=(user)=>{
47 if (user[0]==null){ 35 if (user[0]==null){
48 - console.log('2 err')
49 return Promise.reject({ 36 return Promise.reject({
50 code:'id_wrong', 37 code:'id_wrong',
51 message:'id wrong' 38 message:'id wrong'
52 }) 39 })
53 } 40 }
54 - console.log('3')
55 if(bcrypt.compareSync(password,user[0].password)){ 41 if(bcrypt.compareSync(password,user[0].password)){
56 - console.log(`3 success\nLogin : ${userId}`) 42 + console.log(`Login : ${userId}`)
57 req.session.sid=userId 43 req.session.sid=userId
58 req.session.save(()=>{ 44 req.session.save(()=>{
59 res.status(200).json({userId:userId}) 45 res.status(200).json({userId:userId})
60 }) 46 })
61 } 47 }
62 else{ 48 else{
63 - console.log('3 err')
64 return Promise.reject({ 49 return Promise.reject({
65 code:'pw_wrong', 50 code:'pw_wrong',
66 message:'pw wrong' 51 message:'pw wrong'
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
3 const express=require('express') 3 const express=require('express')
4 const session=require('express-session') 4 const session=require('express-session')
5 const findById=require('../../database/user/findById') 5 const findById=require('../../database/user/findById')
6 -const mysql=require('../../mysql') 6 +const mysql=require('../../database/mysql')
7 const bcrypt=require('bcrypt-nodejs') 7 const bcrypt=require('bcrypt-nodejs')
8 8
9 exports.SignUp=(req,res)=>{ 9 exports.SignUp=(req,res)=>{
...@@ -44,11 +44,9 @@ exports.SignUp=(req,res)=>{ ...@@ -44,11 +44,9 @@ exports.SignUp=(req,res)=>{
44 } 44 }
45 const hash=bcrypt.hashSync(password,bcrypt.genSaltSync(10),null) 45 const hash=bcrypt.hashSync(password,bcrypt.genSaltSync(10),null)
46 mysql.getConnection((err,connection)=>{ 46 mysql.getConnection((err,connection)=>{
47 - if(err) 47 + if(err) throw err
48 - throw err
49 connection.query(`insert into user (userId,password) values (\'${userId}\',\'${hash}\');`,(err,results,fields)=>{ 48 connection.query(`insert into user (userId,password) values (\'${userId}\',\'${hash}\');`,(err,results,fields)=>{
50 - if(err) 49 + if(err) throw err
51 - throw err
52 connection.release() 50 connection.release()
53 }) 51 })
54 }) 52 })
......
1 -const mysql=require('../../mysql') 1 +const mysql=require('../mysql')
2 2
3 exports.findById=(userId)=>{ 3 exports.findById=(userId)=>{
4 return new Promise((resolve,reject)=>{ 4 return new Promise((resolve,reject)=>{
......
...@@ -108,6 +108,16 @@ ...@@ -108,6 +108,16 @@
108 "qs": "6.5.2", 108 "qs": "6.5.2",
109 "raw-body": "2.3.3", 109 "raw-body": "2.3.3",
110 "type-is": "~1.6.16" 110 "type-is": "~1.6.16"
111 + },
112 + "dependencies": {
113 + "iconv-lite": {
114 + "version": "0.4.23",
115 + "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.23.tgz",
116 + "integrity": "sha512-neyTUVFtahjf0mB3dZT77u+8O0QB89jFdnBkd5P1JgYPbPaia3gXXOVL2fq8VyU2gMMD7SaN7QukTB/pmXYvDA==",
117 + "requires": {
118 + "safer-buffer": ">= 2.1.2 < 3"
119 + }
120 + }
111 } 121 }
112 }, 122 },
113 "boolbase": { 123 "boolbase": {
...@@ -125,6 +135,11 @@ ...@@ -125,6 +135,11 @@
125 "resolved": "https://registry.npmjs.org/caseless/-/caseless-0.12.0.tgz", 135 "resolved": "https://registry.npmjs.org/caseless/-/caseless-0.12.0.tgz",
126 "integrity": "sha1-G2gcIf+EAzyCZUMJBolCDRhxUdw=" 136 "integrity": "sha1-G2gcIf+EAzyCZUMJBolCDRhxUdw="
127 }, 137 },
138 + "charset": {
139 + "version": "1.0.1",
140 + "resolved": "https://registry.npmjs.org/charset/-/charset-1.0.1.tgz",
141 + "integrity": "sha512-6dVyOOYjpfFcL1Y4qChrAoQLRHvj2ziyhcm0QJlhOcAhykL/k1kTUPbeo+87MNRTRdk2OIIsIXbuF3x2wi5EXg=="
142 + },
128 "cheerio": { 143 "cheerio": {
129 "version": "1.0.0-rc.2", 144 "version": "1.0.0-rc.2",
130 "resolved": "https://registry.npmjs.org/cheerio/-/cheerio-1.0.0-rc.2.tgz", 145 "resolved": "https://registry.npmjs.org/cheerio/-/cheerio-1.0.0-rc.2.tgz",
...@@ -495,9 +510,9 @@ ...@@ -495,9 +510,9 @@
495 } 510 }
496 }, 511 },
497 "iconv-lite": { 512 "iconv-lite": {
498 - "version": "0.4.23", 513 + "version": "0.4.24",
499 - "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.23.tgz", 514 + "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.24.tgz",
500 - "integrity": "sha512-neyTUVFtahjf0mB3dZT77u+8O0QB89jFdnBkd5P1JgYPbPaia3gXXOVL2fq8VyU2gMMD7SaN7QukTB/pmXYvDA==", 515 + "integrity": "sha512-v3MXnZAcvnywkTUEZomIActle7RXXeedOR31wwl7VlyoXO4Qi9arvSenNQWne1TcRwhCL1HwLI21bEqdpj8/rA==",
501 "requires": { 516 "requires": {
502 "safer-buffer": ">= 2.1.2 < 3" 517 "safer-buffer": ">= 2.1.2 < 3"
503 } 518 }
...@@ -756,6 +771,16 @@ ...@@ -756,6 +771,16 @@
756 "http-errors": "1.6.3", 771 "http-errors": "1.6.3",
757 "iconv-lite": "0.4.23", 772 "iconv-lite": "0.4.23",
758 "unpipe": "1.0.0" 773 "unpipe": "1.0.0"
774 + },
775 + "dependencies": {
776 + "iconv-lite": {
777 + "version": "0.4.23",
778 + "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.23.tgz",
779 + "integrity": "sha512-neyTUVFtahjf0mB3dZT77u+8O0QB89jFdnBkd5P1JgYPbPaia3gXXOVL2fq8VyU2gMMD7SaN7QukTB/pmXYvDA==",
780 + "requires": {
781 + "safer-buffer": ">= 2.1.2 < 3"
782 + }
783 + }
759 } 784 }
760 }, 785 },
761 "readable-stream": { 786 "readable-stream": {
......
...@@ -15,11 +15,13 @@ ...@@ -15,11 +15,13 @@
15 "dependencies": { 15 "dependencies": {
16 "bcrypt-nodejs": "0.0.3", 16 "bcrypt-nodejs": "0.0.3",
17 "body-parser": "^1.18.3", 17 "body-parser": "^1.18.3",
18 + "charset": "^1.0.1",
18 "cheerio": "^1.0.0-rc.2", 19 "cheerio": "^1.0.0-rc.2",
19 "dotenv": "^6.1.0", 20 "dotenv": "^6.1.0",
20 "express": "^4.16.4", 21 "express": "^4.16.4",
21 "express-session": "^1.15.6", 22 "express-session": "^1.15.6",
22 "fs": "0.0.1-security", 23 "fs": "0.0.1-security",
24 + "iconv-lite": "^0.4.24",
23 "morgan": "^1.9.1", 25 "morgan": "^1.9.1",
24 "mysql": "^2.16.0", 26 "mysql": "^2.16.0",
25 "path": "^0.12.7", 27 "path": "^0.12.7",
......
...@@ -32,6 +32,30 @@ ...@@ -32,6 +32,30 @@
32 } 32 }
33 }) 33 })
34 }) 34 })
35 + $('#newDeckButton').click(function(){
36 + var data=new Object()
37 + data.deckTitle=$('#deckTitle').val()
38 + data.deckCode=$('#deckCode').val()
39 + const stringData=JSON.stringify(data)
40 + console.log(stringData)
41 + $.ajax({
42 + type:'POST',
43 + url:'/api/deck/newdeck',
44 + data:stringData,
45 + dataType:'JSON',
46 + contentType:'application/json; charset=utf-8',
47 + traditional:true,
48 + processdata:false,
49 + success:function(result){
50 + alert('덱 생성 성공!')
51 + window.location.href='/decklist'
52 + },
53 + error:function(result){
54 + alert(`덱 생성 실패!\nmessage:${result['message']}`)
55 + return false
56 + }
57 + })
58 + })
35 }) 59 })
36 </script> 60 </script>
37 </head> 61 </head>
...@@ -61,11 +85,13 @@ ...@@ -61,11 +85,13 @@
61 <div class="padding" style="width:100px"></div> 85 <div class="padding" style="width:100px"></div>
62 <div class="container" style="width:50%;"> 86 <div class="container" style="width:50%;">
63 <h3 class="form-signin-heading">덱 추가</h3> 87 <h3 class="form-signin-heading">덱 추가</h3>
88 + <form id="addDeckForm">
64 <label class="sr-only" for="deckTitle">덱 이름</label> 89 <label class="sr-only" for="deckTitle">덱 이름</label>
65 - <input class="form-control" id="deckTitle" autofocus="" required="" type="text" placeholder="덱 이름" name="userId"> 90 + <input class="form-control" id="deckTitle" autofocus="" required="" type="text" placeholder="덱 이름" name="deckTitle">
66 <label class="sr-only" for="deckCode">덱 코드</label> 91 <label class="sr-only" for="deckCode">덱 코드</label>
67 - <textarea class="form-control" id="deckCode" required="" placeholder="덱 코드" name="password" style="resize:none;height:150px;"></textarea> 92 + <textarea class="form-control" id="deckCode" required="" placeholder="덱 코드" style="resize:none;height:150px;" name="deckCode"></textarea>
68 - <input type="button" class="btn btn-primary btn-block" value="확인" /> 93 + </form>
94 + <input type="button" class="btn btn-primary btn-block" id="newDeckButton" value="확인" />
69 </div> 95 </div>
70 </div> 96 </div>
71 97
......