led-matrix-player/main.py

102 lines
2.5 KiB
Python

import sys
import spotipy
import spotipy.util as util
import config as cfg
from PIL import Image
import asyncio
from rgbmatrix import RGBMatrix, RGBMatrixOptions
import threading
import requests
from io import BytesIO
import time
client_id = cfg.client_id
client_secret = cfg.client_secret
redirect_uri = cfg.redirect_uri
username = cfg.username
scope = 'user-read-currently-playing'
options = RGBMatrixOptions()
options.rows = 32
options.cols = 64
options.chain_length = 6
options.parallel = 2
options.hardware_mapping = 'regular'
options.pixel_mapper_config = 'U-mapper'
options.limit_refresh_rate_hz = 240
options.gpio_slowdown = 4
options.brightness = 50
screen = RGBMatrix(options = options)
song_name=""
artist_name=""
token = util.prompt_for_user_token(username, scope, client_id, client_secret, redirect_uri)
def get_track():
print('getting info')
sp = spotipy.Spotify(auth=token)
track = sp.currently_playing()
print(track['is_playing'])
global song_name
song_name = track['item']['name']
global artist_name
artist_name = track['item']['artists'][0]['name']
print(track['item']['name'] + ' - ' + track['item']['artists'][0]['name'])
print('getting image')
image_url = track['item']['album']['images'][0]['url']
response = requests.get(image_url)
image = Image.open(BytesIO(response.content))
print(image_url)
image = image.resize((32,32))
return image
def showImage(image, offset_x = 0, offset_y = 0):
image = image.convert('RGB')
width, height = image.size
for x in range(width):
for y in range(height):
r, g, b = image.getpixel((x, y))
screen.SetPixel(x+offset_x,y+offset_y,r,g,b)
#if token:
# sp = spotipy.Spotify(auth=token)
# track = sp.currently_playing()
# print(track['is_playing'])
# print(track['item']['name'] + ' - ' + track['item']['artists'][0]['name'])
# imageString = requests.get(track['item']['album']['images'][0]['url'])
# image = Image.open(BytesIO(imageString.content))
# image = image.resize((32,32))
# #image.save("thumbnail.jpg","JPEG")
#else:
# print("Can't get token for", username)
#def main():
# x = threading.Thread(target=draw)
# x.start()
def drawCanvas():
canvas = Image.new('RGB', (192, 128), color = 'red')
canvas.paste(get_track(),(0,0))
songinfo = ImageDraw.Draw(canvas)
songinfo.text((10,10), song_name)
showImage(canvas)
if __name__ == "__main__":
#main()
print('text')
while True:
drawCanvas()
time.sleep(10)