✨ (spotify) add spotify player
This commit is contained in:
54
src/utils/spotify.ts
Normal file
54
src/utils/spotify.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
const LOFI = 'spotify:playlist:0vvXsWCC9xrXsKd4FyS8kM'
|
||||
const LOFI_MAX = 250
|
||||
|
||||
const random = (max: number) => {
|
||||
return Math.floor(Math.random() * Math.floor(max))
|
||||
}
|
||||
|
||||
export const redirectToSpotifyConnect = () => {
|
||||
const authorizeURL = new URL('https://accounts.spotify.com/authorize')
|
||||
authorizeURL.searchParams.append(
|
||||
'client_id',
|
||||
process.env.VUE_APP_SPOTIFY_CLIENT_ID || ''
|
||||
)
|
||||
authorizeURL.searchParams.append('response_type', 'token')
|
||||
authorizeURL.searchParams.append('show_dialog', 'false')
|
||||
authorizeURL.searchParams.append('scope', 'user-modify-playback-state')
|
||||
authorizeURL.searchParams.append('redirect_uri', `${location.origin}/spotify`)
|
||||
|
||||
location.href = authorizeURL.toString()
|
||||
}
|
||||
|
||||
export const playOnSpotify = async (token: string, deviceId: string) => {
|
||||
await fetch(
|
||||
`https://api.spotify.com/v1/me/player/play?device_id=${deviceId}`,
|
||||
{
|
||||
method: 'PUT',
|
||||
headers: {
|
||||
accept: 'application/json',
|
||||
'content-type': 'application/json',
|
||||
authorization: `Bearer ${token}`
|
||||
},
|
||||
body: JSON.stringify({
|
||||
context_uri: LOFI,
|
||||
offset: {
|
||||
position: random(LOFI_MAX)
|
||||
}
|
||||
})
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
export const pauseOnSpotify = async (token: string, deviceId: string) => {
|
||||
await fetch(
|
||||
`https://api.spotify.com/v1/me/player/pause?device_id=${deviceId}`,
|
||||
{
|
||||
method: 'PUT',
|
||||
headers: {
|
||||
accept: 'application/json',
|
||||
'content-type': 'application/json',
|
||||
authorization: `Bearer ${token}`
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user