What i'm trying to realise with this python script is to stop spotify from playing when it's prayer time , but it never seems to work although i read tens of article of dealing with spotify for developpers and conversations with chatgpt, this is what i ended up with:
import requests
import datetime
import geocoder
import time
import spotipy
from spotipy.oauth2 import SpotifyOAuth
# Set your Spotify app credentials (client ID and client secret)
client_id = 'YOUR_CLIENT_ID'
client_secret = 'YOUR_CLIENT_SECRET'
redirect_uri = 'YOUR_REDIRECT_URI'
def get_prayer_times(latitude, longitude):
today = datetime.date.today()
formatted_date = today.strftime('%Y-%m-%d')
# Get the prayer times for today
url = f"https://api.aladhan.com/v1/timings/{formatted_date}?latitude={latitude}&longitude={longitude}&method=2"
response = requests.get(url)
data = response.json()
prayer_times = data['data']['timings']
keys_to_remove = ['Sunrise', 'Sunset', 'Midnight', 'Imsak', 'Firstthird', 'Lastthird']
# Remove unnecessary keys from the prayer times dictionary
for key in keys_to_remove:
prayer_times.pop(key, None)
return prayer_times
def get_ip_address():
response = requests.get('https://api.ipify.org?format=json')
ip_data = response.json()
ip_address = ip_data['ip']
return ip_address
def get_latitude_longitude(ip_address):
g = geocoder.ip(ip_address)
if g.ok:
latitude, longitude = g.latlng
return latitude, longitude
else:
return None
def pause_spotify():
devices = sp.devices()
for device in devices['devices']:
if device['is_active']:
sp.pause_playback(device['id'])
def resume_spotify():
devices = sp.devices()
for device in devices['devices']:
if device['is_active']:
sp.start_playback(device['id'])
def main():
ip_address = get_ip_address()
location = get_latitude_longitude(ip_address)
if location:
latitude, longitude = location
prayer_times = get_prayer_times(latitude, longitude)
print("Prayer Times:")
print(prayer_times)
# Get current time
current_time = datetime.datetime.now().strftime('%H:%M')
for prayer, time_value in prayer_times.items():
if current_time >= time_value:
# Prayer time has passed
print(f"{prayer} time has passed.")
pause_spotify()
else:
# Prayer time has not yet arrived
print(f"{prayer} time is yet to come.")
resume_spotify()
break
if __name__ == '__main__':
auth_manager = SpotifyOAuth(client_id=client_id, client_secret=client_secret, redirect_uri=redirect_uri)
sp = spotipy.Spotify(auth_manager=auth_manager)
main()
now i get how to get my client_id and client_secret from my spotifyfordeveloppers dashboard but i could never understand how i cant get that redirect uri although i read in some places that u can fill it with any spotify related link, but i always get the following error i couldn't solve: sp = spotipy.Spotify(auth_manager=SpotifyClientCredentials(client_id=client_id, ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ TypeError: Spotify.init() got an unexpected keyword argument 'auth_manager'
note: i'm still a beginner with using APIs and coding in generally but i know pretty much everything about python and coding basics. Thank you.
.........................
You need to set up the Redirect URI at Spotify Developer Dash-Board
https://developer.spotify.com/dashboard
And needs to set up scopes
# scopes for Remote control playback, Get Available Devices, Pause playback
SCOPEs = ['app-remote-control', 'user-read-playback-state', 'user-modify-playback-state']
Detail information in here
This code will work
import requests
import datetime
import geocoder
import time
import spotipy
from spotipy.oauth2 import SpotifyOAuth
# Set your Spotify app credentials (client ID and client secret)
client_id = 'YOUR_CLIENT_ID'
client_secret = 'YOUR_CLIENT_SECRET'
redirect_uri = 'YOUR_REDIRECT_URI' # my demo use http://localhost:3000/callback
# scopes for Remote control playback, Get Available Devices, Pause playback
SCOPEs = ['app-remote-control', 'user-read-playback-state', 'user-modify-playback-state']
def get_prayer_times(latitude, longitude):
today = datetime.date.today()
formatted_date = today.strftime('%Y-%m-%d')
# Get the prayer times for today
url = f"https://api.aladhan.com/v1/timings/{formatted_date}?latitude={latitude}&longitude={longitude}&method=2"
response = requests.get(url)
data = response.json()
prayer_times = data['data']['timings']
keys_to_remove = ['Sunrise', 'Sunset', 'Midnight', 'Imsak', 'Firstthird', 'Lastthird']
# Remove unnecessary keys from the prayer times dictionary
for key in keys_to_remove:
prayer_times.pop(key, None)
return prayer_times
def get_ip_address():
response = requests.get('https://api.ipify.org?format=json')
ip_data = response.json()
ip_address = ip_data['ip']
return ip_address
def get_latitude_longitude(ip_address):
g = geocoder.ip(ip_address)
if g.ok:
latitude, longitude = g.latlng
return latitude, longitude
else:
return None
def pause_spotify():
devices = sp.devices()
for device in devices['devices']:
if device['is_active']:
sp.pause_playback(device['id'])
def resume_spotify():
devices = sp.devices()
for device in devices['devices']:
if device['is_active']:
sp.start_playback(device['id'])
def main():
ip_address = get_ip_address()
location = get_latitude_longitude(ip_address)
if location:
latitude, longitude = location
prayer_times = get_prayer_times(latitude, longitude)
print("Prayer Times:")
print(prayer_times)
# Get current time
current_time = datetime.datetime.now().strftime('%H:%M')
for prayer, time_value in prayer_times.items():
if current_time >= time_value:
# Prayer time has passed
print(f"{prayer} time has passed.")
pause_spotify()
print(f"paused spotify song")
else:
# Prayer time has not yet arrived
print(f"{prayer} time is yet to come.")
resume_spotify()
print(f"resumed spotify song")
break
if __name__ == '__main__':
auth_manager = SpotifyOAuth(client_id=client_id, client_secret=client_secret, redirect_uri=redirect_uri, scope=SCOPEs)
sp = spotipy.Spotify(auth_manager=auth_manager)
main()
I captured a video (animation GIF). it shows to change from stopped to resume the song.
The left side is VS code to run the program, the right side is stopped in Spotify in Chrome. The song will resume, watch the green icon.