spotipy-dev / spotipy

A light weight Python library for the Spotify Web API
http://spotipy.readthedocs.org
MIT License
5.03k stars 957 forks source link

Help Request from Newbie error running #1030

Closed tarrant33 closed 5 months ago

tarrant33 commented 1 year ago

I try running some code for a test but keep getting below errors. Im new to python and using raspberry pi

pi@raspberry:~/SpotifyTest $ python3 test1.py 
/usr/bin/xdg-open: 811: : Permission denied
^X^CTraceback (most recent call last):
  File "/home/pi/SpotifyTest/test1.py", line 34, in <module>
    current_track = sp.current_playback()
  File "/home/pi/.local/lib/python3.9/site-packages/spotipy/client.py", line 1757, in current_playback
    return self._get("me/player", market=market, additional_types=additional_types)
  File "/home/pi/.local/lib/python3.9/site-packages/spotipy/client.py", line 323, in _get
    return self._internal_call("GET", url, payload, kwargs)
  File "/home/pi/.local/lib/python3.9/site-packages/spotipy/client.py", line 247, in _internal_call
    headers = self._auth_headers()
  File "/home/pi/.local/lib/python3.9/site-packages/spotipy/client.py", line 238, in _auth_headers
    token = self.auth_manager.get_access_token(as_dict=False)
  File "/home/pi/.local/lib/python3.9/site-packages/spotipy/oauth2.py", line 535, in get_access_token
    "code": code or self.get_auth_response(),
  File "/home/pi/.local/lib/python3.9/site-packages/spotipy/oauth2.py", line 490, in get_auth_response
    return self._get_auth_response_local_server(redirect_port)
  File "/home/pi/.local/lib/python3.9/site-packages/spotipy/oauth2.py", line 459, in _get_auth_response_local_server
    server.handle_request()
  File "/usr/lib/python3.9/socketserver.py", line 294, in handle_request
    ready = selector.select(timeout)
  File "/usr/lib/python3.9/selectors.py", line 416, in select
    fd_event_list = self._selector.poll(timeout)
KeyboardInterrupt

any help or insight would be greatly appreciated

dieser-niko commented 1 year ago

Can you post a code snippet? And just so you know, you can use a codeblock in comments like this:

```python3
<paste in your code here>
.```

Just remove the dot at the end, that's just so I can post this with correct formatting

tarrant33 commented 1 year ago
import os
import time
from spotipy.oauth2 import SpotifyOAuth
from PIL import Image, ImageDraw, ImageFont
import spotipy
from rgbmatrix import RGBMatrix, RGBMatrixOptions

SPOTIPY_CLIENT_ID = 'xxx'
SPOTIPY_CLIENT_SECRET = 'xxx'
SPOTIPY_REDIRECT_URI = 'http://localhost:8080/callback'

# Initialize Spotipy with your credentials
sp = spotipy.Spotify(auth_manager=SpotifyOAuth(client_id=os.environ['SPOTIPY_CLIENT_ID'],
                                              client_secret=os.environ['SPOTIPY_CLIENT_SECRET'],
                                              redirect_uri=os.environ['SPOTIPY_REDIRECT_URI'],
                                              scope="user-library-read user-read-currently-playing"))

# Initialize RGB matrix display with your provided options
options = RGBMatrixOptions()
options.rows = 64  # Number of rows (64 pixels)
options.cols = 64  # Number of columns (64 pixels)
options.chain_length = 2  # 2 chains
matrix = RGBMatrix(options=options)

# Set up custom font
font_directory = "fonts"
font_filename = "04B_03__.TTF"
font_path = os.path.join(font_directory, font_filename)
font = ImageFont.truetype(font_path, size=8)

while True:
    # Get currently playing track information
    current_track = sp.current_playback()
    if current_track is not None and 'item' in current_track:
        track_name = current_track['item']['name']
        album_name = current_track['item']['album']['name']
        artist_name = current_track['item']['artists'][0]['name']
        album_image_url = current_track['item']['album']['images'][0]['url']

        # Load and resize the album image
        album_image = Image.open(album_image_url)
        album_image = album_image.resize((40, 40))

        # Create a blank image for displaying information
        display = Image.new('RGB', (64, 64), color=(0, 0, 0))
        draw = ImageDraw.Draw(display)

        # Paste the resized album image on the left side
        display.paste(album_image, (0, 0))

        # Draw track name, album name, and artist name using custom font
        draw.text((45, 0), track_name, fill=(255, 255, 255), font=font)
        draw.text((45, 15), album_name, fill=(255, 255, 255), font=font)
        draw.text((45, 30), artist_name, fill=(255, 255, 255), font=font)

        # Display the information on the RGB matrix
        matrix.Clear()
        matrix.SetImage(display, 0, 0)
        time.sleep(10)  # Display the information for 10 seconds

    time.sleep(5)  # Poll for currently playing track every 5 seconds
dieser-niko commented 1 year ago

Hm, can't really figure out where the "Permission denied" comes from. The other error seems to be caused by you trying to stop the script. But it would probably work with sudo.

If you want, try to debug your script and go through line by line until you get the error again.

dieser-niko commented 6 months ago

Any updates?

dieser-niko commented 5 months ago

Closing as there is no activity or reply from the author.