서주원

implement crawlerMulligan.js and crawlerOpponent.js

1 +const puppeteer=require('puppeteer');
2 +const cheerio=require('cheerio');
3 +
4 +exports.getDecks=(cardIds)=>{
5 +
6 + let idInQuery=cardIds[0]
7 + for(let i=1;i<cardIds.length;i++){
8 + idInQuery+= '%2C'+cardIds[i]
9 + }
10 + const getContent=()=>{
11 + return new Promise((resolve,reject)=>{
12 + const asyncFunc=async ()=>{
13 + const browser=await puppeteer.launch()
14 + try{
15 + const page=await browser.newPage()
16 + await page.setViewport({width:1366,height:768})
17 + await page.goto(`https://hsreplay.net/decks/#timeRange=LAST_30_DAYS&includedCards=${idInQuery}`,{waitUntil: 'networkidle2'})
18 + const content=await page.content()
19 + browser.close()
20 + return content
21 + }
22 + catch(err)
23 + {
24 + console.log(err)
25 + browser.close()
26 + }
27 + }
28 + resolve(asyncFunc())
29 + })
30 + }
31 +
32 + const getDeckHref=(content)=>{
33 + const $=cheerio.load(content)
34 + let deck=$('#decks-container > div > main > div.deck-list > ul > li:nth-child(2)').find('a')
35 + const deckHref=$(deck).attr('href')
36 + return deckHref
37 + }
38 + const getDeckConetent=(href)=>{
39 + const asyncFunc=async ()=>{
40 + const browser=await puppeteer.launch()
41 + try{
42 + const page=await browser.newPage()
43 + await page.setViewport({width:1366,height:768})
44 + await page.goto(`https://hsreplay.net${href}?hl=ko`,{waitUntil: 'networkidle2'})
45 + const content=await page.content()
46 + browser.close()
47 + return content
48 + }
49 + catch(err)
50 + {
51 + console.log(err)
52 + browser.close()
53 + }
54 + }
55 + return asyncFunc()
56 + }
57 + const getMulligan=(content)=>{
58 + const $=cheerio.load(content)
59 + let cardNames=$('.card-name')
60 + let cardWinRates=$('.table-cell')
61 + let cards=[]
62 + for(let i=0;i<cardNames.length;i++){
63 + let cardName=$(cardNames[i]).text()
64 + let cardWinRate=$(cardWinRates[6*i]).text()
65 + cardWinRate=cardWinRate.replace('▼','')
66 + cardWinRate=cardWinRate.replace('▲','')
67 + cardWinRate=cardWinRate.replace('%','')
68 + cards.push({cardName:cardName,cardWinRate:cardWinRate})
69 + }
70 + cards.sort((a,b)=>{
71 + return a.cardWinRate<b.cardWinRate ? 1:-1
72 + })
73 + console.log(cards)
74 +
75 + }
76 + getContent()
77 + .then(getDeckHref)
78 + .then(getDeckConetent)
79 + .then(getMulligan)
80 +}
1 +const puppeteer=require('puppeteer');
2 +const cheerio=require('cheerio');
3 +
4 +exports.getDecks=(opponentClass)=>{
5 +
6 + const getContent=()=>{
7 + return new Promise((resolve,reject)=>{
8 + const asyncFunc=async ()=>{
9 + const browser=await puppeteer.launch()
10 + try{
11 + const page=await browser.newPage()
12 + await page.setViewport({width:1366,height:768})
13 + await page.goto(`https://hsreplay.net/decks/#timeRange=LAST_30_DAYS&playerClasses=${opponentClass}?hl=ko`,{waitUntil: 'networkidle2'})
14 + const content=await page.content()
15 + browser.close()
16 + return content
17 + }
18 + catch(err)
19 + {
20 + console.log(err)
21 + browser.close()
22 + }
23 + }
24 + resolve(asyncFunc())
25 + })
26 + }
27 +
28 + const getDeckInfo=(content)=>{
29 + const $=cheerio.load(content)
30 + let deckNames=$('.deck-name')
31 + let deckGames=$('.game-count')
32 + let decks=[]
33 + for(let i=0;i<3;i++){
34 + let deckName=$(deckNames[i]).text()
35 + let deckGame=$(deckGames[i]).text()
36 + decks.push({deckTitle:deckName,deckGame:deckGame})
37 + }
38 + console.log(decks)
39 + return decks
40 + }
41 + getContent()
42 + .then(getDeckInfo)
43 +}
1 +const mysql=require('../../database/mysql')
2 +
3 +exports.GetCardId=(deckId)=>{
4 + return new Promise((resolve,reject)=>{
5 + mysql.getConnection((err,connection)=>{
6 + if (err) throw err
7 + connection.query(`select cardId from card where deckId=\'${deckId}\'`,(err,results,fields)=>{
8 + if (err) throw err
9 + resolve(results)
10 + })
11 + connection.release()
12 + })
13 + })
14 +}
...\ No newline at end of file ...\ No newline at end of file
1 +const crawler=require('./crawlerMulligan')
2 +const getCardId=require('./getCardId')
3 +
4 +exports.GetMulligan=(req,res)=>{
5 + const deckId=req.session.deckId || 113
6 + const opponentClass=req.body.class || 'PALADIN'
7 + const DataCheck=()=>{
8 + return new Promise((resolve,reject)=>{
9 + if (!deckId || !opponentClass){
10 + return reject({
11 + code:'query_error',
12 + message:'query error',
13 + })
14 + }
15 + resolve()
16 + })
17 + }
18 + const GetCardId=()=>{
19 + return getCardId.GetCardId(deckId)
20 + }
21 + const CrawlerMulligan=(cardIds)=>{
22 + return crawler.getDecks(cardIds)
23 + }
24 + DataCheck()
25 + .then(GetCardId)
26 + .then(CrawlerMulligan)
27 + .then((cards)=>{
28 + res.status(200).json(cards)
29 + })
30 + .catch((err)=>{
31 + res.status(500).json(err)
32 + })
33 +}
...\ No newline at end of file ...\ No newline at end of file
1 +const request=require('request')
2 +const iconv=require('iconv-lite')
3 +const charset=require('charset')
4 +const crawler=require('./crawlerOpponent')
5 +
6 +exports.GetOpponent=(req,res)=>{
7 + const opponentClass=req.body.class
8 + const DataCheck=()=>{
9 + return new Promise((resolve,reject)=>{
10 + if(!opponentClass){
11 + return reject({
12 + code:'query_error',
13 + message:'query_error'
14 + })
15 + }
16 + resolve()
17 + })
18 + }
19 + const CralwerOpponent=()=>{
20 + return crawler.getDecks(opponentClass)
21 + }
22 + DataCheck
23 + .then(CralwerOpponent)
24 + .then((decks)=>{
25 + res.status(200).json(decks)
26 + })
27 + .catch((err)=>{
28 + res.status(500).json(err)
29 + })
30 +}
...\ No newline at end of file ...\ No newline at end of file
1 const express=require('express') 1 const express=require('express')
2 const router=express.Router() 2 const router=express.Router()
3 3
4 +const getMulligan=require('./getMulligan')
5 +
6 +router.post('/getmulligan',getMulligan.GetMulligan)
7 +
4 module.exports=router 8 module.exports=router
...\ No newline at end of file ...\ No newline at end of file
......
1 -const crawler=require('./crawler') 1 +const crawler=require('./crawlerDeckCodes')
2 const cheerio=require('cheerio') 2 const cheerio=require('cheerio')
3 const addCards=require('../card/addCards') 3 const addCards=require('../card/addCards')
4 const addDeck=require('../../database/deck/addDeck') 4 const addDeck=require('../../database/deck/addDeck')
......
...@@ -6,7 +6,7 @@ exports.GetDeckId=(deckOwner,deckTitle)=>{ ...@@ -6,7 +6,7 @@ exports.GetDeckId=(deckOwner,deckTitle)=>{
6 if (err) throw err 6 if (err) throw err
7 connection.query(`select * from deck where deckOwner=\'${deckOwner}\' and deckTitle=\'${deckTitle}\'`,(err,results,field)=>{ 7 connection.query(`select * from deck where deckOwner=\'${deckOwner}\' and deckTitle=\'${deckTitle}\'`,(err,results,field)=>{
8 if (err) throw err 8 if (err) throw err
9 - console.log('result in getDeckId'+results[0]) 9 + //console.log('result in getDeckId'+results[0])
10 connection.release() 10 connection.release()
11 resolve(results[0].id) 11 resolve(results[0].id)
12 }) 12 })
......
This diff is collapsed. Click to expand it.
...@@ -27,6 +27,7 @@ ...@@ -27,6 +27,7 @@
27 "mysql": "^2.16.0", 27 "mysql": "^2.16.0",
28 "mysql-apostrophe": "^1.0.8", 28 "mysql-apostrophe": "^1.0.8",
29 "path": "^0.12.7", 29 "path": "^0.12.7",
30 + "puppeteer": "^1.11.0",
30 "request": "^2.88.0", 31 "request": "^2.88.0",
31 "request-promise": "^4.2.2" 32 "request-promise": "^4.2.2"
32 } 33 }
......
...@@ -32,6 +32,9 @@ ...@@ -32,6 +32,9 @@
32 } 32 }
33 }) 33 })
34 }) 34 })
35 + $('#submitButton').click(function(){
36 +
37 + })
35 }) 38 })
36 </script> 39 </script>
37 </head> 40 </head>
...@@ -66,17 +69,17 @@ ...@@ -66,17 +69,17 @@
66 <div class="col-md-4"> 69 <div class="col-md-4">
67 <img src="https://d1u5p3l4wpay3k.cloudfront.net/hearthstone_gamepedia/c/c5/Garrosh_Hellscream%28635%29.png?version=bab934001bb784a94c59a47823d535a7" style="width:150px;height:200px;"/> 70 <img src="https://d1u5p3l4wpay3k.cloudfront.net/hearthstone_gamepedia/c/c5/Garrosh_Hellscream%28635%29.png?version=bab934001bb784a94c59a47823d535a7" style="width:150px;height:200px;"/>
68 <br> 71 <br>
69 - <input type="radio" name="checkOpponent" value="전사"/>전사 72 + <input type="radio" name="checkOpponent" value="WARRIOR"/>전사
70 </div> 73 </div>
71 <div class="col-md-4"> 74 <div class="col-md-4">
72 <img src="https://d1u5p3l4wpay3k.cloudfront.net/hearthstone_gamepedia/4/4b/Thrall%28319%29.png?version=adcee55715548b949a7d973c2fddbd95" style="width:150px;height:200px;"/> 75 <img src="https://d1u5p3l4wpay3k.cloudfront.net/hearthstone_gamepedia/4/4b/Thrall%28319%29.png?version=adcee55715548b949a7d973c2fddbd95" style="width:150px;height:200px;"/>
73 <br> 76 <br>
74 - <input type="radio" name="checkOpponent" value="주술사"/>주술사 77 + <input type="radio" name="checkOpponent" value="SHAMAN"/>주술사
75 </div> 78 </div>
76 <div class="col-md-4"> 79 <div class="col-md-4">
77 <img src="https://d1u5p3l4wpay3k.cloudfront.net/hearthstone_gamepedia/a/a4/Valeera_Sanguinar%282%29.png?version=84a816910b223169eb14cc93c20437b2" style="width:150px;height:200px;"/> 80 <img src="https://d1u5p3l4wpay3k.cloudfront.net/hearthstone_gamepedia/a/a4/Valeera_Sanguinar%282%29.png?version=84a816910b223169eb14cc93c20437b2" style="width:150px;height:200px;"/>
78 <br> 81 <br>
79 - <input type="radio" name="checkOpponent" value="도적"/>도적 82 + <input type="radio" name="checkOpponent" value="ROGUE"/>도적
80 </div> 83 </div>
81 </div> 84 </div>
82 <br> 85 <br>
...@@ -86,17 +89,17 @@ ...@@ -86,17 +89,17 @@
86 <div class="col-md-4"> 89 <div class="col-md-4">
87 <img src="https://d1u5p3l4wpay3k.cloudfront.net/hearthstone_gamepedia/4/4d/Uther_Lightbringer%28257%29.png?version=b45ade5ac3fdd2579160fe5d7b7c1b20" style="width:150px;height:200px;"/> 90 <img src="https://d1u5p3l4wpay3k.cloudfront.net/hearthstone_gamepedia/4/4d/Uther_Lightbringer%28257%29.png?version=b45ade5ac3fdd2579160fe5d7b7c1b20" style="width:150px;height:200px;"/>
88 <br> 91 <br>
89 - <input type="radio" name="checkOpponent" value="성기사"/>성기사 92 + <input type="radio" name="checkOpponent" value="PALADIN"/>성기사
90 </div> 93 </div>
91 <div class="col-md-4"> 94 <div class="col-md-4">
92 <img src="https://d1u5p3l4wpay3k.cloudfront.net/hearthstone_gamepedia/a/a0/Rexxar%28484%29.png?version=c21b57837db15d20cc814f2bf45682b6" style="width:150px;height:200px;"/> 95 <img src="https://d1u5p3l4wpay3k.cloudfront.net/hearthstone_gamepedia/a/a0/Rexxar%28484%29.png?version=c21b57837db15d20cc814f2bf45682b6" style="width:150px;height:200px;"/>
93 <br> 96 <br>
94 - <input type="radio" name="checkOpponent" value="사냥꾼"/>사냥꾼 97 + <input type="radio" name="checkOpponent" value="HUNTER"/>사냥꾼
95 </div> 98 </div>
96 <div class="col-md-4"> 99 <div class="col-md-4">
97 <img src="https://d1u5p3l4wpay3k.cloudfront.net/hearthstone_gamepedia/f/fa/Malfurion_Stormrage%28621%29.png?version=b3f5a40e33f33d32995f3becbdd7aa94" style="width:150px;height:200px;"/> 100 <img src="https://d1u5p3l4wpay3k.cloudfront.net/hearthstone_gamepedia/f/fa/Malfurion_Stormrage%28621%29.png?version=b3f5a40e33f33d32995f3becbdd7aa94" style="width:150px;height:200px;"/>
98 <br> 101 <br>
99 - <input type="radio" name="checkOpponent" value="드루이드"/>드루이드 102 + <input type="radio" name="checkOpponent" value="DRUID"/>드루이드
100 </div> 103 </div>
101 </div> 104 </div>
102 <br> 105 <br>
...@@ -106,17 +109,17 @@ ...@@ -106,17 +109,17 @@
106 <div class="col-md-4"> 109 <div class="col-md-4">
107 <img src="https://d1u5p3l4wpay3k.cloudfront.net/hearthstone_gamepedia/0/0a/Gul%27dan%28618%29.png?version=90f421585c6f2d493ba94e259a76190e" style="width:150px;height:200px;"/> 110 <img src="https://d1u5p3l4wpay3k.cloudfront.net/hearthstone_gamepedia/0/0a/Gul%27dan%28618%29.png?version=90f421585c6f2d493ba94e259a76190e" style="width:150px;height:200px;"/>
108 <br> 111 <br>
109 - <input type="radio" name="checkOpponent" value="흑마법사"/>흑마법사 112 + <input type="radio" name="checkOpponent" value="WARLOCK"/>흑마법사
110 </div> 113 </div>
111 <div class="col-md-4"> 114 <div class="col-md-4">
112 <img src="https://d1u5p3l4wpay3k.cloudfront.net/hearthstone_gamepedia/3/3c/Jaina_Proudmoore%28320%29.png?version=75868a59a53f90bce829edeb66126b73" style="width:150px;height:200px;"/> 115 <img src="https://d1u5p3l4wpay3k.cloudfront.net/hearthstone_gamepedia/3/3c/Jaina_Proudmoore%28320%29.png?version=75868a59a53f90bce829edeb66126b73" style="width:150px;height:200px;"/>
113 <br> 116 <br>
114 - <input type="radio" name="checkOpponent" value="마법사"/>마법사 117 + <input type="radio" name="checkOpponent" value="MAGE"/>마법사
115 </div> 118 </div>
116 <div class="col-md-4"> 119 <div class="col-md-4">
117 <img src="https://d1u5p3l4wpay3k.cloudfront.net/hearthstone_gamepedia/8/80/Anduin_Wrynn%28110%29.png?version=ba8ecc39b3fdd4a2ede72e046c434454" style="width:150px;height:200px;"/> 120 <img src="https://d1u5p3l4wpay3k.cloudfront.net/hearthstone_gamepedia/8/80/Anduin_Wrynn%28110%29.png?version=ba8ecc39b3fdd4a2ede72e046c434454" style="width:150px;height:200px;"/>
118 <br> 121 <br>
119 - <input type="radio" name="checkOpponent" value="사제"/>사제 122 + <input type="radio" name="checkOpponent" value="PRIEST"/>사제
120 </div> 123 </div>
121 </div> 124 </div>
122 <br> 125 <br>
...@@ -124,7 +127,7 @@ ...@@ -124,7 +127,7 @@
124 <div class="row"> 127 <div class="row">
125 <div class="col-md-5"></div> 128 <div class="col-md-5"></div>
126 <div class="col-md-2"> 129 <div class="col-md-2">
127 - <input class="btn btn-lg btn-primary btn-block" type="button" value="확인"/> 130 + <input class="btn btn-lg btn-primary btn-block" type="button" id="submitButton" value="확인"/>
128 </div> 131 </div>
129 <div class="col-md-5"></div> 132 <div class="col-md-5"></div>
130 </div> 133 </div>
......