-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi.js
126 lines (99 loc) · 4.18 KB
/
api.js
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
const express = require("express");
const cors = require("cors");
const axios = require("axios");
const cheerio = require("cheerio");
const fs = require("fs");
const app = express();
app.use(cors());
let config;
try {
const configPath = process.env.OW_OBS_CONFIG_PATH ?? './config.json'
config = JSON.parse(fs.readFileSync(configPath), 'utf-8');
startEndpoint();
} catch (err) {
console.log(`Error while reading 'config.json': ${err}`)
}
function startEndpoint() {
app.get('/fetch', async (req, res) => {
try {
const url = `https://overwatch.blizzard.com/en-us/career/${config.tag}`;
const response = await axios.get(url);
const $ = cheerio.load(response.data);
const isPrivate = $('.Profile-player--privateText').length > 0;
if (isPrivate) {
console.log(`Error: account associated with tag "${config.tag}" is private.`);
res.status(403).json({ error: "Player profile is private." });
return;
}
const portrait = $('.Profile-player--portrait').attr('src');
const endorsement = $('.Profile-playerSummary--endorsement').attr('src');
let roleRanks = $('.Profile-playerSummary--rank');
let roles = $('.Profile-playerSummary--role img');
let hasTank = false;
let hasDPS = false;
let hasSupport = false;
let tankRank, dpsRank, supportRank;
if (roles.length === 3) {
hasTank = true;
hasDPS = true;
hasSupport = true;
tankRank = $(roleRanks[0]).attr('src');
dpsRank = $(roleRanks[1]).attr('src');
supportRank = $(roleRanks[2]).attr('src');
} else {
let tankURL = "https://static.playoverwatch.com/img/pages/career/icons/role/tank-f64702b684.svg#icon"
let dpsURL = "https://static.playoverwatch.com/img/pages/career/icons/role/offense-ab1756f419.svg#icon"
let supportURL = "https://static.playoverwatch.com/img/pages/career/icons/role/support-0258e13d85.svg#icon"
let len = roles.length;
for (let i = 0; i < len; i++) {
let src = $(roles[i]).attr('src');
switch (src) {
case tankURL:
hasTank = true;
tankRank = $(roleRanks[i]).attr('src');
break;
case dpsURL:
hasDPS = true;
dpsRank = $(roleRanks[i]).attr('src');
break;
case supportURL:
hasSupport = true;
supportRank = $(roleRanks[i]).attr('src');
break;
}
}
}
const data = {
portrait,
endorsement,
hasTank,
hasDPS,
hasSupport,
}
if (typeof tankRank !== 'undefined') {
data.tankRank = tankRank;
}
if (typeof dpsRank !== 'undefined') {
data.dpsRank = dpsRank;
}
if (typeof supportRank !== 'undefined') {
data.supportRank = supportRank
}
res.json(data);
} catch (err) {
if (err.response && err.response.status === 404) {
console.log(`Error: the tag "${config.tag}" does not seem to exist.`);
return res.status(500).json({ error: "invalid_tag" });
} else if (err.response && err.response.status === 502) {
console.log(`Error: Bad Gateway error (502) occured.`);
return res.status(502).json({ error: "bad_gateway" })
} else {
console.log(`Error: ${err}`)
return res.status(500).json({ error: "server_side" });
}
}
});
return app.listen(config.port, () => {
console.log(`Endpoint "/fetch" started on port ${config.port}`)
});
}