feat: more chart periods
This commit is contained in:
parent
3bad27b305
commit
cab278f102
8 changed files with 79 additions and 10 deletions
|
@ -2,9 +2,9 @@ import { getStore, setEnvironmentContext } from '@netlify/blobs'
|
|||
import { DateTime } from 'luxon'
|
||||
import fs from 'fs'
|
||||
|
||||
const getKeys = () => {
|
||||
const getKeys = (months = 1) => {
|
||||
const currentDate = DateTime.now()
|
||||
const weeks = Math.floor(currentDate.daysInMonth / 7)
|
||||
const weeks = Math.floor((currentDate.daysInMonth * months) / 7)
|
||||
let count = 0
|
||||
const keys = []
|
||||
|
||||
|
@ -24,8 +24,14 @@ export const onPreBuild = async ({ constants }) => {
|
|||
token: constants.NETLIFY_API_TOKEN,
|
||||
})
|
||||
|
||||
const keys = getKeys()
|
||||
const chartData = { data: [] }
|
||||
const monthKeys = getKeys()
|
||||
const monthChartData = { data: [] }
|
||||
const threeMonthKeys = getKeys(3)
|
||||
const threeMonthChartData = { data: [] }
|
||||
const sixMonthKeys = getKeys(6)
|
||||
const sixMonthChartData = { data: [] }
|
||||
const oneYearKeys = getKeys(12)
|
||||
const oneYearChartData = { data: [] }
|
||||
const scrobbles = getStore('scrobbles')
|
||||
const artists = getStore('artists')
|
||||
const albums = getStore('albums')
|
||||
|
@ -34,14 +40,32 @@ export const onPreBuild = async ({ constants }) => {
|
|||
const albumsMap = await albums.get('albums-map', { type: 'json' })
|
||||
const nowPlaying = await scrobbles.get('now-playing', { type: 'json'})
|
||||
|
||||
for (const key of keys) {
|
||||
for (const key of monthKeys) {
|
||||
const scrobbleData = await scrobbles.get(key, { type: 'json'})
|
||||
chartData['data'].push(...scrobbleData['data'])
|
||||
monthChartData['data'].push(...scrobbleData['data'])
|
||||
}
|
||||
|
||||
for (const key of threeMonthKeys) {
|
||||
const scrobbleData = await scrobbles.get(key, { type: 'json'})
|
||||
threeMonthChartData['data'].push(...scrobbleData['data'])
|
||||
}
|
||||
|
||||
for (const key of sixMonthKeys) {
|
||||
const scrobbleData = await scrobbles.get(key, { type: 'json'})
|
||||
sixMonthChartData['data'].push(...scrobbleData['data'])
|
||||
}
|
||||
|
||||
for (const key of oneYearKeys) {
|
||||
const scrobbleData = await scrobbles.get(key, { type: 'json'})
|
||||
oneYearChartData['data'].push(...scrobbleData['data'])
|
||||
}
|
||||
|
||||
fs.writeFileSync('./src/_data/json/scrobbles-window.json', JSON.stringify(windowData))
|
||||
fs.writeFileSync('./src/_data/json/artists-map.json', JSON.stringify(artistsMap))
|
||||
fs.writeFileSync('./src/_data/json/albums-map.json', JSON.stringify(albumsMap))
|
||||
fs.writeFileSync('./src/_data/json/now-playing.json', JSON.stringify(nowPlaying))
|
||||
fs.writeFileSync('./src/_data/json/scrobbles-month-chart.json', JSON.stringify(chartData))
|
||||
fs.writeFileSync('./src/_data/json/scrobbles-month-chart.json', JSON.stringify(monthChartData))
|
||||
fs.writeFileSync('./src/_data/json/scrobbles-three-month-chart.json', JSON.stringify(threeMonthChartData))
|
||||
fs.writeFileSync('./src/_data/json/scrobbles-six-month-chart.json', JSON.stringify(sixMonthChartData))
|
||||
fs.writeFileSync('./src/_data/json/scrobbles-one-year-chart.json', JSON.stringify(oneYearChartData))
|
||||
}
|
Reference in a new issue