EorzeaMarketNote Web API using Server-Side JavaScript example
Overview
Here is a example to display information on the Mandragora server's potion.
All sources
Please clone the example source from the following git repository.
The example for Server-Side JavaScript is contained in the js_server directory.
Premise
The example is based on the following tools and libraries.
How to run
The command to execute the example is as follow.
npm install
npm start
When the program runs, access the following URL.
Main source(example.js)
const axios = require('axios');
const axios_extension = require('axios-extensions');
const express = require('express');
const app = express();
const portNo = 3000;
const cache_adapter = axios_extension.cacheAdapterEnhancer(axios.defaults.adapter);
app.get('/', async (req, res, next) => {
try {
const result = {}
const client = axios.create({ adapter: cache_adapter });
const apiResponse = await client.get('https://ff14marketnoteapi.ownway.info/research/1/market_research?dc=Mana');
if (apiResponse.status == 200) {
const aggregate_date = new Date(apiResponse.headers['last-modified']);
result['aggregate_date'] = aggregate_date;
const data = apiResponse.data;
const data_in_world = data['Mandragora'] || {};
const latest_market_researches = data_in_world['l'] || {};
const optional_infos = data_in_world['o'] || {};
// 7992765ec34: potion's item key
const item_key = '7992765ec34';
const potion_l = latest_market_researches[item_key] || null;
const potion_o = optional_infos[item_key] || null;
if (potion_l) {
const price_nq = potion_l[0];
const price_hq = potion_l[1];
const stock_nq = potion_l[2];
const stock_hq = potion_l[3];
const circulation1_nq = potion_l[4];
const circulation1_hq = potion_l[5];
const circulation2_nq = potion_l[6];
const circulation2_hq = potion_l[7];
const circulation_nq = circulation1_nq && circulation2_nq ? Math.round(24 * circulation1_nq / circulation2_nq) : null;
const circulation_hq = circulation1_hq && circulation2_hq ? Math.round(24 * circulation1_hq / circulation2_hq) : null;
const researched_time = new Date(potion_l[8]);
result['price_nq'] = price_nq;
result['price_hq'] = price_hq;
result['stock_nq'] = stock_nq;
result['stock_hq'] = stock_hq;
result['circulation_nq'] = circulation_nq;
result['circulation_hq'] = circulation_hq;
result['researched_time'] = researched_time;
} else {
console.log("No latest market research.");
}
if (potion_o) {
const material_cost_hq_first = potion_o[0];
const material_cost_price_first = potion_o[1];
result['material_cost_hq_first'] = material_cost_hq_first;
result['material_cost_price_first'] = material_cost_price_first;
} else {
console.log("No optional info.");
}
res.send(result);
} else {
res.send({ 'error_status_code': response.status });
}
} catch (error) {
res.send({ 'error': error.message });
}
});
app.listen(portNo, () => {
console.log("It started.");
});