-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
141 lines (126 loc) · 4.95 KB
/
index.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import fs from 'node:fs/promises';
import fss from 'node:fs';
import path from 'node:path';
import md from 'node-md-config';
import promiseTimeout from './lib/promiseTimeout.js';
import Settings from './lib/Settings.js';
import Creator from './lib/AttachmentCreator.js';
import MarketFetcher from './lib/market/v1/MarketFetcher.js';
export default class PriceCheckQuerier {
/**
* Creates an instance representing a WarframeNexusStats data object
* @constructor
*/
constructor({ logger = console, marketCache = undefined, skipMarket = false }) {
this.settings = new Settings();
this.logger = logger;
this.skipMarket = skipMarket;
if (!skipMarket) {
try {
/**
* Fetch market data
* @type {MarketFetcher}
*/
this.marketFetcher = new MarketFetcher({ logger, settings: this.settings, marketCache });
} catch (e) {
this.logger.error(`couldn't set up market fetcher: ${e.message}`);
}
}
/**
* Attachment creator for generating attachments
* @type {Creator}
*/
this.creator = new Creator();
}
/**
* Lookup a list of results for a query
* @param {string} query Query to search the nexus-stats database against
* @param {string} platform Platform to query price data for. One of 'pc', 'ps4', 'xb1'
* @returns {Promise<Array<NexusItem>>} a Promise of an array of Item objects
*/
async priceCheckQuery(query, platform = 'pc') {
this.logger.info(`state:\n\tskipMarket: ${this.skipMarket}\n\tquery: ${query}\n\tplatform: ${platform}`);
if (!query) {
throw new Error('This funtcion requires a query to be provided');
}
// eslint-disable-next-line no-param-reassign
platform = this.settings.lookupAlias(platform.toLowerCase());
let successfulQuery;
let attachments = [];
/* istanbul ignore else */
if (this.marketFetcher && !this.skipMarket) {
this.logger.info(`querying market for ${query} on ${platform}`);
try {
const marketPromise = this.marketFetcher.queryMarket(query, {
successfulQuery,
platform: this.settings.lookupAlias(platform, true),
});
const marketResults = await promiseTimeout(this.settings.timeouts.market, marketPromise);
attachments = [...attachments, ...marketResults];
} catch (e) {
this.logger.error(`Couldn't process ${query} on warframe.market... time out.`);
}
} else {
this.logger.info('No market fetcher, skipping market');
}
return attachments;
}
/**
* Lookup a list of results for a query
* @param {string} query Query to search the nexus-stats database against
* @param {Object[]} priorResults results provided from a prior search
* @param {string} platform Platform to query price data for. One of 'pc', 'ps4', 'xb1'
* @returns {Promise<string>} a Promise of a string containing the results of the query
*/
async priceCheckQueryString(query, priorResults, platform = 'pc') {
const components = priorResults || (await this.priceCheckQuery(query, platform));
const tokens = [`[${platform.toUpperCase()}] ${query}`];
components.slice(0, 4).forEach((component) => {
tokens.push(`${md.lineEnd}${component.toString()}`);
});
let componentsToReturnString = `${md.codeMulti}${tokens.join()}${md.blockEnd}`;
componentsToReturnString = components.length > 0 ? componentsToReturnString : this.settings.defaultString;
return componentsToReturnString;
}
/**
* Lookup a list of results for a query
* @param {string} query Query to search the nexus-stats database against
* @param {Object[]} priorResults results provided from a prior search
* @param {string} platform Platform to query price data for. One of 'pc', 'ps4', 'xb1'
* @returns {Promise<Array<Object>>} a Promise of an array of attachment objects
*/
async priceCheckQueryAttachment(query, priorResults, platform = 'pc') {
const components = priorResults || (await this.priceCheckQuery(query, platform));
const realPlatform = this.settings.lookupAlias(platform);
const attachment = this.creator.attachmentFromComponents(components, query, realPlatform);
return [attachment];
}
/**
* Stop updating caches
*/
async stopUpdating() {
/* istanbul ignore else */
if (!this.skipMarket && this.marketFetcher.marketCache) {
this.marketFetcher.marketCache.stop();
} else {
this.logger.log('no market cache, or skipMarket was true');
}
if (fss.existsSync(`${global.__basedir}/tmp`)) {
const files = await fs.readdir(`${global.__basedir}/tmp`);
let allSuccess = true;
await Promise.all(
files.map(async (file) => {
try {
await fs.unlink(path.join(global.__basedir, 'tmp', file));
} catch (e) {
allSuccess = false;
this.logger.debug(`Couldn't delete ${file}`);
}
})
);
if (allSuccess) {
await fs.rmdir(`${global.__basedir}/tmp`);
}
}
}
}