-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
112 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { CLIENT_ID, CLIENT_SECRET, REFRESH_TOKEN } from '$env/static/private'; | ||
import { json } from '@sveltejs/kit'; | ||
|
||
import querystring from 'querystring'; | ||
const TOKEN_ENDPOINT = `https://accounts.spotify.com/api/token`; | ||
const NOW_PLAYING_ENDPOINT = `https://api.spotify.com/v1/me/player/currently-playing`; | ||
|
||
const client_id = CLIENT_ID; | ||
const client_secret = CLIENT_SECRET; | ||
const refresh_token = REFRESH_TOKEN; | ||
|
||
const getAccessToken = async () => { | ||
const basic = Buffer.from(`${client_id}:${client_secret}`).toString('base64'); | ||
const response = await fetch(TOKEN_ENDPOINT, { | ||
method: 'POST', | ||
headers: { | ||
Authorization: `Basic ${basic}`, | ||
'Content-Type': 'application/x-www-form-urlencoded' | ||
}, | ||
body: querystring.stringify({ | ||
grant_type: 'refresh_token', | ||
refresh_token | ||
}) | ||
}); | ||
|
||
return response.json(); | ||
}; | ||
|
||
const getNowPlaying = async () => { | ||
const { access_token } = await getAccessToken(); | ||
return fetch(NOW_PLAYING_ENDPOINT, { | ||
headers: { | ||
Authorization: `Bearer ${access_token}` | ||
} | ||
}); | ||
}; | ||
|
||
async function getNowPlayingItem() { | ||
const response = await getNowPlaying(); | ||
if (response.status === 204 || response.status > 400) { | ||
return false; | ||
} | ||
const song = await response.json(); | ||
const albumImageUrl = song.item.album.images[0].url; | ||
const artist = song.item.artists | ||
.map( | ||
( | ||
//@ts-ignore | ||
_artist | ||
) => _artist.name | ||
) | ||
.join(', '); | ||
const isPlaying = song.is_playing; | ||
const songUrl = song.item.external_urls.spotify; | ||
const title = song.item.name; | ||
|
||
return { | ||
albumImageUrl, | ||
artist, | ||
isPlaying, | ||
songUrl, | ||
title | ||
}; | ||
} | ||
|
||
export const GET = async () => { | ||
const nowPlaying = await getNowPlayingItem(); | ||
|
||
return json(nowPlaying); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,71 +1,21 @@ | ||
interface Timestamps { | ||
start: number; | ||
end: number; | ||
} | ||
|
||
interface SpotifyData { | ||
track_id: string; | ||
timestamps: Timestamps; | ||
album: string; | ||
album_art_url: string; | ||
artist: string; | ||
song: string; | ||
} | ||
|
||
interface DiscordUser { | ||
id: string; | ||
username: string; | ||
avatar: string; | ||
discriminator: string; | ||
bot: boolean; | ||
global_name: string; | ||
avatar_decoration_data: null | any; // Replace 'any' with the actual type if known | ||
display_name: string; | ||
public_flags: number; | ||
} | ||
export type NowPlaying = | ||
| false | ||
| { | ||
albumImageUrl: string; | ||
artist: string; | ||
isPlaying: boolean; | ||
songUrl: string; | ||
title: string; | ||
}; | ||
|
||
interface Activity { | ||
flags: number; | ||
id: string; | ||
name: string; | ||
type: number; | ||
state: string; | ||
session_id: string; | ||
details: string; | ||
timestamps: Timestamps; | ||
assets: { | ||
large_image: string; | ||
large_text: string; | ||
}; | ||
sync_id: string; | ||
created_at: number; | ||
party: { | ||
id: string; | ||
}; | ||
} | ||
export async function getSpotifyData() { | ||
if (typeof window === 'undefined') return false; | ||
|
||
export interface UserData { | ||
data: { | ||
kv: Record<string, any>; // Replace 'any' with the actual type if known | ||
spotify?: SpotifyData; | ||
discord_user: DiscordUser; | ||
activities: Activity[]; | ||
discord_status: string; | ||
active_on_discord_web: boolean; | ||
active_on_discord_desktop: boolean; | ||
active_on_discord_mobile: boolean; | ||
listening_to_spotify: boolean; | ||
}; | ||
success: boolean; | ||
} | ||
const response = await fetch('/api/'); | ||
|
||
const API_URL = 'https://api.lanyard.rest/v1/users/961161387101536296'; | ||
const data: NowPlaying = await response.json(); | ||
|
||
export async function getSpotifyData() { | ||
//@ts-ignore | ||
if (typeof window === 'undefined') return { data: null }; | ||
const response = await fetch(API_URL + '?__cacheIgnore=' + Math.random()); | ||
const data: UserData = await response.json(); | ||
if (!data) return false; | ||
|
||
return data; | ||
} |