removed spotify

Signed-off-by: Ebbe Baß <ebbe@ping-mee.de>
master
Ebbe Baß 2022-07-27 15:49:29 +02:00
parent b2825bed3d
commit 3d0c6561a8
1 changed files with 40 additions and 99 deletions

139
main.py
View File

@ -2,22 +2,10 @@ from base64 import b16decode
import time import time
from datetime import datetime from datetime import datetime
from random import * from random import *
from ctypes.wintypes import VARIANT_BOOL
from multiprocessing.spawn import old_main_modules
import os
from unicodedata import decimal
import requests
current_path = os.path.dirname(os.path.abspath(__file__))
try: try:
from rpi_ws281x import * from rpi_ws281x import *
import RPi.GPIO as GPIO import RPi.GPIO as GPIO
import paho.mqtt.client as mqtt import paho.mqtt.client as mqtt
import spotipy
import urllib.request
import colorgram
import numpy as np
from PIL import Image
from dotenv import load_dotenv
except ImportError: except ImportError:
print('Some modules are missing. Try to install them with "pip3 install -r requirements.txt"') print('Some modules are missing. Try to install them with "pip3 install -r requirements.txt"')
exit() exit()
@ -44,29 +32,6 @@ def debug_print(message: str):
current_time = now.strftime("%H:%M:%S") current_time = now.strftime("%H:%M:%S")
print('[DEBUG]['+current_time+'] '+message) print('[DEBUG]['+current_time+'] '+message)
def convertColor(hexCode):
R = int(hexCode[:2],16)
G = int(hexCode[2:4],16)
B = int(hexCode[4:6],16)
total = R + G + B
if R == 0:
firstPos = 0
else:
firstPos = R / total
if G == 0:
secondPos = 0
else:
secondPos = G / total
return [firstPos, secondPos]
def getRandomHex():
return "%06x" % random.randint(0, 0xFFFFFF)
def extract_colors(url, n=4):
urllib.request.urlretrieve(url, current_path+r"/cover.jpg")
img = current_path+r"/cover.jpg"
return colorgram.extract(img, n)
def wheel(pos): def wheel(pos):
if pos < 85: if pos < 85:
return Color(pos * 3, 255 - pos * 3, 0) return Color(pos * 3, 255 - pos * 3, 0)
@ -155,73 +120,49 @@ if __name__ == '__main__':
mode = 3 mode = 3
elif payload == "Random Color per LED": elif payload == "Random Color per LED":
mode = 4 mode = 4
if os.path.isfile(current_path+r'/.env') == False:
# If not create it and add content
with open(current_path+r'/.env', 'w') as f:
var_lines = ["SPOTIPY_CLIENT_ID = ", "SPOTIPY_CLIENT_SECRET = ", "SPOTIPY_USER_NAME = ", "SPOTIPY_REDIRECT_URI = "]
for var_line in var_lines:
f.write(var_line+'\n')
print('Please fill out the variables in the .env file ( '+current_path+r'/.env )')
exit()
else:
load_dotenv()
sp_scopes = 'user-read-currently-playing user-read-playback-state streaming user-read-email user-read-private user-read-currently-playing user-read-playback-position user-read-playback-state' strip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS, LED_CHANNEL)
global sp strip.begin()
sp = spotipy.Spotify(auth_manager=SpotifyOAuth(scope=sp_scopes))
one_device_is_active = False
for x in sp.devices()['devices']:
if x['is_active'] == True:
try:
sp.start_playback(x['id'])
except:
pass
one_device_is_active = True
break
strip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS, LED_CHANNEL) mode = 0
strip.begin() power = "True"
global r
r = 255
global g
g = 255
global b
b = 255
mode = 0 mqttBroker ="homeassistant.ping-mee.local"
power = "True" client = mqtt.Client("server_rack_led")
global r client.username_pw_set("mqtt", "pmMQTT_11!")
r = 255 debug_print("Connecting to MQTT Broker "+str(mqttBroker))
global g client.connect(mqttBroker)
g = 255 client.on_connect = mqtt_on_connect
global b client.on_message = mqtt_on_message
b = 255
mqttBroker ="homeassistant.ping-mee.local" client.publish("server-rack-led/power", "True")
client = mqtt.Client("server_rack_led") client.publish("server-rack-led/mode", "Rainbow")
client.username_pw_set("mqtt", "pmMQTT_11!")
debug_print("Connecting to MQTT Broker "+str(mqttBroker))
client.connect(mqttBroker)
client.on_connect = mqtt_on_connect
client.on_message = mqtt_on_message
client.publish("server-rack-led/power", "True") try:
client.publish("server-rack-led/mode", "Rainbow") client.loop_start()
while True:
if power == "True":
if mode == 0:
rainbowCycle(strip)
elif mode == 1:
setColor(strip, Color(int(r), int(g), int(b)))
elif mode == 2:
strobe(strip, Color(int(r), int(g), int(b)))
elif mode == 3:
randomColor(strip)
elif mode == 4:
randomColorPerLED(strip)
else:
setColor(strip, Color(0,0,0))
try: except KeyboardInterrupt:
client.loop_start() client.publish("server-rack-led/power", "False")
while True: client.loop_stop()
if power == "True": setColor(strip, Color(0,0,0))
if mode == 0: GPIO.cleanup()
rainbowCycle(strip)
elif mode == 1:
setColor(strip, Color(int(r), int(g), int(b)))
elif mode == 2:
strobe(strip, Color(int(r), int(g), int(b)))
elif mode == 3:
randomColor(strip)
elif mode == 4:
randomColorPerLED(strip)
else:
setColor(strip, Color(0,0,0))
except KeyboardInterrupt:
client.publish("server-rack-led/power", "False")
client.loop_stop()
setColor(strip, Color(0,0,0))
GPIO.cleanup()