Skip to content

Commit

Permalink
update sahko
Browse files Browse the repository at this point in the history
  • Loading branch information
shnigi committed Oct 30, 2023
1 parent d05d0ad commit 340b354
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion sahko.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,33 @@ const sahko = async (ctx) => {
const currentDate = date.toISOString().split('T')[0];
const currentHour = date.getHours();
const requestUrl = `https://api.porssisahko.net/v1/price.json?date=${currentDate}&hour=${currentHour}`;
const upcomingPrices = 'https://api.porssisahko.net/v1/latest-prices.json';
const response = await fetch(requestUrl);
const json = await response.json();
ctx.reply(`Sähkö maksaapi ${json.price} cnt/kwh`);
const upcomingResponse = await fetch(upcomingPrices);
const upcomingJson = await upcomingResponse.json();
const futurePrices = upcomingJson.prices
.filter((item) => {
const startDate = new Date(item.startDate);
return startDate > date;
})
.reverse()
.slice(0, 10)
.map((item) => {
const startDate = new Date(item.startDate);
const formattedStartDate = startDate.toLocaleTimeString('fi-FI', {
hour: '2-digit',
minute: '2-digit',
});
return { startDate: formattedStartDate, price: item.price };
});

const formattedFuturePrices = futurePrices
.map((item) => `${item.startDate} ${item.price.toFixed(3)} cnt/kwh`)
.join('\n');

ctx.reply(`Sähkö maksaapi ${json.price.toFixed(3)} cnt/kwh\n
Tulevat tunnit: \n${formattedFuturePrices}`);
};

module.exports = { sahko };

0 comments on commit 340b354

Please sign in to comment.