mirror of
https://github.com/ArthurDanjou/artdanj-api.git
synced 2026-01-28 02:20:27 +01:00
Connect Athena to Spotify
This commit is contained in:
16
app/Tasks/CurrentSongTask.ts
Normal file
16
app/Tasks/CurrentSongTask.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import Logger from '@ioc:Adonis/Core/Logger'
|
||||
import { getCurrentPlayingFromSpotify } from 'App/Utils/SongUtils'
|
||||
|
||||
const MS = 1000
|
||||
let taskId
|
||||
|
||||
export async function Activate(): Promise<void> {
|
||||
Logger.info(`Starting task runner for watching spotify current playing [${MS} ms]`)
|
||||
await getCurrentPlayingFromSpotify()
|
||||
taskId = setInterval(getCurrentPlayingFromSpotify, MS)
|
||||
}
|
||||
|
||||
export function ShutDown(): void {
|
||||
clearInterval(taskId)
|
||||
Logger.info('Shutdown task runner for getting current developing state')
|
||||
}
|
||||
40
app/Tasks/HistorySongsTask.ts
Normal file
40
app/Tasks/HistorySongsTask.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
import Logger from '@ioc:Adonis/Core/Logger'
|
||||
import { getCurrentPlayingFromCache } from 'App/Utils/SongUtils'
|
||||
import Song from 'App/Models/Song'
|
||||
|
||||
const MS = 10000
|
||||
let taskId
|
||||
|
||||
async function LogSpotifyHistory(): Promise<void> {
|
||||
const current = await getCurrentPlayingFromCache()
|
||||
|
||||
if (!current.is_playing) return
|
||||
|
||||
if (current.progress && current.progress < 1000) return
|
||||
|
||||
const last_entry = await Song.query().where('id', current.id!).orderBy('date', 'desc').first()
|
||||
|
||||
if (last_entry && new Date().getTime() - last_entry.duration <= new Date(last_entry.date).getTime()) return
|
||||
|
||||
await Song.create({
|
||||
date: current.started_at,
|
||||
duration: current.duration,
|
||||
item_name: current.name,
|
||||
item_type: current.type,
|
||||
author: current.author,
|
||||
device_name: current.device_name,
|
||||
device_type: current.device_type,
|
||||
image: current.image?.url,
|
||||
})
|
||||
}
|
||||
|
||||
export async function Activate(): Promise<void> {
|
||||
Logger.info(`Starting task runner for tracking spotify listen history [${MS} ms]`)
|
||||
await LogSpotifyHistory()
|
||||
taskId = setInterval(LogSpotifyHistory, MS)
|
||||
}
|
||||
|
||||
export function ShutDown(): void {
|
||||
clearInterval(taskId)
|
||||
Logger.info('Shutdown task runner for getting current developing state')
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
import Logger from '@ioc:Adonis/Core/Logger'
|
||||
|
||||
const MS = 1000
|
||||
|
||||
export async function getCurrentPlayingMusic(): Promise<void> {
|
||||
// Fetch from deezer
|
||||
}
|
||||
|
||||
export async function Activate(): Promise<void> {
|
||||
Logger.info(`Starting task runner for watching deezer current playing [${MS} ms]`)
|
||||
await getCurrentPlayingMusic()
|
||||
setInterval(getCurrentPlayingMusic, MS)
|
||||
}
|
||||
@@ -1,51 +1,13 @@
|
||||
import { btoa } from 'buffer'
|
||||
import axios from 'axios'
|
||||
import Env from '@ioc:Adonis/Core/Env'
|
||||
import Logger from '@ioc:Adonis/Core/Logger'
|
||||
import Redis from '@ioc:Adonis/Addons/Redis'
|
||||
import { fetchDevelopingState } from 'App/Utils/StatesTask'
|
||||
|
||||
const MS = 1000 * 2 * 60 // 2 min
|
||||
let taskId
|
||||
|
||||
interface StatesResponse {
|
||||
time: number
|
||||
}
|
||||
|
||||
async function getCurrentTime(): 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')
|
||||
}
|
||||
}
|
||||
|
||||
export async function Activate(): Promise<void> {
|
||||
Logger.info(`Starting task runner for getting current developing state [every ${MS} ms]`)
|
||||
await getCurrentTime()
|
||||
taskId = setInterval(getCurrentTime, MS)
|
||||
await fetchDevelopingState()
|
||||
taskId = setInterval(fetchDevelopingState, MS)
|
||||
}
|
||||
|
||||
export function ShutDown(): void {
|
||||
|
||||
Reference in New Issue
Block a user