Changed dashboard to get data from Spotify instead

This commit is contained in:
r-freeman 2023-02-01 22:33:03 +00:00
parent 95249f521c
commit 77574bce22
4 changed files with 53 additions and 19 deletions

View File

@ -1,6 +0,0 @@
{
"statsfm": {
"top_genre": "Modern rock",
"top_artist": "Muse"
}
}

View File

@ -1,7 +1,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 {getLocalData} from '@/lib/localData'
import {Metric} from '@/types'
export async function getDashboardData() {
@ -13,20 +13,21 @@ export async function getDashboardData() {
const totalStars = await getTotalStars(totalRepos)
const totalArticles = (await getAllArticles()).length
const totalArticleViews = (await getViews()).views
const {statsfm} = JSON.parse(await getLocalData())
const topArtist = await getTopArtist()
const topGenre = await getTopGenre()
const metrics: Metric[] = [
{
title: "Top genre",
value: statsfm.top_genre,
value: topGenre.genre,
group: "Spotify",
href: "https://spotify.com/"
href: "https://open.spotify.com/?"
},
{
title: "Top artist",
value: statsfm.top_artist,
value: topArtist.artist,
group: "Spotify",
href: "https://spotify.com/"
href: topArtist.uri
},
{
title: "Repos",

View File

@ -1,7 +0,0 @@
import path from 'path'
import {promises as fs} from 'fs'
export async function getLocalData() {
const jsonDirectory = path.join(process.cwd(), 'json')
return await fs.readFile(jsonDirectory + '/data.json', 'utf8')
}

View File

@ -6,6 +6,7 @@ const SPOTIFY_REFRESH_TOKEN = process.env.SPOTIFY_REFRESH_TOKEN
const SPOTIFY_TOKEN = "https://accounts.spotify.com/api/token"
const SPOTIFY_CURRENTLY_PLAYING = "https://api.spotify.com/v1/me/player/currently-playing"
const SPOTIFY_RECENTLY_PLAYED = "https://api.spotify.com/v1/me/player/recently-played"
const SPOTIFY_TOP_ARTISTS = "https://api.spotify.com/v1/me/top/artists"
const basic = Buffer.from(`${SPOTIFY_CLIENT_ID}:${SPOTIFY_CLIENT_SECRET}`).toString('base64')
@ -52,4 +53,49 @@ export const getRecentlyPlayed = async () => {
Authorization: `Bearer ${access_token}`
}
})
}
type TopArtistItem = {
name: string
uri: string
genres: string[]
}
type TopArtistsResponse = {
items: TopArtistItem[]
}
export const getTopArtist = async () => {
const {access_token}: Response = await getAccessToken() as Response
const response = await fetch(SPOTIFY_TOP_ARTISTS, {
headers: {
Authorization: `Bearer ${access_token}`
}
}).then(r => r.json()) as TopArtistsResponse
const artist = response.items[0].name
const uri = response.items[0].uri
return {
artist,
uri
}
}
export const getTopGenre = async () => {
const {access_token}: Response = await getAccessToken() as Response
const response = await fetch(SPOTIFY_TOP_ARTISTS, {
headers: {
Authorization: `Bearer ${access_token}`
}
}).then(r => r.json()) as TopArtistsResponse
let genre = response.items[0].genres[0]
genre = genre.charAt(0).toUpperCase() + genre.slice(1)
return {
genre
}
}