From 77574bce225f53b0b6fc2900a2483f960a5ea6e1 Mon Sep 17 00:00:00 2001 From: r-freeman Date: Wed, 1 Feb 2023 22:33:03 +0000 Subject: [PATCH] Changed dashboard to get data from Spotify instead --- json/data.json | 6 ------ lib/dashboard.ts | 13 +++++++------ lib/localData.ts | 7 ------- lib/spotify.ts | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 53 insertions(+), 19 deletions(-) delete mode 100644 json/data.json delete mode 100644 lib/localData.ts diff --git a/json/data.json b/json/data.json deleted file mode 100644 index d69f786..0000000 --- a/json/data.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "statsfm": { - "top_genre": "Modern rock", - "top_artist": "Muse" - } -} \ No newline at end of file diff --git a/lib/dashboard.ts b/lib/dashboard.ts index 078e792..91b86c2 100644 --- a/lib/dashboard.ts +++ b/lib/dashboard.ts @@ -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", diff --git a/lib/localData.ts b/lib/localData.ts deleted file mode 100644 index 1028658..0000000 --- a/lib/localData.ts +++ /dev/null @@ -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') -} \ No newline at end of file diff --git a/lib/spotify.ts b/lib/spotify.ts index dbaa9b1..43a0dc8 100644 --- a/lib/spotify.ts +++ b/lib/spotify.ts @@ -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 + } } \ No newline at end of file