From ea6a312ab5bc606c791fabb4457d1d3ec4e2c8ac Mon Sep 17 00:00:00 2001 From: Arthur DANJOU Date: Mon, 17 Jan 2022 21:30:07 +0100 Subject: [PATCH] Remove expiration for credentials Add try / catch to counter error Add reset cache method Add github readme update when music change --- app/Utils/SongUtils.ts | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/app/Utils/SongUtils.ts b/app/Utils/SongUtils.ts index ec41d21..2e13e75 100644 --- a/app/Utils/SongUtils.ts +++ b/app/Utils/SongUtils.ts @@ -5,6 +5,7 @@ import { SpotifyArtist, SpotifyTrack } from 'App/Types/ILocalSpotify' import { Artist, InternalPlayerResponse, PlayerResponse, SpotifyToken } from 'App/Types/ISpotify' import Song from 'App/Models/Song' import queryString from 'query-string' +import { updateGithubReadmeSpotify } from 'App/Utils/UpdateGithubReadme' export async function getSpotifyAccount(): Promise { return await Redis.exists('spotify:account') @@ -20,7 +21,7 @@ export async function setSpotifyAccount(token: SpotifyToken): Promise { await Redis.set('spotify:account', JSON.stringify({ access_token: token.access_token, refresh_token: token.refresh_token, - }), 'ex', token.expires_in) + })) } export function getAuthorizationURI(): string { @@ -77,20 +78,23 @@ export async function regenerateTokens(): Promise { await setSpotifyAccount(authorization_tokens.data) } -async function RequestWrapper(url: string): Promise> { +async function RequestWrapper(url: string): Promise | undefined> { let request const options: AxiosRequestConfig = { headers: { Authorization: `Bearer ${(await getSpotifyAccount()).access_token}`, }, } - request = await axios.get(url, options) - - if (request.status !== 200) { + try { + request = await axios.get(url, options) + } + catch (error) { await regenerateTokens() request = await axios.get(url, options) } - return request + + if (request.status === 200) + return request } export async function getCurrentPlayingFromCache(): Promise { @@ -103,7 +107,7 @@ export async function getCurrentPlayingFromSpotify(): Promise { + await updateCurrentSong({ is_playing: false }) +} + export async function updateCurrentSong(song: InternalPlayerResponse): Promise { // const current = JSON.parse(await Redis.get('spotify/current') as string) await Redis.set('spotify:current', JSON.stringify(song)) + await updateGithubReadmeSpotify() // const changed = diff(current, song) // todo send message to Rabbit @@ -164,7 +173,7 @@ export async function getHistory(range: 'day' | 'week' | 'month' | 'total') { return { history: songs } } -export async function fetchTopArtist(): Promise { +export async function fetchTopArtist(): Promise { if (await Redis.exists('spotify:top:artists')) return JSON.parse(await Redis.get('spotify:top:artists') || '{}') @@ -172,7 +181,7 @@ export async function fetchTopArtist(): Promise { const artists: SpotifyArtist[] = [] - if (fetched_artists.data) { + if (fetched_artists) { for (const artist of fetched_artists.data.items) { artists.push({ id: artist.id, @@ -185,7 +194,9 @@ export async function fetchTopArtist(): Promise { } } else { - return [] + return { + artists: 'cannot_fetch_artists', + } } await Redis.set('spotify:top:artists', JSON.stringify({