Created
April 27, 2021 14:28
-
-
Save HairyMike/ddcb03da94bd0a62c57b962acb4250fe to your computer and use it in GitHub Desktop.
This file contains hidden or 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
const express = require('express'); | |
const app = express(); | |
const port = 3000; | |
const https = require('https'); | |
const options = { | |
hostname: 'ink.twetch.com', | |
port: 443, | |
path: '/twonks/market/sold', | |
method: 'GET' | |
} | |
app.get('/:thing', (req, res) => { | |
const twetchReq = https.request(options, twetchRes => { | |
console.log(`statusCode: ${twetchRes.statusCode}`); | |
var twetchData = ''; | |
twetchRes.on('data', chunk => { | |
twetchData += chunk; | |
}) | |
twetchRes.on('end', () => { | |
var source = JSON.parse(twetchData); | |
var groups = {}; | |
source.forEach((tx) => { | |
var meta = JSON.parse(JSON.parse(tx.meta)); | |
groups[meta.title] = groups[meta.title] || {} | |
var created = tx.created.split(' ')[0]; | |
groups[meta.title][created] = groups[meta.title][created] || [] | |
groups[meta.title][created].push(tx); | |
}); | |
var result = {}; | |
Object.keys(groups).forEach(group => { | |
Object.keys(groups[group]).forEach(date => { | |
var max = 0; | |
var min = 9999999999999999999999; | |
var sum = 0; | |
groups[group][date].forEach(tx => { | |
var price = tx.price; | |
if (price > max) { | |
max = price; | |
} | |
if (min > price) { | |
min = price; | |
} | |
sum += price; | |
}) | |
result[group] = result[group] || [] | |
result[group].push({ | |
Date: date, | |
Open: groups[group][date][0].price, | |
High: max, | |
Low: min, | |
Volume: sum, | |
Close: groups[group][date][groups[group][date].length - 1].price | |
}) | |
}) | |
}); | |
res.send(result[req.params.thing]); | |
}) | |
}) | |
twetchReq.on('error', error => { | |
console.error(error) | |
}); | |
twetchReq.end() | |
}) | |
app.listen(port, () => { | |
console.log(`Listening at http://localhost:${port}`); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment