warframe-nexus-query 2.1.0 → 2.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +367 -27
- package/index.js +25 -6
- package/lib/AttachmentCreator.js +80 -7
- package/lib/market/v2/.eslintrc.json +6 -0
- package/lib/market/v2/MarketFetcherV2.js +418 -0
- package/lib/market/v2/constants.js +60 -0
- package/lib/market/v2/index.js +12 -0
- package/lib/market/v2/models/Item.js +135 -0
- package/lib/market/v2/models/Order.js +197 -0
- package/lib/market/v2/models/Summary.js +165 -0
- package/lib/market/v2/types.d.ts +137 -0
- package/lib/market/v2/utils/cache.js +189 -0
- package/lib/market/v2/utils/http.js +246 -0
- package/lib/market/v2/utils/i18n.js +40 -0
- package/lib/market/v2/utils/statistics.js +185 -0
- package/package.json +9 -79
- package/lib/market/v1/MarketFetcher.js +0 -148
- package/lib/market/v1/summary.js +0 -65
|
@@ -1,148 +0,0 @@
|
|
|
1
|
-
import fs from 'node:fs/promises';
|
|
2
|
-
|
|
3
|
-
import getColors from 'get-image-colors';
|
|
4
|
-
import imageDownloader from 'image-downloader';
|
|
5
|
-
import jsonQuery from 'json-query';
|
|
6
|
-
import JSONCache from 'json-fetch-cache';
|
|
7
|
-
|
|
8
|
-
import baseDir from '../../../basedir.js';
|
|
9
|
-
|
|
10
|
-
import MarketSummary from './summary.js';
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* @typedef {Object} MappedPrice
|
|
14
|
-
* @property {string} volume
|
|
15
|
-
* @property {string} median
|
|
16
|
-
* @property {string} max_price
|
|
17
|
-
* @property {string} min_price
|
|
18
|
-
* @property {string} datetime ts formatted date string
|
|
19
|
-
*/
|
|
20
|
-
|
|
21
|
-
export default class MarketFetcher {
|
|
22
|
-
constructor({ logger = console, settings, marketCache }) {
|
|
23
|
-
/**
|
|
24
|
-
* The json cache stored data from warframe.market
|
|
25
|
-
* @type {JSONCache}
|
|
26
|
-
*/
|
|
27
|
-
this.marketCache = marketCache || new JSONCache(settings.urls.market, settings.maxCacheLength, { logger });
|
|
28
|
-
|
|
29
|
-
this.settings = settings;
|
|
30
|
-
this.urls = {
|
|
31
|
-
market: process.env.MARKET_URL_OVERRIDE || 'https://api.warframe.market',
|
|
32
|
-
marketAssets: process.env.MARKET_ASSETS_URL_OVERRIDE || 'https://warframe.market/static/assets/',
|
|
33
|
-
marketBasePath: '/v1/items/',
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
this.logger = logger;
|
|
37
|
-
|
|
38
|
-
this.logger.info('finished setting up market fetcher');
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
async averagesForItem(urlName, platform = 'pc') {
|
|
42
|
-
const reqOpts = {
|
|
43
|
-
headers: {
|
|
44
|
-
platform: platform.toLowerCase(),
|
|
45
|
-
},
|
|
46
|
-
};
|
|
47
|
-
const data = await fetch(`${this.urls.market}${this.urls.marketBasePath}${urlName}/statistics`, reqOpts).then((d) =>
|
|
48
|
-
d.json()
|
|
49
|
-
);
|
|
50
|
-
/**
|
|
51
|
-
* @type {MappedPrice[]}
|
|
52
|
-
*/
|
|
53
|
-
const stats = data.payload.statistics_live['48hours'];
|
|
54
|
-
|
|
55
|
-
// Descending sort
|
|
56
|
-
stats.sort((a, b) => {
|
|
57
|
-
const tsA = new Date(a.datetime).getTime();
|
|
58
|
-
const tsB = new Date(b.datetime).getTime();
|
|
59
|
-
if (tsA === tsB) return 0;
|
|
60
|
-
if (tsA < tsB) return -1;
|
|
61
|
-
if (tsA > tsB) return 1;
|
|
62
|
-
/* istanbul ignore next */
|
|
63
|
-
return 0;
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
const deepData = stats[0];
|
|
67
|
-
return {
|
|
68
|
-
soldCount: deepData ? Number.parseInt(deepData.volume, 10) : undefined,
|
|
69
|
-
soldPrice: deepData ? Number.parseInt(deepData.median, 10) : undefined,
|
|
70
|
-
maximum: deepData ? Number.parseInt(deepData.max_price, 10) : undefined,
|
|
71
|
-
minimum: deepData ? Number.parseInt(deepData.min_price, 10) : undefined,
|
|
72
|
-
};
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
async resultForItem(urlName, platform = 'pc') {
|
|
76
|
-
const reqOpts = {
|
|
77
|
-
headers: {
|
|
78
|
-
platform: platform.toLowerCase(),
|
|
79
|
-
},
|
|
80
|
-
};
|
|
81
|
-
const res = await fetch(`${this.urls.market}${this.urls.marketBasePath}${urlName}`, reqOpts).then((d) => d.json());
|
|
82
|
-
return res.payload ? Promise.all(res.payload.item.items_in_set.map((item) => this.summaryForItem(item))) : {};
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* Construct a summary for the market item
|
|
87
|
-
* @param {Object} item Warframe.market Item
|
|
88
|
-
* @returns {Object} Market summary for item
|
|
89
|
-
*/
|
|
90
|
-
async summaryForItem(item) {
|
|
91
|
-
try {
|
|
92
|
-
// set up temp folder
|
|
93
|
-
await fs.mkdir(`${baseDir}/tmp`);
|
|
94
|
-
} catch (e) {
|
|
95
|
-
// this.logger.error(e);
|
|
96
|
-
}
|
|
97
|
-
const summary = new MarketSummary(item);
|
|
98
|
-
|
|
99
|
-
summary.prices = await this.averagesForItem(item.url_name);
|
|
100
|
-
const options = {
|
|
101
|
-
url: summary.thumbnail,
|
|
102
|
-
dest: `${baseDir}/tmp/${summary.name}.png`,
|
|
103
|
-
};
|
|
104
|
-
try {
|
|
105
|
-
const { image } = await imageDownloader.image(options);
|
|
106
|
-
if (image) {
|
|
107
|
-
const colors = await getColors(image, 'image/png');
|
|
108
|
-
summary.color = typeof colors !== 'undefined' ? colors[0].hex().replace('#', '0x') : 0xff0000;
|
|
109
|
-
}
|
|
110
|
-
} catch (e) {
|
|
111
|
-
/* istanbul ignore next */
|
|
112
|
-
this.logger.error(e);
|
|
113
|
-
} finally {
|
|
114
|
-
await fs.unlink(`${baseDir}/tmp/${summary.name}.png`);
|
|
115
|
-
}
|
|
116
|
-
return summary;
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
async queryMarket(query, { successfulQuery, platform = 'pc' }) {
|
|
120
|
-
this.logger.info(`querying market for ${query} on ${platform}`);
|
|
121
|
-
const attachments = [];
|
|
122
|
-
try {
|
|
123
|
-
// get market data
|
|
124
|
-
const marketData = await this.marketCache.getDataJson();
|
|
125
|
-
if (!marketData) {
|
|
126
|
-
this.logger.info('No market data!');
|
|
127
|
-
return [];
|
|
128
|
-
}
|
|
129
|
-
const marketResults = jsonQuery(`items[*item_name~/^${successfulQuery || query}.*/i]`, {
|
|
130
|
-
data: marketData.payload || {},
|
|
131
|
-
allowRegexp: true,
|
|
132
|
-
}).value;
|
|
133
|
-
if (!marketResults || marketResults.length < 1) {
|
|
134
|
-
this.logger.info('No market results!');
|
|
135
|
-
return [];
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
const marketComponents = await this.resultForItem(marketResults[0].url_name, platform);
|
|
139
|
-
if (marketComponents.length > 0) {
|
|
140
|
-
attachments.push(...marketComponents);
|
|
141
|
-
}
|
|
142
|
-
} catch (err) {
|
|
143
|
-
/* istanbul ignore next */
|
|
144
|
-
this.logger.error(err);
|
|
145
|
-
}
|
|
146
|
-
return attachments;
|
|
147
|
-
}
|
|
148
|
-
}
|
package/lib/market/v1/summary.js
DELETED
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
const urls = {
|
|
2
|
-
market: process.env.MARKET_URL_OVERRIDE || 'https://api.warframe.market/v1/items',
|
|
3
|
-
marketAssets: process.env.MARKET_ASSETS_URL_OVERRIDE || 'https://warframe.market/static/assets/',
|
|
4
|
-
};
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Represents an individual component object of an item.
|
|
8
|
-
*/
|
|
9
|
-
export default class Summary {
|
|
10
|
-
/**
|
|
11
|
-
* Represents an individual item.
|
|
12
|
-
* @param {json} itemData the component data to parse into a summary
|
|
13
|
-
* @constructor
|
|
14
|
-
*/
|
|
15
|
-
constructor(itemData) {
|
|
16
|
-
this.name = itemData.en.item_name.trim();
|
|
17
|
-
this.thumbnail = `${urls.marketAssets}${itemData.thumb}`;
|
|
18
|
-
this.partThumb = `${urls.marketAssets}${itemData.sub_icon}`;
|
|
19
|
-
this.tradingTax = itemData.trading_tax || 0;
|
|
20
|
-
this.ducats = itemData.ducats || 0;
|
|
21
|
-
this.masteryLevel = itemData.mastery_level;
|
|
22
|
-
this.tradable = itemData.tradable;
|
|
23
|
-
this.wikiUrl = itemData.en.wiki_link;
|
|
24
|
-
this.codex = itemData.en.codex;
|
|
25
|
-
this.description = itemData.en.description.replace(/<\/?p>/gi, '');
|
|
26
|
-
this.drops = itemData.en.drop;
|
|
27
|
-
this.url = `http://warframe.market/items/${itemData.url_name}`;
|
|
28
|
-
|
|
29
|
-
this.type = 'market-v1';
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* The components' string representation
|
|
34
|
-
* @param {string} opt Option, either 'codex', 'item', 'location', 'all'
|
|
35
|
-
* @returns {string}
|
|
36
|
-
*/
|
|
37
|
-
toString(opt = 'item') {
|
|
38
|
-
let value = this.name;
|
|
39
|
-
if (opt === 'codex' || opt === 'all') {
|
|
40
|
-
value += `\`\`\`\nCodex: ${this.codex}\n`;
|
|
41
|
-
}
|
|
42
|
-
if (opt === 'item' || opt === 'all') {
|
|
43
|
-
value +=
|
|
44
|
-
`${this.pad(`Requires: ${this.masteryLevel}`, 15)}| Tax: ${this.pad(`${this.tradingTax}cr`)}\n` +
|
|
45
|
-
`Item is ${this.tradable ? '' : ' not '} tradable\n` +
|
|
46
|
-
`${this.pad(`Sold ${this.prices.soldCount} for ${this.prices.soldPrice}`, 15)}|` +
|
|
47
|
-
`Minimum ${this.prices.minimum} | Maximum ${this.prices.maximum}\n`;
|
|
48
|
-
}
|
|
49
|
-
if (opt === 'location' || opt === 'all') {
|
|
50
|
-
value += `${this.pad(`Drops: ${this.drops.map((drop) => drop.name).join(', ')}`, 15)}\n`;
|
|
51
|
-
}
|
|
52
|
-
value += `\`\`\`\n${this.wikiUrl}`;
|
|
53
|
-
return `\u221F${value}`;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
pad(str, length = 10, character = ' ') {
|
|
57
|
-
let stringRet;
|
|
58
|
-
if (str.length < length) {
|
|
59
|
-
stringRet = this.pad(`${str}${character}`);
|
|
60
|
-
} else {
|
|
61
|
-
stringRet = str;
|
|
62
|
-
}
|
|
63
|
-
return stringRet;
|
|
64
|
-
}
|
|
65
|
-
}
|