-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* New galnet news feed * Refactored commotiy ticker (now leverages cache)
- Loading branch information
1 parent
3b56dde
commit 842eb77
Showing
9 changed files
with
107 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
const fs = require('fs') | ||
const { getISOTimestamp } = require('../utils/dates') | ||
const dbAsync = require('../db/db-async') | ||
const { ARDENT_MARKET_TICKER_CACHE } = require('../consts') | ||
|
||
module.exports = async () => { | ||
const stations = await dbAsync.all(` | ||
SELECT * FROM stations.stations AS s WHERE | ||
s.stationType != 'Fleet Carrier' | ||
AND s.stationType IS NOT NULL | ||
GROUP BY s.stationName | ||
ORDER BY s.updatedAt DESC | ||
LIMIT 50`) | ||
|
||
const imports = [] | ||
for (const station of stations) { | ||
const stationImport = await dbAsync.get(` | ||
SELECT * FROM trade.commodities as c | ||
WHERE c.marketId = @marketId | ||
AND (c.demand > 1000 OR c.demand = 0) | ||
AND c.demandBracket = 3 | ||
AND c.updatedAt > '${getISOTimestamp('-1')}' | ||
ORDER BY c.sellPrice DESC | ||
LIMIT 5`, { marketId: station.marketId }) | ||
if (stationImport) imports.push(stationImport) | ||
} | ||
|
||
const exports = [] | ||
for (const station of stations) { | ||
const stationExport = await dbAsync.get(` | ||
SELECT * FROM trade.commodities as c | ||
WHERE c.marketId = @marketId | ||
AND c.stock > 1000 | ||
AND c.stockBracket = 3 | ||
AND c.updatedAt > '${getISOTimestamp('-1')}' | ||
ORDER BY c.buyPrice DESC | ||
LIMIT 5`, { marketId: station.marketId }) | ||
if (stationExport) exports.push(stationExport) | ||
} | ||
|
||
const data = [ | ||
...imports, | ||
...exports | ||
] | ||
.sort((a, b) => a.updatedAt.localeCompare(b.updatedAt)) // Sort results by recency | ||
.filter((obj1, i, arr) => arr.findIndex(obj2 => (obj2.marketId === obj1.marketId)) === i) // Filter so only one entry for each station | ||
|
||
fs.writeFileSync(ARDENT_MARKET_TICKER_CACHE, JSON.stringify(data, null, 2)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
const fs = require('fs') | ||
const { ARDENT_GALNET_NEWS_CACHE } = require('../consts') | ||
|
||
const GALNET_NEWS_FEED = 'https://cms.zaonce.net/en-GB/jsonapi/node/galnet_article' | ||
|
||
module.exports = async () => { | ||
try { | ||
const req = await fetch(GALNET_NEWS_FEED) | ||
const json = await req.json() | ||
|
||
const data = json?.data.map(item => ({ | ||
published: new Date(item.attributes.published_at).toISOString(), | ||
date: item.attributes['field_galnet_date'], | ||
title: item.attributes.title, | ||
text: item.attributes.body.value.replace(/\r/g, ''), | ||
slug: item.attributes.field_slug, | ||
url: `https://community.elitedangerous.com/galnet/uid/${item.attributes.field_galnet_guid}` | ||
})) | ||
|
||
fs.writeFileSync(ARDENT_GALNET_NEWS_CACHE, JSON.stringify(data, null, 2)) | ||
} catch (e) { | ||
console.error('Failed to fetch galnet news', e) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
const fs = require('fs') | ||
const { ARDENT_GALNET_NEWS_CACHE, ARDENT_MARKET_TICKER_CACHE } = require('../../lib/consts') | ||
|
||
module.exports = (router) => { | ||
router.get('/api/v1/news/galnet', async (ctx, next) => { | ||
ctx.body = JSON.parse(fs.readFileSync(ARDENT_GALNET_NEWS_CACHE)) | ||
}) | ||
router.get('/api/v1/news/commodities', async (ctx, next) => { | ||
ctx.body = JSON.parse(fs.readFileSync(ARDENT_MARKET_TICKER_CACHE)) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters