mirror of
https://github.com/ArthurDanjou/artdanj-api.git
synced 2026-01-14 12:14:33 +01:00
Remove expiration for credentials
Add try / catch to counter error Add reset cache method Add github readme update when music change
This commit is contained in:
@@ -5,6 +5,7 @@ import { SpotifyArtist, SpotifyTrack } from 'App/Types/ILocalSpotify'
|
|||||||
import { Artist, InternalPlayerResponse, PlayerResponse, SpotifyToken } from 'App/Types/ISpotify'
|
import { Artist, InternalPlayerResponse, PlayerResponse, SpotifyToken } from 'App/Types/ISpotify'
|
||||||
import Song from 'App/Models/Song'
|
import Song from 'App/Models/Song'
|
||||||
import queryString from 'query-string'
|
import queryString from 'query-string'
|
||||||
|
import { updateGithubReadmeSpotify } from 'App/Utils/UpdateGithubReadme'
|
||||||
|
|
||||||
export async function getSpotifyAccount(): Promise<SpotifyToken> {
|
export async function getSpotifyAccount(): Promise<SpotifyToken> {
|
||||||
return await Redis.exists('spotify:account')
|
return await Redis.exists('spotify:account')
|
||||||
@@ -20,7 +21,7 @@ export async function setSpotifyAccount(token: SpotifyToken): Promise<void> {
|
|||||||
await Redis.set('spotify:account', JSON.stringify({
|
await Redis.set('spotify:account', JSON.stringify({
|
||||||
access_token: token.access_token,
|
access_token: token.access_token,
|
||||||
refresh_token: token.refresh_token,
|
refresh_token: token.refresh_token,
|
||||||
}), 'ex', token.expires_in)
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getAuthorizationURI(): string {
|
export function getAuthorizationURI(): string {
|
||||||
@@ -77,20 +78,23 @@ export async function regenerateTokens(): Promise<void> {
|
|||||||
await setSpotifyAccount(authorization_tokens.data)
|
await setSpotifyAccount(authorization_tokens.data)
|
||||||
}
|
}
|
||||||
|
|
||||||
async function RequestWrapper<T = never>(url: string): Promise<AxiosResponse<T>> {
|
async function RequestWrapper<T = never>(url: string): Promise<AxiosResponse<T> | undefined> {
|
||||||
let request
|
let request
|
||||||
const options: AxiosRequestConfig = {
|
const options: AxiosRequestConfig = {
|
||||||
headers: {
|
headers: {
|
||||||
Authorization: `Bearer ${(await getSpotifyAccount()).access_token}`,
|
Authorization: `Bearer ${(await getSpotifyAccount()).access_token}`,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
request = await axios.get<T>(url, options)
|
try {
|
||||||
|
request = await axios.get<T>(url, options)
|
||||||
if (request.status !== 200) {
|
}
|
||||||
|
catch (error) {
|
||||||
await regenerateTokens()
|
await regenerateTokens()
|
||||||
request = await axios.get<T>(url, options)
|
request = await axios.get<T>(url, options)
|
||||||
}
|
}
|
||||||
return request
|
|
||||||
|
if (request.status === 200)
|
||||||
|
return request
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getCurrentPlayingFromCache(): Promise<InternalPlayerResponse> {
|
export async function getCurrentPlayingFromCache(): Promise<InternalPlayerResponse> {
|
||||||
@@ -103,7 +107,7 @@ export async function getCurrentPlayingFromSpotify(): Promise<InternalPlayerResp
|
|||||||
|
|
||||||
let current: InternalPlayerResponse
|
let current: InternalPlayerResponse
|
||||||
|
|
||||||
if (current_track.data && current_track.data.is_playing) {
|
if (current_track && current_track.data && current_track.data.is_playing) {
|
||||||
current = {
|
current = {
|
||||||
is_playing: true,
|
is_playing: true,
|
||||||
device_name: current_track.data.device.name,
|
device_name: current_track.data.device.name,
|
||||||
@@ -128,9 +132,14 @@ export async function getCurrentPlayingFromSpotify(): Promise<InternalPlayerResp
|
|||||||
return current
|
return current
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function resetCurrentSongCache(): Promise<void> {
|
||||||
|
await updateCurrentSong({ is_playing: false })
|
||||||
|
}
|
||||||
|
|
||||||
export async function updateCurrentSong(song: InternalPlayerResponse): Promise<void> {
|
export async function updateCurrentSong(song: InternalPlayerResponse): Promise<void> {
|
||||||
// const current = JSON.parse(await Redis.get('spotify/current') as string)
|
// const current = JSON.parse(await Redis.get('spotify/current') as string)
|
||||||
await Redis.set('spotify:current', JSON.stringify(song))
|
await Redis.set('spotify:current', JSON.stringify(song))
|
||||||
|
await updateGithubReadmeSpotify()
|
||||||
|
|
||||||
// const changed = diff(current, song)
|
// const changed = diff(current, song)
|
||||||
// todo send message to Rabbit
|
// todo send message to Rabbit
|
||||||
@@ -164,7 +173,7 @@ export async function getHistory(range: 'day' | 'week' | 'month' | 'total') {
|
|||||||
return { history: songs }
|
return { history: songs }
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function fetchTopArtist(): Promise<SpotifyArtist[]> {
|
export async function fetchTopArtist(): Promise<SpotifyArtist[] | { artists: string }> {
|
||||||
if (await Redis.exists('spotify:top:artists'))
|
if (await Redis.exists('spotify:top:artists'))
|
||||||
return JSON.parse(await Redis.get('spotify:top:artists') || '{}')
|
return JSON.parse(await Redis.get('spotify:top:artists') || '{}')
|
||||||
|
|
||||||
@@ -172,7 +181,7 @@ export async function fetchTopArtist(): Promise<SpotifyArtist[]> {
|
|||||||
|
|
||||||
const artists: SpotifyArtist[] = []
|
const artists: SpotifyArtist[] = []
|
||||||
|
|
||||||
if (fetched_artists.data) {
|
if (fetched_artists) {
|
||||||
for (const artist of fetched_artists.data.items) {
|
for (const artist of fetched_artists.data.items) {
|
||||||
artists.push({
|
artists.push({
|
||||||
id: artist.id,
|
id: artist.id,
|
||||||
@@ -185,7 +194,9 @@ export async function fetchTopArtist(): Promise<SpotifyArtist[]> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
return []
|
return {
|
||||||
|
artists: 'cannot_fetch_artists',
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
await Redis.set('spotify:top:artists', JSON.stringify({
|
await Redis.set('spotify:top:artists', JSON.stringify({
|
||||||
|
|||||||
Reference in New Issue
Block a user