mirror of
https://github.com/ArthurDanjou/artdanj-api.git
synced 2026-01-28 10:30:28 +01:00
Connect Athena to Spotify
This commit is contained in:
@@ -1,11 +1,237 @@
|
||||
export async function getHistory(range: 'day' | 'week' | 'month') {
|
||||
return range
|
||||
import { readFileSync, writeFileSync } from 'fs'
|
||||
import axios, { AxiosRequestConfig, AxiosResponse } from 'axios'
|
||||
import Env from '@ioc:Adonis/Core/Env'
|
||||
import Redis from '@ioc:Adonis/Addons/Redis'
|
||||
import { SpotifyArtist, SpotifyTrack } from 'App/Types/ILocalSpotify'
|
||||
import { Artist, InternalPlayerResponse, PlayerResponse, SpotifyToken } from 'App/Types/ISpotify'
|
||||
import Song from 'App/Models/Song'
|
||||
|
||||
export function getSpotifyAccount(): { access: string; refresh: string } {
|
||||
return JSON.parse(readFileSync('.config/.spotify').toString())
|
||||
}
|
||||
|
||||
export async function getTopTrack() {
|
||||
return 0
|
||||
export function getAuthorizationURI(): string {
|
||||
const query = JSON.stringify({
|
||||
response_type: 'code',
|
||||
client_id: Env.get('SPOTIFY_ID'),
|
||||
scope: encodeURIComponent('user-read-playback-state user-read-currently-playing'),
|
||||
redirect_uri: `${Env.get('BASE_URL')}/spotify/callback`,
|
||||
})
|
||||
|
||||
return `https://accounts.spotify.com/authorize?${query}`
|
||||
}
|
||||
|
||||
export async function GetCurrentPlaying() {
|
||||
return null
|
||||
export async function setupSpotify(code: string): Promise<void> {
|
||||
const authorization_tokens: AxiosResponse<SpotifyToken> = await axios.post(
|
||||
'https://accounts.spotify.com/api/token',
|
||||
{
|
||||
code,
|
||||
grant_type: 'authorization_code',
|
||||
redirect_uri: `${Env.get('BASE_URL')}/spotify/callback`,
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
'Authorization': `Basic ${Buffer.from(`${Env.get('SPOTIFY_ID')}:${Env.get('SPOTIFY_SECRET')}`).toString('base64')}`,
|
||||
'Content-Type': 'application/x-www-form-urlencoded',
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
if (authorization_tokens.status === 200) {
|
||||
writeFileSync(
|
||||
'.config/.spotify',
|
||||
JSON.stringify({
|
||||
access: authorization_tokens.data.access_token,
|
||||
refresh: authorization_tokens.data.refresh_token,
|
||||
}),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
export async function regenerateTokens(): Promise<void> {
|
||||
const refresh_token = getSpotifyAccount().refresh
|
||||
|
||||
const authorization_tokens: AxiosResponse<SpotifyToken> = await axios.post(
|
||||
'https://accounts.spotify.com/api/token',
|
||||
{
|
||||
grant_type: 'refresh_token',
|
||||
refresh_token,
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
'Authorization': `Basic ${Buffer.from(`${Env.get('SPOTIFY_ID')}:${Env.get('SPOTIFY_SECRET')}`).toString('base64')}`,
|
||||
'Content-Type': 'application/x-www-form-urlencoded',
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
if (authorization_tokens.status === 200) {
|
||||
writeFileSync(
|
||||
'.config/.spotify',
|
||||
JSON.stringify({
|
||||
access: authorization_tokens.data.access_token,
|
||||
refresh: authorization_tokens.data.refresh_token,
|
||||
}),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
async function RequestWrapper<T = never>(url: string): Promise<AxiosResponse<T>> {
|
||||
let request
|
||||
const options: AxiosRequestConfig = {
|
||||
headers: {
|
||||
Authorization: `Bearer ${getSpotifyAccount().access}`,
|
||||
},
|
||||
}
|
||||
request = await axios.get<T>(url, options)
|
||||
|
||||
if (request.status === 401) {
|
||||
await regenerateTokens()
|
||||
request = await axios.get<T>(url, options)
|
||||
}
|
||||
return request
|
||||
}
|
||||
|
||||
export async function getCurrentPlayingFromCache(): Promise<InternalPlayerResponse> {
|
||||
return JSON.parse(await Redis.get('spotify:current') || '') || { is_playing: false }
|
||||
}
|
||||
|
||||
export async function getCurrentPlayingFromSpotify(): Promise<InternalPlayerResponse> {
|
||||
const current_track = await RequestWrapper<PlayerResponse>('https://api.spotify.com/v1/me/player?additional_types=track,episode')
|
||||
|
||||
let current: InternalPlayerResponse
|
||||
|
||||
if (current_track.data && !['track', 'episode'].includes(current_track.data.currently_playing_type))
|
||||
current = { is_playing: false }
|
||||
|
||||
if (current_track.data && current_track.data.is_playing) {
|
||||
current = {
|
||||
is_playing: true,
|
||||
device_name: current_track.data.device.name,
|
||||
device_type: current_track.data.device.type,
|
||||
name: current_track.data.item.name,
|
||||
type: current_track.data.item.type,
|
||||
author: current_track.data.item.artists.map(artist => artist.name).join(', '),
|
||||
id: current_track.data.item.id,
|
||||
image: current_track.data.item.album.images[0],
|
||||
progress: current_track.data.progress_ms,
|
||||
duration: current_track.data.item.duration_ms,
|
||||
started_at: current_track.data.timestamp,
|
||||
}
|
||||
}
|
||||
else {
|
||||
current = { is_playing: false }
|
||||
}
|
||||
|
||||
if ((await Redis.get('spotify:current') as string) !== JSON.stringify(current))
|
||||
await updateCurrentSong(current)
|
||||
|
||||
return current
|
||||
}
|
||||
|
||||
export async function updateCurrentSong(song: InternalPlayerResponse): Promise<void> {
|
||||
// const current = JSON.parse(await Redis.get('spotify/current') as string)
|
||||
await Redis.set('spotify:current', JSON.stringify(song))
|
||||
|
||||
// const changed = diff(current, song)
|
||||
// todo send message to Rabbit
|
||||
}
|
||||
|
||||
export async function getHistory(range: 'day' | 'week' | 'month' | 'total') {
|
||||
if (await Redis.exists(`spotify:history:range:${range || 'day'}`))
|
||||
return JSON.parse(await Redis.get(`spotify:history:range:${range || 'day'}`) || '')
|
||||
|
||||
let startDate = new Date(new Date().getTime() - 24 * 60 * 60 * 1000)
|
||||
if (range === 'week') startDate = new Date(new Date().getTime() - 7 * 24 * 60 * 60 * 1000)
|
||||
else if (range === 'month') startDate = new Date(new Date().setMonth(new Date().getMonth() - 1))
|
||||
|
||||
const endDate = new Date()
|
||||
|
||||
const songs = await Song
|
||||
.query()
|
||||
.where('date', '<=', endDate)
|
||||
.where('date', '>=', startDate)
|
||||
.orderBy('date', 'desc')
|
||||
|
||||
if (songs.length <= 0)
|
||||
return { history: 'no_tracks_in_that_range' }
|
||||
|
||||
await Redis.set(`spotify:history:range:${range || 'day'}`, JSON.stringify({
|
||||
cached: new Date().toUTCString(),
|
||||
history: songs,
|
||||
}), 'ex', 300)
|
||||
|
||||
return { history: songs }
|
||||
}
|
||||
|
||||
export async function fetchTopArtist(): Promise<SpotifyArtist[]> {
|
||||
if (await Redis.exists('spotify:top:artists'))
|
||||
return JSON.parse(await Redis.get('spotify:top:artists') || '')
|
||||
|
||||
const fetched_artists = await RequestWrapper<{ items: Artist[] }>('https://api.spotify.com/v1/me/top/type/artists?limit=5')
|
||||
|
||||
const artists: SpotifyArtist[] = []
|
||||
|
||||
if (fetched_artists.data) {
|
||||
for (const artist of fetched_artists.data.items) {
|
||||
artists.push({
|
||||
id: artist.id,
|
||||
image: artist.images[0].url,
|
||||
name: artist.name,
|
||||
followers: artist.followers.total,
|
||||
genres: artist.genres,
|
||||
popularity: artist.popularity,
|
||||
})
|
||||
}
|
||||
}
|
||||
else {
|
||||
return []
|
||||
}
|
||||
|
||||
await Redis.set('spotify:top:artists', JSON.stringify({
|
||||
cached: new Date().toUTCString(),
|
||||
top: artists,
|
||||
}), 'ex', 600)
|
||||
|
||||
return artists
|
||||
}
|
||||
|
||||
export async function fetchTopTrack(): Promise<SpotifyTrack[]> {
|
||||
if (await Redis.exists('spotify:top:tracks'))
|
||||
return JSON.parse(await Redis.get('spotify:top:tracks') || '')
|
||||
|
||||
const fetched_tracks = await Song
|
||||
.query()
|
||||
.orderBy('date', 'desc')
|
||||
.limit(5)
|
||||
|
||||
const tracks: SpotifyTrack[] = []
|
||||
|
||||
if (fetched_tracks.length >= 0) {
|
||||
for (const track of fetched_tracks) {
|
||||
tracks.push({
|
||||
item: {
|
||||
name: track.item_name,
|
||||
type: track.item_type,
|
||||
},
|
||||
device: {
|
||||
name: track.device_name,
|
||||
type: track.device_type,
|
||||
},
|
||||
duration: track.duration,
|
||||
author: track.author,
|
||||
image: track.image,
|
||||
})
|
||||
}
|
||||
}
|
||||
else {
|
||||
return []
|
||||
}
|
||||
|
||||
await Redis.set('spotify:top:tracks', JSON.stringify({
|
||||
cached: new Date().toUTCString(),
|
||||
top: tracks,
|
||||
}), 'ex', 300)
|
||||
|
||||
return tracks
|
||||
}
|
||||
|
||||
40
app/Utils/StatesTask.ts
Normal file
40
app/Utils/StatesTask.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
import { btoa } from 'buffer'
|
||||
import axios from 'axios'
|
||||
import Env from '@ioc:Adonis/Core/Env'
|
||||
import Redis from '@ioc:Adonis/Addons/Redis'
|
||||
import Logger from '@ioc:Adonis/Core/Logger'
|
||||
|
||||
interface StatesResponse {
|
||||
time: number
|
||||
}
|
||||
|
||||
export async function fetchDevelopingState(): Promise<void> {
|
||||
try {
|
||||
const response = await axios.get<{ data: StatesResponse[]}>(`https://wakatime.com/api/v1/users/${Env.get('WAKATIME_USER')}/heartbeats`, {
|
||||
headers: {
|
||||
Authorization: `Basic ${btoa(Env.get('WAKATIME_KEY'))}`,
|
||||
},
|
||||
params: {
|
||||
date: new Date(),
|
||||
},
|
||||
})
|
||||
|
||||
if (response.status === 200) {
|
||||
const heartbeat = response.data.data[response.data.data.length - 1]
|
||||
const current_time = new Date(Date.now()).getTime() / 1000
|
||||
|
||||
if (heartbeat && heartbeat.time!) {
|
||||
const active = current_time - heartbeat.time <= 60 * 5 // Less than 5 min.
|
||||
const redis_state = await Redis.get('states:developing') === 'true'
|
||||
|
||||
if (redis_state !== active) {
|
||||
await Redis.set('states:developing', String(active))
|
||||
if (redis_state) await Redis.set('states:sleeping', 'false')
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (error) {
|
||||
Logger.error('Error while getting the states')
|
||||
}
|
||||
}
|
||||
@@ -1,23 +1,7 @@
|
||||
import DevelopmentHour from 'App/Models/DevelopmentHour'
|
||||
import CommandsRun from 'App/Models/CommandsRun'
|
||||
import BuildsRun from 'App/Models/BuildsRun'
|
||||
|
||||
interface Time {
|
||||
hours: number
|
||||
minutes: number
|
||||
seconds: number
|
||||
}
|
||||
|
||||
export interface Stats {
|
||||
range: {
|
||||
start: string
|
||||
end: string
|
||||
}
|
||||
|
||||
development_time: Time
|
||||
commands_ran: number
|
||||
builds_ran: number
|
||||
}
|
||||
import { Stats, Time } from 'App/Types/IStats'
|
||||
|
||||
function formatDate(date: Date): string {
|
||||
return date.toISOString().split('T')[0]
|
||||
|
||||
Reference in New Issue
Block a user