getOpponent.js
2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
const crawler=require('./crawlerOpponent')
const puppeteer=require('puppeteer')
const cheerio=require('cheerio')
exports.GetOpponent=(req,res)=>{
const opponentClass=req.session.opponentClass
const DataCheck=()=>{
return new Promise((resolve,reject)=>{
if(!opponentClass){
return reject({
code:'query_error',
message:'query_error'
})
}
resolve()
})
}
const CrawlerOpponent=()=>{
return crawler.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)=>{
return new Promise((resolve,reject)=>{
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)
resolve(decks)
})
}
DataCheck()
//.then(CrawlerOpponent)
.then(GetContent)
.then(GetDeckInfo)
.then((decks)=>{
return new Promise((resolve,reject)=>{
console.log('decks in getOpponent: ',decks)
resolve(decks)
})
})
.catch((err)=>{
console.log(err)
})
}