Module spotify_state -> mqtt
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

spotify.py 5.6KB

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