192 lines
5.7 KiB
Python
192 lines
5.7 KiB
Python
import sys
|
|
import spotipy
|
|
import spotipy.util as util
|
|
import config as cfg
|
|
from PIL import Image, ImageDraw, ImageFont
|
|
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
|
|
options.disable_hardware_pulsing = True
|
|
screen = RGBMatrix(options = options)
|
|
|
|
song_name=""
|
|
artist_name=""
|
|
|
|
cache_path="/home/pi/.cache-"+username
|
|
|
|
is_playing = False
|
|
|
|
token = None #oauth2.SpotifyOAuth(username, scope, client_id, client_secret, redirect_uri)
|
|
|
|
#token = util.prompt_for_user_token(username, scope, client_id, client_secret, redirect_uri)
|
|
|
|
font = ImageFont.load_default()
|
|
|
|
def get_track():
|
|
print('getting current song info')
|
|
sp = spotipy.Spotify(auth=token)
|
|
track = sp.currently_playing()
|
|
global is_playing
|
|
is_playing = track['is_playing']
|
|
global song_name
|
|
song_name = track['item']['name']
|
|
global artist_name
|
|
artist_name = track['item']['artists'][0]['name']
|
|
for i in range(len(track['item']['artists'])-1):
|
|
artist_name +=', ' + track['item']['artists'][i+1]['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 = 'black')
|
|
cover_art = get_track()
|
|
if(is_playing):
|
|
canvas.paste(cover_art,(0,96))
|
|
songinfo = ImageDraw.Draw(canvas)
|
|
songinfo_offset_x = 33
|
|
songinfo_offset_y = 101
|
|
songinfo.text((songinfo_offset_x,songinfo_offset_y+11), song_name)
|
|
songinfo.text((songinfo_offset_x,songinfo_offset_y), artist_name)
|
|
|
|
#scrolling_text(song_name,songinfo_offset_x, songinfo_offset_y+10)
|
|
if(font.getsize(song_name)[0]>160):
|
|
x = threading.Thread(target=scrolling_text,args=(song_name,songinfo_offset_x,songinfo_offset_y+11))
|
|
x.start()
|
|
print('scrolling text started')
|
|
if(font.getsize(artist_name)[0]>160):
|
|
|
|
y = threading.Thread(target=scrolling_text,args=(artist_name,songinfo_offset_x,songinfo_offset_y))
|
|
y.start()
|
|
|
|
showImage(canvas)
|
|
|
|
|
|
def scrolling_text(text,offset_x=0,offset_y=0):
|
|
scroll_frame_time = (8/2)/(font.getsize(text)[0]-160)
|
|
#print(scroll_frame_time)
|
|
if(scroll_frame_time>0.1):
|
|
scroll_frame_time = 0.1
|
|
for i in range(font.getsize(text)[0]-160):
|
|
scrolled_text_canvas = Image.new('RGB', (160, 11), color = 'black')
|
|
scrolled_text = ImageDraw.Draw(scrolled_text_canvas)
|
|
scrolled_text.text((-i,0), text)
|
|
showImage(scrolled_text_canvas, offset_x, offset_y)
|
|
time.sleep(scroll_frame_time)
|
|
#print('scrolled '+str(i)+' px')
|
|
if((5-(font.getsize(text)[0]-160)*2*scroll_frame_time)>0):
|
|
time.sleep(5-(font.getsize(text)[0]-160)*2*scroll_frame_time)
|
|
for i in range(font.getsize(text)[0]-160):
|
|
scrolled_text_canvas = Image.new('RGB', (160, 11), color = 'black')
|
|
scrolled_text = ImageDraw.Draw(scrolled_text_canvas)
|
|
scrolled_text.text(((-(font.getsize(text)[0]-160))+i+1,0), text)
|
|
showImage(scrolled_text_canvas, offset_x, offset_y)
|
|
time.sleep(scroll_frame_time)
|
|
#print('scrolled '+str(i)+' px')
|
|
|
|
|
|
def main():
|
|
get_access_token()
|
|
|
|
def get_access_token():
|
|
global token
|
|
sp_oauth = spotipy.SpotifyOAuth(
|
|
client_id,
|
|
client_secret,
|
|
redirect_uri,
|
|
scope=scope,
|
|
cache_path=cache_path,
|
|
username=username,
|
|
open_browser=False
|
|
)
|
|
token_info = sp_oauth.get_cached_token()
|
|
|
|
if not token_info:
|
|
code = sp_oauth.get_auth_response()
|
|
token = sp_oauth.get_access_token(code, as_dict=False)
|
|
else:
|
|
token = token_info["access_token"]
|
|
|
|
def is_token_expired():
|
|
sp_oauth = spotipy.SpotifyOAuth(
|
|
client_id,
|
|
client_secret,
|
|
redirect_uri,
|
|
scope=scope,
|
|
cache_path=cache_path,
|
|
username=username,
|
|
open_browser=False
|
|
)
|
|
token_info = sp_oauth.get_cached_token()
|
|
if not token_info:
|
|
return True
|
|
else:
|
|
return sp_oauth.is_token_expired(token_info)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|
|
print('led-matrix-viewer started')
|
|
|
|
while True:
|
|
drawCanvas()
|
|
time.sleep(10)
|
|
if(is_token_expired()):
|
|
get_access_token()
|
|
|
|
|
|
|
|
|
|
|