copy and paste from experimental to main
This commit is contained in:
parent
05e64eeafa
commit
0cea53f537
3 changed files with 91 additions and 53 deletions
1
.cache
Normal file
1
.cache
Normal 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
8
.env
|
@ -1,4 +1,4 @@
|
|||
SPOTIPY_CLIENT_ID=''
|
||||
SPOTIPY_CLIENT_SECRET=''
|
||||
SPOTIPY_REDIRECT_URI=''
|
||||
DISCORD_WEBHOOK_URL=""
|
||||
SPOTIPY_CLIENT_ID='eb7b918dbc374538a632d54dcabc4d48'
|
||||
SPOTIPY_CLIENT_SECRET='cdfb0359de3d4c75a99e039424ac6386'
|
||||
SPOTIPY_REDIRECT_URI='https://rintyuu.uk/callback/'
|
||||
DISCORD_WEBHOOK_URL="https://discord.com/api/webhooks/1261062131588005989/AzwPZ1U14HQetDk5snhveLq-bUkHMNcxpXG7ZNyd4ieUiEsSL5OfOXEc31qj2QcbjBBq"
|
||||
|
|
135
main.py
135
main.py
|
@ -2,8 +2,12 @@ import os
|
|||
import requests
|
||||
import spotipy
|
||||
from spotipy.oauth2 import SpotifyOAuth
|
||||
import time
|
||||
from dotenv import load_dotenv
|
||||
|
||||
# Number of seconds to check if same song is playing
|
||||
interval = "15"
|
||||
|
||||
# Load environment variables from .env file
|
||||
load_dotenv()
|
||||
|
||||
|
@ -16,62 +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**Album**\n{album_name}\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}
|
||||
# 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}
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
# Send the message to the Discord webhook
|
||||
response = requests.post(discord_webhook_url, json=message)
|
||||
# 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(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