From 4dc2e16f079269327e7318d732e5876e300677ea Mon Sep 17 00:00:00 2001
From: yanosea
Date: Mon, 1 Apr 2024 00:39:22 +0900
Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A8refactor:=20refactor=20spotify=20st?=
=?UTF-8?q?atus=20implement?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.env | 2 -
front/src/components/Footer.astro | 118 +---------------
front/src/components/SpotifyStatus.astro | 40 +++---
front/src/libs/spotify.ts | 173 ++++++++++++++++++-----
4 files changed, 159 insertions(+), 174 deletions(-)
diff --git a/.env b/.env
index d218424..da25da8 100644
--- a/.env
+++ b/.env
@@ -6,8 +6,6 @@ FRONT_SSL_CRT_FILE_NAME=
FRONT_SSL_PRIVATE_KEY_FILE_NAME=
BACK_PORT=
BACK_CONTAINER_NAME=
-NOWPLAYING_URL=
-LASTPLAYED_URL=
SPOTIFY_ID=
SPOTIFY_SECRET=
SPOTIFY_REDIRECT_URI=
diff --git a/front/src/components/Footer.astro b/front/src/components/Footer.astro
index 2e05558..1d71ef0 100644
--- a/front/src/components/Footer.astro
+++ b/front/src/components/Footer.astro
@@ -24,118 +24,8 @@ const year = new Date().getFullYear();
-
diff --git a/front/src/components/SpotifyStatus.astro b/front/src/components/SpotifyStatus.astro
index e3520ec..d629e0e 100644
--- a/front/src/components/SpotifyStatus.astro
+++ b/front/src/components/SpotifyStatus.astro
@@ -2,14 +2,14 @@
import { Icon } from "astro-icon/components";
---
-
+
-
+
@@ -18,7 +18,7 @@ import { Icon } from "astro-icon/components";
now playing
yanosea online!
-
+
-
+
-
+
-
+
-
+
-
+
-
+
@@ -74,7 +74,7 @@ import { Icon } from "astro-icon/components";
last played
-
+
-
+
-
+
-
+
-
+
-
+
-
+
diff --git a/front/src/libs/spotify.ts b/front/src/libs/spotify.ts
index 4f32225..ef5b67b 100644
--- a/front/src/libs/spotify.ts
+++ b/front/src/libs/spotify.ts
@@ -1,48 +1,145 @@
import type { Track } from "@/libs/types/track";
-export async function getTrackInfo(): Promise<[Boolean, Track]> {
- let isNowPlaying: Boolean = null;
- let track: Track = null;
+export async function updateSpotifyStatus() {
+ const spotifyStatusElement = document.getElementById("spotifyStatus");
+ const [isNowPlaying, track] = await getTrackInfo();
+ if (!isNowPlaying && !track) {
+ spotifyStatusElement.style.display = "none";
+ console.error("failed getting track info...");
+ return;
+ }
+
+ spotifyStatusElement.style.display = "flex";
+ if (isNowPlaying) {
+ showNowPlaying(spotifyStatusElement, track);
+ } else {
+ showLastPlayed(spotifyStatusElement, track);
+ }
+}
+
+async function getTrackInfo(): Promise<[Boolean, Track | undefined]> {
+ const [nowPlayingUrl, lastPlayedUrl] = defineFetchUrl();
try {
- const nowPlayingResponse = await fetch(process.env.NOWPLAYING_URL);
- if (nowPlayingResponse.ok && nowPlayingResponse.status !== 204) {
- const nowPlayingData = await nowPlayingResponse.json();
- isNowPlaying = true;
- track = {
- imageUrl: nowPlayingData.imageUrl,
- trackName: nowPlayingData.trackName,
- trackUrl: nowPlayingData.trackUrl,
- albumName: nowPlayingData.albumName,
- albumUrl: nowPlayingData.albumUrl,
- artistName: nowPlayingData.artistName,
- artistUrl: nowPlayingData.artistUrl,
- };
+ const nowPlayingResponse = await fetchData(nowPlayingUrl || "");
+ console.log("fetched nowplaying!");
+ if (nowPlayingResponse) {
+ return [true, await extractTrackData(nowPlayingResponse, true)];
}
- } catch (error) {
- console.error(error);
+ } catch {
+ console.error("failed fetching nowplaying...");
}
- if (isNowPlaying == null) {
- try {
- const lastPlayedResponse = await fetch(process.env.LASTPLAYED_URL);
- if (lastPlayedResponse.ok && lastPlayedResponse.status !== 204) {
- const lastPlayedData = await lastPlayedResponse.json();
- isNowPlaying = false;
- track = {
- imageUrl: lastPlayedData.imageUrl,
- playedAt: lastPlayedData.playedAt,
- trackName: lastPlayedData.trackName,
- trackUrl: lastPlayedData.trackUrl,
- albumName: lastPlayedData.albumName,
- albumUrl: lastPlayedData.albumUrl,
- artistName: lastPlayedData.artistName,
- artistUrl: lastPlayedData.artistUrl,
- };
- }
- } catch (error) {
- console.error(error);
+ try {
+ const lastPlayedResponse = await fetchData(lastPlayedUrl || "");
+ console.log("fetched lastplayed!");
+ if (lastPlayedResponse) {
+ return [false, await extractTrackData(lastPlayedResponse, false)];
}
+ } catch {
+ console.error("failed fetching lastplayed...");
}
- return [isNowPlaying, track];
+ return [false, undefined];
+}
+
+function defineFetchUrl(): [string, string] {
+ const isDevelopment = window.location.hostname === "localhost" ||
+ window.location.hostname === "127.0.0.1";
+ const nowPlayingUrl = isDevelopment
+ ? "http://localhost:1323/api/nowplaying"
+ : "https://yanosea.org/api/nowplaying";
+ const lastplayedUrl = isDevelopment
+ ? "http://localhost:1323/api/lastplayed"
+ : "https://yanosea.org/api/lastplayed";
+ return [nowPlayingUrl, lastplayedUrl];
+}
+
+async function fetchData(url: string): Promise {
+ try {
+ const response = await fetch(url);
+ if (response.ok && response.status !== 204) {
+ return response;
+ }
+ } catch (error) {
+ console.error(`failed fetching from ${url}`);
+ return null;
+ }
+}
+
+async function extractTrackData(
+ response: Response,
+ nowPlaying: boolean,
+): Promise