Title playing identification added
This commit is contained in:
parent
13efc23f2d
commit
d8096b0f31
@ -15,10 +15,10 @@ DEVICE_NAME = "Multimedia"
|
|||||||
#
|
#
|
||||||
__BASEPATH__ = os.path.abspath(os.path.dirname(__file__))
|
__BASEPATH__ = os.path.abspath(os.path.dirname(__file__))
|
||||||
APP_NAME = "spotify"
|
APP_NAME = "spotify"
|
||||||
LOGTARGET = 'logfile' # possible choices are: 'logfile' or 'stdout'
|
LOGTARGET = 'stdout' # possible choices are: 'logfile' or 'stdout'
|
||||||
LOGLVL = 'DEBUG'
|
LOGLVL = 'DEBUG'
|
||||||
|
|
||||||
LOGHOST = 'cutelog'
|
LOGHOST = 'cutelog'
|
||||||
LOGPORT = 19996
|
LOGPORT = 19996
|
||||||
|
|
||||||
formatter = report.LONG_FMT
|
formatter = report.SHORT_FMT
|
||||||
|
@ -1 +1,3 @@
|
|||||||
paho-mqtt
|
paho-mqtt
|
||||||
|
spotipy
|
||||||
|
|
||||||
|
58
spotify.py
58
spotify.py
@ -4,6 +4,10 @@ import paho.mqtt.client as paho
|
|||||||
import report
|
import report
|
||||||
import subprocess
|
import subprocess
|
||||||
import time
|
import time
|
||||||
|
import spotipy
|
||||||
|
from spotipy.oauth2 import SpotifyClientCredentials
|
||||||
|
import config
|
||||||
|
import json
|
||||||
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@ -17,15 +21,19 @@ class librespot(object):
|
|||||||
ON_CMD = ['play', ]
|
ON_CMD = ['play', ]
|
||||||
OFF_CMD = ['pause', 'stop', ]
|
OFF_CMD = ['pause', 'stop', ]
|
||||||
|
|
||||||
def __init__(self, state_callback):
|
def __init__(self, state_callback, title_callback):
|
||||||
logger.info("Starting Librespot...")
|
logger.info("Starting Librespot...")
|
||||||
self.__state_callback__ = state_callback
|
self.__state_callback__ = state_callback
|
||||||
|
self.__title_callback__ = title_callback
|
||||||
self.__process__ = subprocess.Popen(["librespot", "-v", "--name", config.DEVICE_NAME],
|
self.__process__ = subprocess.Popen(["librespot", "-v", "--name", config.DEVICE_NAME],
|
||||||
shell=False,
|
shell=False,
|
||||||
# We pipe the output to an internal pipe
|
# We pipe the output to an internal pipe
|
||||||
stderr=subprocess.PIPE)
|
stderr=subprocess.PIPE)
|
||||||
self.__state__ = None
|
self.__state__ = None
|
||||||
|
self.__title__ = None
|
||||||
|
self.__spot_id__ = None
|
||||||
self.set_state(False)
|
self.set_state(False)
|
||||||
|
self.set_title("")
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
while True:
|
while True:
|
||||||
@ -42,6 +50,14 @@ class librespot(object):
|
|||||||
out_txt = output.decode('utf-8').strip('\n').strip()
|
out_txt = output.decode('utf-8').strip('\n').strip()
|
||||||
out_txt = out_txt[out_txt.find(']') + 2:]
|
out_txt = out_txt[out_txt.find(']') + 2:]
|
||||||
logger.debug("librespot output: %s", out_txt)
|
logger.debug("librespot output: %s", out_txt)
|
||||||
|
# TODO: Parse for "librespot output: Loading <Here Ever After> with Spotify URI <spotify:track:0zckHMfaB6vT5o23ZVBLHJ>"
|
||||||
|
if out_txt.lower().startswith("loading"):
|
||||||
|
self.__spot_id__ = out_txt.split("<")[2][:-1]
|
||||||
|
logger.info("Track-ID %s identified", self.__spot_id__)
|
||||||
|
if self.__state__:
|
||||||
|
self.set_title(self.get_title_by_id())
|
||||||
|
else:
|
||||||
|
self.set_title("")
|
||||||
if "command=" in out_txt:
|
if "command=" in out_txt:
|
||||||
command = out_txt[out_txt.find('command=') + 8:].strip().lower()
|
command = out_txt[out_txt.find('command=') + 8:].strip().lower()
|
||||||
logger.debug("librespot command: %s", command)
|
logger.debug("librespot command: %s", command)
|
||||||
@ -52,6 +68,21 @@ class librespot(object):
|
|||||||
self.set_state(True)
|
self.set_state(True)
|
||||||
if command in self.OFF_CMD:
|
if command in self.OFF_CMD:
|
||||||
self.set_state(False)
|
self.set_state(False)
|
||||||
|
if self.__state__:
|
||||||
|
self.set_title(self.get_title_by_id())
|
||||||
|
else:
|
||||||
|
self.set_title("")
|
||||||
|
|
||||||
|
def get_title_by_id(self):
|
||||||
|
if self.__spot_id__ is None:
|
||||||
|
return ""
|
||||||
|
else:
|
||||||
|
sp = spotipy.Spotify(auth_manager=SpotifyClientCredentials(client_id=config.SPOTIFY_CLIENT_ID,client_secret=config.SPOTIFY_CLIENT_SECRET))
|
||||||
|
try:
|
||||||
|
track = sp.track(self.__spot_id__)
|
||||||
|
except Exception:
|
||||||
|
return None
|
||||||
|
return track["artists"][0]["name"] + " - " + track["name"]
|
||||||
|
|
||||||
def set_state(self, target_state):
|
def set_state(self, target_state):
|
||||||
if target_state != self.__state__:
|
if target_state != self.__state__:
|
||||||
@ -59,18 +90,35 @@ class librespot(object):
|
|||||||
logger.info("spotify state changed to %s", self.__state__)
|
logger.info("spotify state changed to %s", self.__state__)
|
||||||
self.__state_callback__(self.__state__)
|
self.__state_callback__(self.__state__)
|
||||||
|
|
||||||
|
def set_title(self, title):
|
||||||
|
if title != self.__title__:
|
||||||
|
self.__title__ = title
|
||||||
|
logger.info("spotify title changed to \"%s\"", self.__title__)
|
||||||
|
self.__title_callback__(self.__title__)
|
||||||
|
|
||||||
def send_msg_mqtt(state):
|
|
||||||
|
def send_state_msg_mqtt(state):
|
||||||
client= paho.Client("spotify")
|
client= paho.Client("spotify")
|
||||||
client.username_pw_set(config.MQTT_USER, config.MQTT_PASS)
|
client.username_pw_set(config.MQTT_USER, config.MQTT_PASS)
|
||||||
client.connect(config.MQTT_SERVER, 1883)
|
client.connect(config.MQTT_SERVER, 1883)
|
||||||
logger.info("Sending Spotify status information to mqtt %s = %s", config.MQTT_TOPIC, str(state))
|
topic = config.MQTT_TOPIC + "/state"
|
||||||
client.publish(config.MQTT_TOPIC, "true" if state else "false")
|
logger.info("Sending Spotify status information to mqtt %s = %s", topic, str(state))
|
||||||
|
client.publish(topic, "true" if state else "false")
|
||||||
|
|
||||||
|
def send_title_msg_mqtt(title):
|
||||||
|
client= paho.Client("spotify")
|
||||||
|
client.username_pw_set(config.MQTT_USER, config.MQTT_PASS)
|
||||||
|
client.connect(config.MQTT_SERVER, 1883)
|
||||||
|
topic = config.MQTT_TOPIC + "/title"
|
||||||
|
logger.info("Sending Spotify status information to mqtt %s = %s", topic, title)
|
||||||
|
client.publish(topic, title)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
report.appLoggingConfigure(config.__BASEPATH__, config.LOGTARGET, ((config.APP_NAME, config.LOGLVL), ), fmt=config.formatter, host=config.LOGHOST, port=config.LOGPORT)
|
report.appLoggingConfigure(config.__BASEPATH__, config.LOGTARGET, ((config.APP_NAME, config.LOGLVL), ), fmt=config.formatter, host=config.LOGHOST, port=config.LOGPORT)
|
||||||
|
|
||||||
ls = librespot(send_msg_mqtt)
|
ls = librespot(send_state_msg_mqtt, send_title_msg_mqtt)
|
||||||
ls.run()
|
ls.run()
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user