Update main.py
i literally just copy and pasted from experimental branch
This commit is contained in:
parent
f97e45c91a
commit
5cedab3495
1 changed files with 83 additions and 51 deletions
134
main.py
134
main.py
|
@ -5,7 +5,8 @@ from spotipy.oauth2 import SpotifyOAuth
|
|||
import time
|
||||
from dotenv import load_dotenv
|
||||
|
||||
current_epoch = time.time()
|
||||
# Number of seconds to check if same song is playing
|
||||
interval = "15"
|
||||
|
||||
# Load environment variables from .env file
|
||||
load_dotenv()
|
||||
|
@ -19,64 +20,95 @@ sp = spotipy.Spotify(auth_manager=SpotifyOAuth(
|
|||
scope=scope
|
||||
))
|
||||
|
||||
# Get the currently playing track information
|
||||
response = sp.current_playback()
|
||||
|
||||
# Discord webhook URL
|
||||
discord_webhook_url = os.getenv('DISCORD_WEBHOOK_URL')
|
||||
|
||||
# Check if a track is currently playing
|
||||
if response is not None and response.get('is_playing', False):
|
||||
track = response['item']
|
||||
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 variables to track the last played track
|
||||
last_track_id = None
|
||||
|
||||
# Initialize playlist_url
|
||||
context_name = None
|
||||
context_url = None
|
||||
interval_start = 1
|
||||
interval_verif = False
|
||||
|
||||
# Check if the track is from a context (playlist)
|
||||
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']
|
||||
while True:
|
||||
current_epoch = time.time()
|
||||
|
||||
# Format the message
|
||||
description = f"**Artists**\n{artists}\n\n**Album**\n{album_name}\n\n<t:{current_epoch:.0f}:R>\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}
|
||||
# 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:
|
||||
print("previous song is still playing")
|
||||
|
||||
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\n**Album**\n{album_name}\n\n<t:{current_epoch:.0f}:R>\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}")
|
||||
# print(f"Current Epoch Time: {current_epoch:.0f}")
|
||||
|
||||
# Send the message to the Discord webhook
|
||||
response = requests.post(discord_webhook_url, json=message)
|
||||
# Send the message to the Discord webhook
|
||||
response = requests.post(discord_webhook_url, json=message)
|
||||
|
||||
if response.status_code == 204:
|
||||
print(f"Currently playing: {track_name} by {artists}")
|
||||
else:
|
||||
print(f"Failed to send message to Discord. Status code: {response.status_code}")
|
||||
|
||||
# Update the last track ID
|
||||
last_track_id = track_id
|
||||
|
||||
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}")
|
||||
else:
|
||||
print("No track is currently playing.")
|
||||
print("No track is currently playing.")
|
||||
|
||||
#if interval_start == 1 and not interval_verif:
|
||||
# print("ignore this seconds has passed message please")
|
||||
# print("im still working out the bugs in this program")
|
||||
# interval_start = 0
|
||||
# interval_verif = True
|
||||
|
||||
# Wait for a certain time before checking again (e.g., 15 seconds)
|
||||
#print("it has been", interval,"seconds")
|
||||
time.sleep(int(interval))
|
||||
|
|
Loading…
Add table
Reference in a new issue