From 1c4024be7d5cbee2a5f4f50b2bf48675cb14a9a6 Mon Sep 17 00:00:00 2001 From: Brandon4466 Date: Wed, 8 Mar 2023 20:26:45 -0800 Subject: [PATCH] Changed update so it doesn't retry to fast Refactored code --- .cache | 2 +- .vscode/launch.json | 16 ++++++++ handler.py | 6 +++ spotifycontroller.py | 92 ++++++++++++++++++++++++-------------------- todo.txt | 8 +++- update.py | 14 +++++-- 6 files changed, 90 insertions(+), 48 deletions(-) create mode 100644 .vscode/launch.json create mode 100644 handler.py diff --git a/.cache b/.cache index 2825e3c..bf36bc1 100644 --- a/.cache +++ b/.cache @@ -1 +1 @@ -{"access_token": "BQDMY3TJbe_zGpD0_Hbhitu8zVPEwuXwPDv-KkVhqZ_itVTbYbUdwoqkUl0xFTpReD9_Hgix6RRsM7HyeWpr6r02dlVG8EzbVf9CDNclFSDH56lplmN_H8wbyZrz1DGqow-uVUZJpfDYrPizY2GclbsJLNj3pxguVNPwfbkLXjiOBK9LO1M4A-eoA8YzTCFxhdL5CC-tW2lucUDEaEpd", "token_type": "Bearer", "expires_in": 3600, "scope": "user-library-modify user-library-read user-modify-playback-state user-read-playback-state", "expires_at": 1677981348, "refresh_token": "AQDMo7jMg-YY6MpV64feNjby-v67EjBDVFxHtJM4Uax4geoYYKU_bYmu8Mpz-7h6XjTurxvaoUZSIyxr1b4dnLgnOwfvCtS2ozFGXifAXl7R4wMjCY79BvGesxGogf24j58"} \ No newline at end of file +{"access_token": "BQB1g4gwPqYVjNK4nO_yo-cpklfGJr3UxL5hIveI0mJjE5SppjfNnpNbvAwC2He9r1SdAPmWfDnNER1XjgNb_RlNDZVYpXTE-D46mfoLc-f-rnR7vagnY5OL7KULnDoYQkp1iOZwDUmOAWxtBKVQSzRG9coPbh7NfB75Iz42zJYu6IneT4E-R0FOJSW3bkTKjbenqn4zMsnoPyKwQmOi", "token_type": "Bearer", "expires_in": 3600, "refresh_token": "AQC5BsmRqj_PhCL7Xj32C6Pz4UeFF09ZufzWv0NU1lGwZCUGaWAQP8F4twJf3Rx5EfKyAg4DIEnHZIFd6e5L4bPQXhPUny2t7A1AA5hCwfFI_LjXWWPij8oKX_0YDr6CXK0", "scope": "user-library-modify user-library-read user-modify-playback-state user-read-playback-state", "expires_at": 1678338237} \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..306f58e --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,16 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Python: Current File", + "type": "python", + "request": "launch", + "program": "${file}", + "console": "integratedTerminal", + "justMyCode": true + } + ] +} \ No newline at end of file diff --git a/handler.py b/handler.py new file mode 100644 index 0000000..392267d --- /dev/null +++ b/handler.py @@ -0,0 +1,6 @@ +def handler(): + try: + exec(open('spotifycontroller.py').read()) + except: + print("An error has ocurred, reestablishing the application now.") + exec(open('spotifycontroller.py').read()) \ No newline at end of file diff --git a/spotifycontroller.py b/spotifycontroller.py index 094f507..8f15162 100644 --- a/spotifycontroller.py +++ b/spotifycontroller.py @@ -19,7 +19,7 @@ import queue if os.name == 'posix': os.system("xset -display :0 s 21600") if os.path.isfile("updated.cfg"): - print("A new update was downloaded and installed. Starting in 5 seconds...") + print("NEW VER") os.remove("updated.cfg") sleep(5) @@ -28,10 +28,13 @@ q = queue.Queue() # import canvas # Set the Spotify app's client ID and client secret -client_id = "69b82a34d0fb40be80b020eae8e80f25" -client_secret = "455575b0e3db44acbbfaa0c419bc3c10" +# client_id = "69b82a34d0fb40be80b020eae8e80f25" +# client_secret = "455575b0e3db44acbbfaa0c419bc3c10" redirect_uri = "http://127.0.0.1:8888/callback" +client_id = "df61ecadf09941eb87e693d37f3ad178" +client_secret = "ba97992d614e48d6b0d023998b2957cb" + # Set the user's Spotify username username = "thebrandon45" password = "Mariposa2502$" @@ -94,7 +97,7 @@ def controlNext(): def controlPrevious(): spotify.previous_track() -def likeSong(): +def likeSong(is_bright): if spotify.current_user_saved_tracks_contains(tracks=[(spotify.current_playback()["item"]["id"])])[0] is False: spotify.current_user_saved_tracks_add(tracks=[(spotify.current_playback()["item"]["id"])]) if is_bright is True: @@ -112,6 +115,41 @@ def likeSong(): play_button.config(image=play_img) pause_button.config(image=pause_img) +def colorUI(track_id, album_art_img_open): + if spotify.current_user_saved_tracks_contains(tracks=[track_id])[0] is True: + liked_song = True + else: + liked_song = False + if math.sqrt(0.299 * (get_colors(album_art_img_open)[0] ** 2) + 0.587 * (get_colors(album_art_img_open)[1] ** 2) + 0.114 * (get_colors(album_art_img_open)[2] ** 2)) > 170: + is_bright = True + if liked_song is True: + play_button.config(image=play_heart_img_black) + pause_button.config(image=pause_heart_img_black) + else: + play_button.config(image=play_img_black) + pause_button.config(image=pause_img_black) + song_label.config(foreground="black") + artist_label.config(foreground="black") + device_name_label.config(foreground="black") + lyrics_label.config(foreground="black") + next_button.config(image=next_img_black) + previous_button.config(image=previous_img_black) + else: + is_bright = False + if liked_song is True: + play_button.config(image=play_heart_img) + pause_button.config(image=pause_heart_img) + else: + play_button.config(image=play_img) + pause_button.config(image=pause_img) + song_label.config(foreground="white") + artist_label.config(foreground="white") + device_name_label.config(foreground="white") + lyrics_label.config(foreground="white") + next_button.config(image=next_img) + previous_button.config(image=previous_img) + return is_bright + def start_playback_on_device(): # global count # global wait_time @@ -293,12 +331,13 @@ next_button.bind("", lambda e:controlNext()) previous_button.bind("", lambda e:controlPrevious()) album_art_label.bind("", lambda e:likeSong()) # devices_list.bind("", lambda e:wakeup()) -import json + + + # Function to update the song label with the current track's name def update_song_label(): global lrc global album_art_img - global is_bright # Get the current playback information current_playback = spotify.current_playback() # If there is no current playback, set the text of the song label to "No playback" @@ -357,42 +396,11 @@ def update_song_label(): previous_button.config(background=bg_color) lyrics_label_frame.config(background=bg_color) lyrics_label.config(background=bg_color) - if spotify.current_user_saved_tracks_contains(tracks=[track_id])[0] is True: - liked_song = True - else: - liked_song = False - if math.sqrt(0.299 * (get_colors(album_art_img_open)[0] ** 2) + 0.587 * (get_colors(album_art_img_open)[1] ** 2) + 0.114 * (get_colors(album_art_img_open)[2] ** 2)) > 170: - is_bright = True - if liked_song is True: - play_button.config(image=play_heart_img_black) - pause_button.config(image=pause_heart_img_black) - else: - play_button.config(image=play_img_black) - pause_button.config(image=pause_img_black) - song_label.config(foreground="black") - artist_label.config(foreground="black") - device_name_label.config(foreground="black") - lyrics_label.config(foreground="black") - next_button.config(image=next_img_black) - previous_button.config(image=previous_img_black) - else: - is_bright = False - if liked_song is True: - play_button.config(image=play_heart_img) - pause_button.config(image=pause_heart_img) - else: - play_button.config(image=play_img) - pause_button.config(image=pause_img) - song_label.config(foreground="white") - artist_label.config(foreground="white") - device_name_label.config(foreground="white") - lyrics_label.config(foreground="white") - next_button.config(image=next_img) - previous_button.config(image=previous_img) - # print(oauth.get_cached_token()["access_token"]) - # print(current_playback["item"]["id"]) - # canvas_url = canvas.get_canvas_for_track(access_token=oauth.get_cached_token()["access_token"], track_id=current_playback["item"]["id"]) - # print(canvas_url) + colorUI(track_id, album_art_img_open) + # print(oauth.get_cached_token()["access_token"]) + # print(current_playback["item"]["id"]) + # canvas_url = canvas.get_canvas_for_track(access_token=oauth.get_cached_token()["access_token"], track_id=current_playback["item"]["id"]) + # print(canvas_url) lrc = q.get() root.after(500, update_song_label) if playing_status == True: diff --git a/todo.txt b/todo.txt index 80d9f60..e19deca 100644 --- a/todo.txt +++ b/todo.txt @@ -69,4 +69,10 @@ create a single json file that has a version list with download links to each ve if newer version is in json file, follow link in json file to download, otherwise continue to start the program. 03/04/2023: -redo background color algo. PROMINENT COLOR not average like it is now. \ No newline at end of file +redo background color algo. PROMINENT COLOR not average like it is now. + +03/04/2023: +add shuffle and repeat buttons + +03/07/2023: +make it so the displays turns off automatically after some time (maybe depending on time, ie. late at night) \ No newline at end of file diff --git a/update.py b/update.py index 50a5d38..c1e17ad 100644 --- a/update.py +++ b/update.py @@ -2,24 +2,30 @@ import urllib from urllib.request import urlopen from zipfile import ZipFile from io import BytesIO -from spotipy import exceptions as SpotipyExceptions -from requests import exceptions as RequestsExceptions +from time import sleep + while True: try: urlopen('http://bbrunson.com', timeout=1) + print("Connection to server established.") break except: pass + try: + print("Checking for updates...") with ZipFile(BytesIO((urlopen('http://files.bbrunson.com/spotify-gui/update.zip')).read())) as zipObj: zipObj.extractall() + print("Update successfully installed.") except urllib.error.HTTPError: + print("No update available.") pass try: exec(open('spotifycontroller.py').read()) -except (RequestsExceptions.HTTPError, SpotipyExceptions.SpotifyException): - print("An error has ocurred, saving and reestablishing application now.") +except: + print("An error has ocurred, reestablishing the application in 10 seconds.") + sleep(10) exec(open('spotifycontroller.py').read()) \ No newline at end of file