copy and paste from experimental to main

This commit is contained in:
rintyuu 2024-09-01 10:02:03 -07:00
parent 05e64eeafa
commit 0cea53f537
3 changed files with 91 additions and 53 deletions

1
.cache Normal file
View file

@ -0,0 +1 @@
{"access_token": "BQCDcA2W14c-etxLoiYas_hnq4lN-5Rhp2TkDomMPkF-ODghreVVOCLF8GffioeKoGQOBjWDWzNToHJ2rjHrLKpi6X5cF6POrF_3d19oNqiqFxk10aoBQPHNi3f1i3pfGAHjDxieCy1iA6nYiPUKstRUwCTq8uRlHjlm9FIeIeRWgRog4bYN68eu4h9-0Wzs5wU", "token_type": "Bearer", "expires_in": 3600, "refresh_token": "AQAy-vVt9Cxy3V_bYptj-TJQzlhOak5hQCMMoGhHu-9w-o5QDjPES45hPMsZFkh1dp0hYi3TQ5HU4R9UnB8kK1wskio3z088F0K4_5OPyKvf0SdQiajXyYTRT1jM4CA3-E0", "scope": "user-read-currently-playing user-read-playback-state", "expires_at": 1725212473}

8
.env
View file

@ -1,4 +1,4 @@
SPOTIPY_CLIENT_ID='' SPOTIPY_CLIENT_ID='eb7b918dbc374538a632d54dcabc4d48'
SPOTIPY_CLIENT_SECRET='' SPOTIPY_CLIENT_SECRET='cdfb0359de3d4c75a99e039424ac6386'
SPOTIPY_REDIRECT_URI='' SPOTIPY_REDIRECT_URI='https://rintyuu.uk/callback/'
DISCORD_WEBHOOK_URL="" DISCORD_WEBHOOK_URL="https://discord.com/api/webhooks/1261062131588005989/AzwPZ1U14HQetDk5snhveLq-bUkHMNcxpXG7ZNyd4ieUiEsSL5OfOXEc31qj2QcbjBBq"

51
main.py
View file

@ -2,8 +2,12 @@ import os
import requests import requests
import spotipy import spotipy
from spotipy.oauth2 import SpotifyOAuth from spotipy.oauth2 import SpotifyOAuth
import time
from dotenv import load_dotenv from dotenv import load_dotenv
# Number of seconds to check if same song is playing
interval = "15"
# Load environment variables from .env file # Load environment variables from .env file
load_dotenv() load_dotenv()
@ -16,26 +20,43 @@ sp = spotipy.Spotify(auth_manager=SpotifyOAuth(
scope=scope scope=scope
)) ))
# Get the currently playing track information
response = sp.current_playback()
# Discord webhook URL # Discord webhook URL
discord_webhook_url = os.getenv('DISCORD_WEBHOOK_URL') discord_webhook_url = os.getenv('DISCORD_WEBHOOK_URL')
# Initialize variables to track the last played track
last_track_id = None
interval_start = 1
interval_verif = False
while True:
current_epoch = time.time()
# Get the currently playing track information
response = sp.current_playback()
# Check if a track is currently playing # Check if a track is currently playing
if response is not None and response.get('is_playing', False): if response is not None and response.get('is_playing', False):
track = response['item'] 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'] track_name = track['name']
album_name = track['album']['name'] album_name = track['album']['name']
artists = ', '.join([artist['name'] for artist in track['artists']]) artists = ', '.join([artist['name'] for artist in track['artists']])
album_art_url = track['album']['images'][0]['url'] album_art_url = track['album']['images'][0]['url']
track_url = track['external_urls']['spotify'] track_url = track['external_urls']['spotify']
# Initialize playlist_url # Initialize context variables
context_name = None context_name = None
context_url = None context_url = None
# Check if the track is from a context (playlist) # Check if the track is from a context (playlist/album)
if response.get('context'): if response.get('context'):
context_type = response['context'].get('type') context_type = response['context'].get('type')
context_uri = response['context']['uri'] context_uri = response['context']['uri']
@ -51,7 +72,7 @@ if response is not None and response.get('is_playing', False):
context_url = context['external_urls']['spotify'] context_url = context['external_urls']['spotify']
# Format the message # Format the message
description = f"**Artists**\n{artists}\n**Album**\n{album_name}\n\n[Listen on Spotify]({track_url})" 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: if context_name and context_url:
description += f" | [{context_name}]({context_url})" description += f" | [{context_name}]({context_url})"
description += f" | [visit the source](https://git.rintyuu.dev/rintyuu/discord-nowplaying)" description += f" | [visit the source](https://git.rintyuu.dev/rintyuu/discord-nowplaying)"
@ -66,12 +87,28 @@ if response is not None and response.get('is_playing', False):
] ]
} }
# print(f"Current Epoch Time: {current_epoch:.0f}")
# Send the message to the Discord webhook # Send the message to the Discord webhook
response = requests.post(discord_webhook_url, json=message) response = requests.post(discord_webhook_url, json=message)
if response.status_code == 204: if response.status_code == 204:
print("Message sent to Discord successfully.") print(f"Currently playing: {track_name} by {artists}")
else: else:
print(f"Failed to send message to Discord. Status code: {response.status_code}") print(f"Failed to send message to Discord. Status code: {response.status_code}")
# Update the last track ID
last_track_id = track_id
else: 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))