diff options
| -rw-r--r-- | .gitignore | 4 | ||||
| -rw-r--r-- | .idea/encodings.xml | 4 | ||||
| -rw-r--r-- | .idea/inspectionProfiles/profiles_settings.xml | 6 | ||||
| -rw-r--r-- | .idea/misc.xml | 7 | ||||
| -rw-r--r-- | .idea/modules.xml | 8 | ||||
| -rw-r--r-- | .idea/spotify-discord-widget.iml | 10 | ||||
| -rw-r--r-- | bot.py | 29 |
7 files changed, 62 insertions, 6 deletions
@@ -1,2 +1,4 @@ .env -.venv
\ No newline at end of file +.venv +tokens.db +.cache
\ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..df87cf9 --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,4 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project version="4"> + <component name="Encoding" addBOMForNewFiles="with BOM under Windows, with no BOM otherwise" /> +</project>
\ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ +<component name="InspectionProjectProfileManager"> + <settings> + <option name="USE_PROJECT_PROFILE" value="false" /> + <version value="1.0" /> + </settings> +</component>
\ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..a174615 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,7 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project version="4"> + <component name="Black"> + <option name="sdkName" value="uv (spotify-discord-widget)" /> + </component> + <component name="ProjectRootManager" version="2" project-jdk-name="uv (spotify-discord-widget)" project-jdk-type="Python SDK" /> +</project>
\ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..489486d --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project version="4"> + <component name="ProjectModuleManager"> + <modules> + <module fileurl="file://$PROJECT_DIR$/.idea/spotify-discord-widget.iml" filepath="$PROJECT_DIR$/.idea/spotify-discord-widget.iml" /> + </modules> + </component> +</project>
\ No newline at end of file diff --git a/.idea/spotify-discord-widget.iml b/.idea/spotify-discord-widget.iml new file mode 100644 index 0000000..ee11dbc --- /dev/null +++ b/.idea/spotify-discord-widget.iml @@ -0,0 +1,10 @@ +<?xml version="1.0" encoding="UTF-8"?> +<module type="PYTHON_MODULE" version="4"> + <component name="NewModuleRootManager"> + <content url="file://$MODULE_DIR$"> + <excludeFolder url="file://$MODULE_DIR$/.venv" /> + </content> + <orderEntry type="jdk" jdkName="uv (spotify-discord-widget)" jdkType="Python SDK" /> + <orderEntry type="sourceFolder" forTests="false" /> + </component> +</module>
\ No newline at end of file @@ -183,7 +183,27 @@ class SpotifyAPI: "album_art": None, "url": None, } + @staticmethod + def get_last_liked_track(sp: spotipy.Spotify) -> dict | None: + res = sp.current_user_saved_tracks(limit=1) + items = res.get("items", []) + + if not items: + return None + track = items[0].get("track") + if not track: + return None + + return { + "title": track["name"], + "artist": track["artists"][0]["name"], + # Renamed key to 'album_art' to match payload builder expectations + "album_art": track["album"]["images"][0]["url"] + if track["album"].get("images") + else None, + "url": track["external_urls"]["spotify"], + } # ============================================================================== # 4. DISCORD PAYLOAD BUILDER & DISCORD IDENTITY API UPDATER @@ -257,15 +277,14 @@ def build_discord_profile_payload( "type": 3, "name": "current_song_picture", "value": { - "url": current_track.get("album_art") or DEFAULT_IMAGE - }, + "url": (current_track.get("album_art") if current_track else None) or DEFAULT_IMAGE }, }, { "type": 1, "name": "current_song_name", "value": current_track.get("title", "None"), }, - {"type": 1, "name": "status", "value": status_label}, + {"type": 1, "name": "status", "value": "recently liked"}, {"type": 1, "name": "range", "value": time_range_label}, ] } @@ -303,7 +322,7 @@ async def process_user_widget_update(discord_user_id: str) -> bool: top_song = SpotifyAPI.get_top_track(sp, time_range="short_term") top_album = SpotifyAPI.get_top_album(sp, time_range="short_term") top_artist = SpotifyAPI.get_top_artist(sp, time_range="short_term") - current_track = SpotifyAPI.get_currently_playing(sp) + last_liked = SpotifyAPI.get_last_liked_track(sp) payload = build_discord_profile_payload( spotify_username=spotify_username, @@ -311,7 +330,7 @@ async def process_user_widget_update(discord_user_id: str) -> bool: top_song=top_song, top_album=top_album, top_artist=top_artist, - current_track=current_track, + current_track=last_liked, time_range_label="4 weeks", ) |
