서주원

implement crawlerMulligan.js and crawlerOpponent.js

const puppeteer=require('puppeteer');
const cheerio=require('cheerio');
exports.getDecks=(cardIds)=>{
let idInQuery=cardIds[0]
for(let i=1;i<cardIds.length;i++){
idInQuery+= '%2C'+cardIds[i]
}
const getContent=()=>{
return new Promise((resolve,reject)=>{
const asyncFunc=async ()=>{
const browser=await puppeteer.launch()
try{
const page=await browser.newPage()
await page.setViewport({width:1366,height:768})
await page.goto(`https://hsreplay.net/decks/#timeRange=LAST_30_DAYS&includedCards=${idInQuery}`,{waitUntil: 'networkidle2'})
const content=await page.content()
browser.close()
return content
}
catch(err)
{
console.log(err)
browser.close()
}
}
resolve(asyncFunc())
})
}
const getDeckHref=(content)=>{
const $=cheerio.load(content)
let deck=$('#decks-container > div > main > div.deck-list > ul > li:nth-child(2)').find('a')
const deckHref=$(deck).attr('href')
return deckHref
}
const getDeckConetent=(href)=>{
const asyncFunc=async ()=>{
const browser=await puppeteer.launch()
try{
const page=await browser.newPage()
await page.setViewport({width:1366,height:768})
await page.goto(`https://hsreplay.net${href}?hl=ko`,{waitUntil: 'networkidle2'})
const content=await page.content()
browser.close()
return content
}
catch(err)
{
console.log(err)
browser.close()
}
}
return asyncFunc()
}
const getMulligan=(content)=>{
const $=cheerio.load(content)
let cardNames=$('.card-name')
let cardWinRates=$('.table-cell')
let cards=[]
for(let i=0;i<cardNames.length;i++){
let cardName=$(cardNames[i]).text()
let cardWinRate=$(cardWinRates[6*i]).text()
cardWinRate=cardWinRate.replace('▼','')
cardWinRate=cardWinRate.replace('▲','')
cardWinRate=cardWinRate.replace('%','')
cards.push({cardName:cardName,cardWinRate:cardWinRate})
}
cards.sort((a,b)=>{
return a.cardWinRate<b.cardWinRate ? 1:-1
})
console.log(cards)
}
getContent()
.then(getDeckHref)
.then(getDeckConetent)
.then(getMulligan)
}
const puppeteer=require('puppeteer');
const cheerio=require('cheerio');
exports.getDecks=(opponentClass)=>{
const getContent=()=>{
return new Promise((resolve,reject)=>{
const asyncFunc=async ()=>{
const browser=await puppeteer.launch()
try{
const page=await browser.newPage()
await page.setViewport({width:1366,height:768})
await page.goto(`https://hsreplay.net/decks/#timeRange=LAST_30_DAYS&playerClasses=${opponentClass}?hl=ko`,{waitUntil: 'networkidle2'})
const content=await page.content()
browser.close()
return content
}
catch(err)
{
console.log(err)
browser.close()
}
}
resolve(asyncFunc())
})
}
const getDeckInfo=(content)=>{
const $=cheerio.load(content)
let deckNames=$('.deck-name')
let deckGames=$('.game-count')
let decks=[]
for(let i=0;i<3;i++){
let deckName=$(deckNames[i]).text()
let deckGame=$(deckGames[i]).text()
decks.push({deckTitle:deckName,deckGame:deckGame})
}
console.log(decks)
return decks
}
getContent()
.then(getDeckInfo)
}
const mysql=require('../../database/mysql')
exports.GetCardId=(deckId)=>{
return new Promise((resolve,reject)=>{
mysql.getConnection((err,connection)=>{
if (err) throw err
connection.query(`select cardId from card where deckId=\'${deckId}\'`,(err,results,fields)=>{
if (err) throw err
resolve(results)
})
connection.release()
})
})
}
\ No newline at end of file
const crawler=require('./crawlerMulligan')
const getCardId=require('./getCardId')
exports.GetMulligan=(req,res)=>{
const deckId=req.session.deckId || 113
const opponentClass=req.body.class || 'PALADIN'
const DataCheck=()=>{
return new Promise((resolve,reject)=>{
if (!deckId || !opponentClass){
return reject({
code:'query_error',
message:'query error',
})
}
resolve()
})
}
const GetCardId=()=>{
return getCardId.GetCardId(deckId)
}
const CrawlerMulligan=(cardIds)=>{
return crawler.getDecks(cardIds)
}
DataCheck()
.then(GetCardId)
.then(CrawlerMulligan)
.then((cards)=>{
res.status(200).json(cards)
})
.catch((err)=>{
res.status(500).json(err)
})
}
\ No newline at end of file
const request=require('request')
const iconv=require('iconv-lite')
const charset=require('charset')
const crawler=require('./crawlerOpponent')
exports.GetOpponent=(req,res)=>{
const opponentClass=req.body.class
const DataCheck=()=>{
return new Promise((resolve,reject)=>{
if(!opponentClass){
return reject({
code:'query_error',
message:'query_error'
})
}
resolve()
})
}
const CralwerOpponent=()=>{
return crawler.getDecks(opponentClass)
}
DataCheck
.then(CralwerOpponent)
.then((decks)=>{
res.status(200).json(decks)
})
.catch((err)=>{
res.status(500).json(err)
})
}
\ No newline at end of file
const express=require('express')
const router=express.Router()
const getMulligan=require('./getMulligan')
router.post('/getmulligan',getMulligan.GetMulligan)
module.exports=router
\ No newline at end of file
......
const crawler=require('./crawler')
const crawler=require('./crawlerDeckCodes')
const cheerio=require('cheerio')
const addCards=require('../card/addCards')
const addDeck=require('../../database/deck/addDeck')
......
......@@ -6,7 +6,7 @@ exports.GetDeckId=(deckOwner,deckTitle)=>{
if (err) throw err
connection.query(`select * from deck where deckOwner=\'${deckOwner}\' and deckTitle=\'${deckTitle}\'`,(err,results,field)=>{
if (err) throw err
console.log('result in getDeckId'+results[0])
//console.log('result in getDeckId'+results[0])
connection.release()
resolve(results[0].id)
})
......
This diff is collapsed. Click to expand it.
......@@ -27,6 +27,7 @@
"mysql": "^2.16.0",
"mysql-apostrophe": "^1.0.8",
"path": "^0.12.7",
"puppeteer": "^1.11.0",
"request": "^2.88.0",
"request-promise": "^4.2.2"
}
......
......@@ -32,6 +32,9 @@
}
})
})
$('#submitButton').click(function(){
})
})
</script>
</head>
......@@ -66,17 +69,17 @@
<div class="col-md-4">
<img src="https://d1u5p3l4wpay3k.cloudfront.net/hearthstone_gamepedia/c/c5/Garrosh_Hellscream%28635%29.png?version=bab934001bb784a94c59a47823d535a7" style="width:150px;height:200px;"/>
<br>
<input type="radio" name="checkOpponent" value="전사"/>전사
<input type="radio" name="checkOpponent" value="WARRIOR"/>전사
</div>
<div class="col-md-4">
<img src="https://d1u5p3l4wpay3k.cloudfront.net/hearthstone_gamepedia/4/4b/Thrall%28319%29.png?version=adcee55715548b949a7d973c2fddbd95" style="width:150px;height:200px;"/>
<br>
<input type="radio" name="checkOpponent" value="주술사"/>주술사
<input type="radio" name="checkOpponent" value="SHAMAN"/>주술사
</div>
<div class="col-md-4">
<img src="https://d1u5p3l4wpay3k.cloudfront.net/hearthstone_gamepedia/a/a4/Valeera_Sanguinar%282%29.png?version=84a816910b223169eb14cc93c20437b2" style="width:150px;height:200px;"/>
<br>
<input type="radio" name="checkOpponent" value="도적"/>도적
<input type="radio" name="checkOpponent" value="ROGUE"/>도적
</div>
</div>
<br>
......@@ -86,17 +89,17 @@
<div class="col-md-4">
<img src="https://d1u5p3l4wpay3k.cloudfront.net/hearthstone_gamepedia/4/4d/Uther_Lightbringer%28257%29.png?version=b45ade5ac3fdd2579160fe5d7b7c1b20" style="width:150px;height:200px;"/>
<br>
<input type="radio" name="checkOpponent" value="성기사"/>성기사
<input type="radio" name="checkOpponent" value="PALADIN"/>성기사
</div>
<div class="col-md-4">
<img src="https://d1u5p3l4wpay3k.cloudfront.net/hearthstone_gamepedia/a/a0/Rexxar%28484%29.png?version=c21b57837db15d20cc814f2bf45682b6" style="width:150px;height:200px;"/>
<br>
<input type="radio" name="checkOpponent" value="사냥꾼"/>사냥꾼
<input type="radio" name="checkOpponent" value="HUNTER"/>사냥꾼
</div>
<div class="col-md-4">
<img src="https://d1u5p3l4wpay3k.cloudfront.net/hearthstone_gamepedia/f/fa/Malfurion_Stormrage%28621%29.png?version=b3f5a40e33f33d32995f3becbdd7aa94" style="width:150px;height:200px;"/>
<br>
<input type="radio" name="checkOpponent" value="드루이드"/>드루이드
<input type="radio" name="checkOpponent" value="DRUID"/>드루이드
</div>
</div>
<br>
......@@ -106,17 +109,17 @@
<div class="col-md-4">
<img src="https://d1u5p3l4wpay3k.cloudfront.net/hearthstone_gamepedia/0/0a/Gul%27dan%28618%29.png?version=90f421585c6f2d493ba94e259a76190e" style="width:150px;height:200px;"/>
<br>
<input type="radio" name="checkOpponent" value="흑마법사"/>흑마법사
<input type="radio" name="checkOpponent" value="WARLOCK"/>흑마법사
</div>
<div class="col-md-4">
<img src="https://d1u5p3l4wpay3k.cloudfront.net/hearthstone_gamepedia/3/3c/Jaina_Proudmoore%28320%29.png?version=75868a59a53f90bce829edeb66126b73" style="width:150px;height:200px;"/>
<br>
<input type="radio" name="checkOpponent" value="마법사"/>마법사
<input type="radio" name="checkOpponent" value="MAGE"/>마법사
</div>
<div class="col-md-4">
<img src="https://d1u5p3l4wpay3k.cloudfront.net/hearthstone_gamepedia/8/80/Anduin_Wrynn%28110%29.png?version=ba8ecc39b3fdd4a2ede72e046c434454" style="width:150px;height:200px;"/>
<br>
<input type="radio" name="checkOpponent" value="사제"/>사제
<input type="radio" name="checkOpponent" value="PRIEST"/>사제
</div>
</div>
<br>
......@@ -124,7 +127,7 @@
<div class="row">
<div class="col-md-5"></div>
<div class="col-md-2">
<input class="btn btn-lg btn-primary btn-block" type="button" value="확인"/>
<input class="btn btn-lg btn-primary btn-block" type="button" id="submitButton" value="확인"/>
</div>
<div class="col-md-5"></div>
</div>
......