-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathconnections.js
More file actions
70 lines (52 loc) · 1.93 KB
/
connections.js
File metadata and controls
70 lines (52 loc) · 1.93 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
const fetch = require("node-fetch")
const sleep = require("sleep").msleep
const { SLEEP_TIME, apiDomains } = require("./constants.js")
const getConnection = async (requestOptions, region, id) => {
sleep(SLEEP_TIME)
const url = "https://" + apiDomains[region] + "/data/wow/connected-realm/" + id
const res = await fetch(url, requestOptions)
const json = await res.json()
if (!json.realms) return console.log("ERROR: response missing realms:", json)
return {
id : id,
region: region.toUpperCase(),
realms: json.realms.map(realm => realm.id),
}
}
const getConnectionIDsForRegion = async (requestOptions, region) => {
sleep(SLEEP_TIME)
const url = "https://" + apiDomains[region] + "/data/wow/connected-realm/"
const res = await fetch(url, requestOptions)
const json = await res.json()
if (!json.connected_realms) return console.log("ERROR: response missing connected_realms:", json)
return json.connected_realms.map(connection => {
const id = connection.href.replace(/.+\/connected-realm\/(\d+).*/, "$1")
return parseInt(id, 10)
})
}
const getConnectionsForRegion = async (accessToken, region) => {
const list = []
const requestOptions = {
headers: {
"Authorization": "Bearer " + accessToken,
"Battlenet-Namespace": "dynamic-" + region,
}
}
console.log(" ")
console.log("Getting connection IDs for region", region)
const ids = await getConnectionIDsForRegion(requestOptions, region)
if (!ids) return
console.log(" Found :", ids.length)
for (const id of ids) {
console.log(" ")
console.log("Getting info for connection", id)
const item = await getConnection(requestOptions, region, id)
console.log(" Count :", item.realms.length)
console.log(" Realms:", item.realms.join(", "))
list.push(item)
}
console.log(" ")
console.log("Got data for", list.length, "connections")
return list
}
module.exports = getConnectionsForRegion