From 49cd2b87355a0031b1376f1842d894c19f0f0d67 Mon Sep 17 00:00:00 2001 From: Brandon4466 Date: Tue, 7 May 2024 13:06:21 -0700 Subject: [PATCH] added animated fading background --- .syncedlyrics/musixmatch_token.json | 2 +- spotifycontroller.py | 49 +++++++++++++++-------------- templates/webapp.html | 36 ++++++++++++++++----- 3 files changed, 54 insertions(+), 33 deletions(-) diff --git a/.syncedlyrics/musixmatch_token.json b/.syncedlyrics/musixmatch_token.json index 2818718..d7bd2e8 100644 --- a/.syncedlyrics/musixmatch_token.json +++ b/.syncedlyrics/musixmatch_token.json @@ -1 +1 @@ -{"token": "240425a54f5df91735a14b241dcfb1e5df28f942d95b9cad8ea6a4", "expiration_time": 1714086845} \ No newline at end of file +{"token": "240507dc1c1d721b422f329d191594415da1e4aec72fa0405c547e", "expiration_time": 1715112558} \ No newline at end of file diff --git a/spotifycontroller.py b/spotifycontroller.py index e3c710e..74ee260 100644 --- a/spotifycontroller.py +++ b/spotifycontroller.py @@ -104,31 +104,32 @@ def appdata(): currently_playing = requests.get("https://api.spotify.com/v1/me/player/currently-playing", headers=user_headers) if currently_playing.content: - if currently_playing.json()["is_playing"] is True and currently_playing.json()["item"]["id"] == request.args.get('id'): - print("QUICK" + str(time.time() - stime)) - return { 'progress_ms': currently_playing.json()["progress_ms"], + if "is_playing" in currently_playing.json(): + if currently_playing.json()["is_playing"] is True and currently_playing.json()["item"]["id"] == request.args.get('id'): + print("QUICK" + str(time.time() - stime)) + return { 'progress_ms': currently_playing.json()["progress_ms"], + } + elif currently_playing.json()["is_playing"] is True and currently_playing.json()["item"]["id"] != request.args.get('id'): + print("FULL" + str(time.time() - stime)) + return { 'id': currently_playing.json()["item"]["id"], + 'name': currently_playing.json()["item"]["name"], + 'artist': currently_playing.json()["item"]["artists"][0]["name"], + 'album': currently_playing.json()["item"]["album"]["name"], + 'image': currently_playing.json()["item"]["album"]["images"][0]["url"], + 'is_playing': currently_playing.json()["is_playing"], + 'progress_ms': currently_playing.json()["progress_ms"], + 'duration_ms': currently_playing.json()["item"]["duration_ms"], + 'is_liked': requests.get("https://api.spotify.com/v1/me/tracks/contains?ids=" + currently_playing.json()["item"]["id"], headers=user_headers).json()[0], + 'canvas': False, + 'gofetch': 'fetch' + } + elif currently_playing.json()["is_playing"] is False: + return { 'is_playing': False + } + else: + return { 'name': "Error", + 'artist': "Error" } - elif currently_playing.json()["is_playing"] is True and currently_playing.json()["item"]["id"] != request.args.get('id'): - print("FULL" + str(time.time() - stime)) - return { 'id': currently_playing.json()["item"]["id"], - 'name': currently_playing.json()["item"]["name"], - 'artist': currently_playing.json()["item"]["artists"][0]["name"], - 'album': currently_playing.json()["item"]["album"]["name"], - 'image': currently_playing.json()["item"]["album"]["images"][0]["url"], - 'is_playing': currently_playing.json()["is_playing"], - 'progress_ms': currently_playing.json()["progress_ms"], - 'duration_ms': currently_playing.json()["item"]["duration_ms"], - 'is_liked': requests.get("https://api.spotify.com/v1/me/tracks/contains?ids=" + currently_playing.json()["item"]["id"], headers=user_headers).json()[0], - 'canvas': False, - 'fetchlyrics': 'fetch' - } - elif currently_playing.json()["is_playing"] is False: - return { 'is_playing': False - } - else: - return { 'name': "Error", - 'artist': "Error" - } else: return { 'is_playing': False } diff --git a/templates/webapp.html b/templates/webapp.html index 2a4199f..6a9c7b0 100644 --- a/templates/webapp.html +++ b/templates/webapp.html @@ -1,5 +1,5 @@ - + @@ -7,6 +7,18 @@ - +
@@ -174,8 +186,10 @@ document.getElementById('canvas_url').style.display = "none"; document.getElementById('image').style.display = "flex"; } - getColors(data['image']) - getLyrics(data['name'], data['artist'], data['progress_ms'], data['fetchlyrics']) + if (data['gofetch'] !== undefined) { + getColors(data['image']) + } + getLyrics(data['name'], data['artist'], data['progress_ms'], data['gofetch']) } }); } @@ -200,8 +214,8 @@ } }); } - function getLyrics(name, artist, progress_ms, fetchlyrics) { - if (lyrics == undefined || fetchlyrics !== undefined && name !== undefined) { + function getLyrics(name, artist, progress_ms, gofetch) { + if (lyrics == undefined || gofetch !== undefined && name !== undefined) { $('#lyric').text(''); $.ajax({ url: '/lyrics', @@ -229,15 +243,21 @@ const colorThief = new ColorThief(); const img = document.getElementById('image'); img.crossOrigin = 'Anonymous'; + animatedBackground = document.querySelector('.animated-background'); if (img.complete) { const color = colorThief.getColor(img); - document.body.style.backgroundColor = 'rgb(' + color + ')'; + // document.body.style.backgroundColor = 'rgb(' + color + ')'; + // document.body.style.backgroundColor = 'rgb(' + color + ')'; + animatedBackground.style.setProperty('--color', 'rgb(' + color + ')'); + animatedBackground.classList.add('update-color'); getLuminance(color); } else { img.addEventListener('load', function() { const color = colorThief.getColor(img); - document.body.style.backgroundColor = 'rgb(' + color + ')'; + // document.body.style.backgroundColor = 'rgb(' + color + ')'; + animatedBackground.style.setProperty('--color', 'rgb(' + color + ')'); + animatedBackground.classList.add('update-color'); getLuminance(color); }); }