Added spotify minutes listened metric to dashboard

This commit is contained in:
r-freeman 2023-02-02 21:47:35 +00:00
parent 4dd5e30f81
commit 520f45cab8
3 changed files with 37 additions and 3 deletions

View File

@ -6,3 +6,4 @@ DATABASE_URL=
SHADOW_DATABASE_URL=
GITHUB_ACCESS_TOKEN=
GITHUB_USERNAME=
STATSFM_USERNAME=

View File

@ -2,6 +2,7 @@ import {getTotalFollowers, getTotalRepos, getTotalStars} from '@/lib/github'
import {getAllArticles} from '@/lib/getAllArticles'
import {getTopArtist, getTopGenre} from '@/lib/spotify'
import {getViews} from '@/lib/views'
import {getStats} from '@/lib/statsfm'
import {Metric} from '@/types'
export async function getDashboardData() {
@ -14,12 +15,19 @@ export async function getDashboardData() {
const totalArticles = (await getAllArticles()).length
const totalArticleViews = (await getViews()).views
const topArtist = await getTopArtist()
const topGenre = await getTopGenre()
const {genre} = await getTopGenre()
const {minutesListened} = await getStats()
const metrics: Metric[] = [
{
title: "Minutes listened",
value: minutesListened,
group: "Spotify",
href: "https://open.spotify.com/?"
},
{
title: "Top genre",
value: topGenre.genre,
value: genre,
group: "Spotify",
href: "https://open.spotify.com/?"
},

25
lib/statsfm.ts Normal file
View File

@ -0,0 +1,25 @@
import fetch from 'node-fetch'
const STATSFM_USERNAME = process.env.STATSFM_USERNAME
const STATSFM_LIFETIME_STATS = `https://beta-api.stats.fm/api/v1/users/${STATSFM_USERNAME}/streams/stats?range=lifetime`
type StatsFmResponse = {
items: {
durationMs: number
}
}
export const getStats = async () => {
const response = await fetch(STATSFM_LIFETIME_STATS, {
method: 'GET',
headers: {
'Accept': 'application/json'
}
}).then(r => r.json()) as StatsFmResponse
const minutesListened = ((response.items.durationMs / 1000) / 60).toFixed(0)
return {
minutesListened
}
}