Module spotify_state -> mqtt
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import config
  2. import logging
  3. import paho.mqtt.client as paho
  4. import report
  5. import subprocess
  6. import time
  7. import spotipy
  8. from spotipy.oauth2 import SpotifyClientCredentials
  9. import config
  10. import json
  11. try:
  12. from config import APP_NAME as ROOT_LOGGER_NAME
  13. except ImportError:
  14. ROOT_LOGGER_NAME = 'root'
  15. logger = logging.getLogger(ROOT_LOGGER_NAME).getChild('main')
  16. class librespot(object):
  17. ON_CMD = ['play', ]
  18. OFF_CMD = ['pause', 'stop', ]
  19. def __init__(self, state_callback, title_callback):
  20. logger.info("Starting Librespot...")
  21. self.__state_callback__ = state_callback
  22. self.__title_callback__ = title_callback
  23. self.__process__ = subprocess.Popen(["librespot", "-v", "--name", config.DEVICE_NAME],
  24. shell=False,
  25. # We pipe the output to an internal pipe
  26. stderr=subprocess.PIPE)
  27. self.__state__ = None
  28. self.__preload_state__ = False
  29. self.__title__ = None
  30. self.__spot_id__ = None
  31. self.__spot_id_preload__ = None
  32. self.set_state(False)
  33. self.set_title("")
  34. def run(self):
  35. while True:
  36. output = self.__process__.stderr.readline()
  37. # Polling returns None when the program is still running, return_code otherwise
  38. return_code = self.__process__.poll()
  39. if return_code is not None:
  40. self.__process__.close()
  41. # Program ended, get exit/return code
  42. raise RuntimeError("Command '{}' finished with exit code {}".format(command, return_code))
  43. # If the output is not empty, feed it to the function, strip the newline first
  44. if output:
  45. out_txt = output.decode('utf-8').strip('\n').strip()
  46. out_txt = out_txt[out_txt.find(']') + 2:]
  47. #logger.debug("librespot output: %s", out_txt)
  48. # TODO: Parse for "librespot output: Loading <Here Ever After> with Spotify URI <spotify:track:0zckHMfaB6vT5o23ZVBLHJ>"
  49. if out_txt.lower().startswith("loading"):
  50. logger.debug("librespot: %s", out_txt)
  51. if self.__preload_state__:
  52. self.__spot_id_preload__ = out_txt.split("<")[2][:-1]
  53. logger.info("Upcomming Track-ID %s identified", self.__spot_id__)
  54. else:
  55. self.__spot_id__ = out_txt.split("<")[2][:-1]
  56. logger.info("Current Track-ID %s identified", self.__spot_id__)
  57. if "command=" in out_txt:
  58. command = out_txt[out_txt.find('command=') + 8:].strip().lower()
  59. logger.debug("librespot command: %s", command)
  60. if command.startswith("preload"):
  61. self.__preload_state__ = True
  62. if command.startswith("load"):
  63. self.set_state(command.split(',')[2].strip() == 'true')
  64. #
  65. self.__preload_state__ = False
  66. self.__spot_id__ = self.__spot_id_preload__
  67. #
  68. elif command in self.ON_CMD:
  69. self.set_state(True)
  70. elif command in self.OFF_CMD:
  71. self.set_state(False)
  72. if self.__state__:
  73. self.set_title(self.get_title_by_id())
  74. else:
  75. self.set_title("")
  76. def get_title_by_id(self):
  77. if self.__spot_id__ is None:
  78. return ""
  79. else:
  80. sp = spotipy.Spotify(auth_manager=SpotifyClientCredentials(client_id=config.SPOTIFY_CLIENT_ID,client_secret=config.SPOTIFY_CLIENT_SECRET))
  81. try:
  82. track = sp.track(self.__spot_id__)
  83. except Exception:
  84. return None
  85. return track["artists"][0]["name"] + " - " + track["name"]
  86. def set_state(self, target_state):
  87. if target_state != self.__state__:
  88. self.__state__ = target_state
  89. logger.info("spotify state changed to %s", self.__state__)
  90. self.__state_callback__(self.__state__)
  91. def set_title(self, title):
  92. if title != self.__title__:
  93. self.__title__ = title
  94. logger.info("spotify title changed to \"%s\"", self.__title__)
  95. self.__title_callback__(self.__title__)
  96. def send_state_msg_mqtt(state):
  97. client= paho.Client("spotify")
  98. client.username_pw_set(config.MQTT_USER, config.MQTT_PASS)
  99. try:
  100. client.connect(config.MQTT_SERVER, 1883)
  101. topic = config.MQTT_TOPIC + "/state"
  102. logger.info("Sending Spotify status information to mqtt %s = %s", topic, str(state))
  103. client.publish(topic, "true" if state else "false")
  104. except (socket.timeout, OSError) as e:
  105. logger.warning("Erro while sending state information")
  106. def send_title_msg_mqtt(title):
  107. client= paho.Client("spotify")
  108. client.username_pw_set(config.MQTT_USER, config.MQTT_PASS)
  109. try:
  110. client.connect(config.MQTT_SERVER, 1883)
  111. topic = config.MQTT_TOPIC + "/title"
  112. logger.info("Sending Spotify status information to mqtt %s = %s", topic, title)
  113. client.publish(topic, title)
  114. except (socket.timeout, OSError) as e:
  115. logger.warning("Erro while sending title information")
  116. if __name__ == '__main__':
  117. report.appLoggingConfigure(config.__BASEPATH__, config.LOGTARGET, ((config.APP_NAME, config.LOGLVL), ), fmt=config.formatter, host=config.LOGHOST, port=config.LOGPORT)
  118. ls = librespot(send_state_msg_mqtt, send_title_msg_mqtt)
  119. ls.run()