From f3a56a97b35da34a9ac4b12141bb618cd4db758c Mon Sep 17 00:00:00 2001 From: rintyuu Date: Sat, 17 Aug 2024 01:58:43 -0700 Subject: [PATCH] new brancho --- main-experimental.py | 99 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 main-experimental.py diff --git a/main-experimental.py b/main-experimental.py new file mode 100644 index 0000000..7033c06 --- /dev/null +++ b/main-experimental.py @@ -0,0 +1,99 @@ +import os +import requests +import spotipy +from spotipy.oauth2 import SpotifyOAuth +import time +from dotenv import load_dotenv + +# Load environment variables from .env file +load_dotenv() + +# Set up Spotipy with authentication +scope = "user-read-currently-playing user-read-playback-state" +sp = spotipy.Spotify(auth_manager=SpotifyOAuth( + client_id=os.getenv('SPOTIPY_CLIENT_ID'), + client_secret=os.getenv('SPOTIPY_CLIENT_SECRET'), + redirect_uri=os.getenv('SPOTIPY_REDIRECT_URI'), + scope=scope +)) + +# Discord webhook URL +discord_webhook_url = os.getenv('DISCORD_WEBHOOK_URL') + +# Initialize variables to track the last played track +last_track_id = None + +while True: + current_epoch = time.time() + + # Get the currently playing track information + response = sp.current_playback() + + # Check if a track is currently playing + if response is not None and response.get('is_playing', False): + track = response['item'] + track_id = track['id'] + + # Check if the track is different from the last played track + if track_id != last_track_id: + track_name = track['name'] + album_name = track['album']['name'] + artists = ', '.join([artist['name'] for artist in track['artists']]) + album_art_url = track['album']['images'][0]['url'] + track_url = track['external_urls']['spotify'] + + # Initialize context variables + context_name = None + context_url = None + + # Check if the track is from a context (playlist/album) + if response.get('context'): + context_type = response['context'].get('type') + context_uri = response['context']['uri'] + context_id = context_uri.split(':')[-1] + + if context_type == 'playlist': + context = sp.playlist(context_id) + context_name = context['name'] + context_url = context['external_urls']['spotify'] + elif context_type == 'album': + context = sp.album(context_id) + context_name = context['name'] + context_url = context['external_urls']['spotify'] + + # Format the message + description = f"**Artists**\n{artists}\n**Album**\n{album_name}\n\n\n[Listen on Spotify]({track_url})" + if context_name and context_url: + description += f" | [{context_name}]({context_url})" + description += f" | [visit the source](https://git.rintyuu.dev/rintyuu/discord-nowplaying)" + + message = { + "embeds": [ + { + "title": track_name, + "description": description, + "thumbnail": {"url": album_art_url} + } + ] + } + + print(f"Current Epoch Time: {current_epoch:.0f}") + + # Send the message to the Discord webhook + response = requests.post(discord_webhook_url, json=message) + + if response.status_code == 204: + print("Message sent to Discord successfully.") + else: + print(f"Failed to send message to Discord. Status code: {response.status_code}") + + # Update the last track ID + last_track_id = track_id + + else: + print("No track is currently playing.") + + # Wait for a certain time before checking again (e.g., 15 seconds) + print("it has been 15 seconds") + time.sleep(15) +