-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.js
More file actions
126 lines (99 loc) · 3.82 KB
/
index.js
File metadata and controls
126 lines (99 loc) · 3.82 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
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
// Import third-party dependencies
require("dotenv").config()
const fetch = require("node-fetch")
const jsonfile = require("jsonfile")
// Import local dependencies
const { authDomains, authRegions, regions } = require("./constants")
const getConnectionsForRegion = require("./connections")
const getRealmsForRegion = require("./realms")
// Verify API user credentials are present
const apiKey = process.env.CLIENT_ID
const clientSecret = process.env.CLIENT_SECRET
if (!apiKey) return console.log("ERROR: missing CLIENT_ID in .env")
if (!clientSecret) return console.log("ERROR: missing CLIENT_SECRET in .env")
// Get valid API access tokens for US and China
const checkAccessToken = async (region, token) => {
const url = "https://" + authDomains[region] + "/oauth/check_token?token=" + token
const res = await fetch(url)
const json = await res.json()
return !json.error
}
const getAccessToken = async (region) => {
const url = "https://" + authDomains[region] + "/oauth/token"
+ "?grant_type=client_credentials"
+ "&client_id=" + apiKey
+ "&client_secret=" + clientSecret
const res = await fetch(url)
const json = await res.json()
if (json.error) return console.error("ERROR: error getting access token: " + json.error)
return json.access_token
}
const getAccessTokens = async () => {
const cached = jsonfile.readFileSync("output/accessToken.json", { throws: false })
const tokens = {}
const uniqueRegions = [...new Set(Object.values(authRegions))]
for (const region of uniqueRegions) {
if (cached && cached[region]) {
const ok = await checkAccessToken(region, cached[region])
if (ok) tokens[region] = cached[region]
}
if (!tokens[region]) {
tokens[region] = getAccessToken(region)
}
}
jsonfile.writeFileSync("output/accessToken.json", tokens)
return tokens
}
// Get an array listing all connected realm groups from all regions
const getConnections = async (accessTokens) => {
const connections = []
console.log(" ")
console.log("Getting data for all connections...")
for (const region of regions) {
const authRegion = authRegions[region]
const accessToken = accessTokens[authRegion]
const regionConnections = await getConnectionsForRegion(accessToken, region)
if (regionConnections) regionConnections.forEach(connection => connections.push(connection))
}
console.log(" ")
console.log("Done.")
return connections
}
// Get an array listing all realms from all regions
const getRealms = async (accessTokens) => {
const realms = []
console.log(" ")
console.log("Getting data for all realms...")
for (const region of regions) {
const authRegion = authRegions[region]
const accessToken = accessTokens[authRegion]
const regionRealms = await getRealmsForRegion(accessToken, region)
if (regionRealms) regionRealms.forEach(realm => realms.push(realm))
}
console.log(" ")
console.log("Done.")
return realms
}
// Write an object to a file as JSON
const writeJSON = (filename, data) => {
console.log(" ")
console.log("Writing data to file", filename)
jsonfile.writeFileSync(filename, data, { spaces: "\t" }, (err) => console.error(err))
console.log("Done.")
}
// Go
async function main () {
console.log(" ")
console.log("API Key :", apiKey)
console.log("API Secret :", clientSecret ? "OK" : "MISSING")
const accessTokens = await getAccessTokens()
console.log("Access Tokens:", accessTokens)
const realmData = await getRealms(accessTokens)
if (!realmData) return console.log("ERROR: getRealms returned nothing")
writeJSON("output/realmData.json", realmData)
const connectionData = await getConnections(accessTokens)
if (!connectionData) return console.log("ERROR: getRealms returned nothing")
writeJSON("output/connectionData.json", connectionData)
console.log(" ")
}
main()