Files
spotvinyl/main.py
2025-09-16 11:44:32 +02:00

53 lines
1.8 KiB
Python

import spotipy
from spotipy.oauth2 import SpotifyOAuth
import sys
# Replace these with your Spotify developer app credentials
CLIENT_ID = 'a1b29f64bef643b5ade0944830637510'
CLIENT_SECRET = '1d74196e6fec41f9986917afd57df3da'
REDIRECT_URI = 'https://vinyly.couraud.xyz'
SCOPE = 'user-modify-playback-state user-read-playback-state'
def main():
auth_manager = SpotifyOAuth(client_id=CLIENT_ID,
client_secret=CLIENT_SECRET,
redirect_uri=REDIRECT_URI,
scope=SCOPE,
open_browser=False)
# Check if token already cached; if not, do manual auth
token_info = auth_manager.get_cached_token()
if not token_info:
print("Please scan the QR code and paste the full redirect URL after authorization:")
redirect_response = input("Redirect URL: ").strip()
token_info = auth_manager.get_access_token(code=auth_manager.parse_response_code(redirect_response))
auth_manager.cache_token(token_info)
sp = spotipy.Spotify(auth_manager=auth_manager)
if len(sys.argv) < 2:
print('Usage: python play_spotify.py "song name"')
sys.exit(1)
song_name = ' '.join(sys.argv[1:])
results = sp.search(q=song_name, type='track', limit=1)
if not results['tracks']['items']:
print(f'No track found for: {song_name}')
sys.exit(1)
track_uri = results['tracks']['items'][0]['uri']
devices = sp.devices()
if not devices['devices']:
print('No active Spotify devices found. Please start playback on a device.')
sys.exit(1)
active_device_id = devices['devices'][0]['id']
sp.start_playback(device_id=active_device_id, uris=[track_uri])
print(f'Playing "{song_name}" on device ID: {active_device_id}')
if __name__ == '__main__':
main()