Module spotify_state -> mqtt
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

spotify.py 4.7KB

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