forked from MrPrimate/ddb-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcampaign.js
53 lines (47 loc) · 1.6 KB
/
campaign.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
// This data is the light version of data available in the character builder
const fetch = require("node-fetch");
const CONFIG = require("./config.js");
const authentication = require("./auth.js");
const Cache = require("./cache");
var CACHE_CAMPAIGNS = new Cache("CAMPAIGNS", 0.25);
// this endpoint aggressively caches campaigns as it's prone to been marked as a bot
const getCampaigns = (cobalt, cacheId) => {
return new Promise((resolve, reject) => {
const cache = CACHE_CAMPAIGNS.exists(cacheId);
if (cache !== undefined) {
return resolve(cache.data);
}
const auth = authentication.CACHE_AUTH.exists(cacheId);
if (!auth || !auth.data) {
reject("Unable to authorise cobalt cookie");
}
const headers = {
"Authorization": `Bearer ${auth.data}`,
"User-Agent": "Foundry VTT Character Integrator",
"Accept": "application/json",
"Accept-Encoding": "gzip, deflate, br",
"Cookie": `CobaltSession=${cobalt}`,
};
const options = {
method: "GET",
headers: headers,
};
fetch(CONFIG.urls.campaignsAPI, options)
.then((res) => res.json())
.then((json) => {
if (json.status == "success") {
CACHE_CAMPAIGNS.add(cacheId, json.data);
resolve(json.data);
} else if (json.blockScript) {
reject("You've been marked as a bot by DDB, please try again later");
} else {
reject("Unknown error");
}
})
.catch((error) => {
console.error(`Error fetching campaigns: ${error}`);
reject(error);
});
});
};
exports.getCampaigns = getCampaigns;