Browse Source

Usage of mqtt app_name for client instance + publishing title information

master
Dirk Alders 2 years ago
parent
commit
89b113959d
1 changed files with 20 additions and 29 deletions
  1. 20
    29
      spotify.py

+ 20
- 29
spotify.py View File

@@ -29,18 +29,19 @@ class librespot(object):
29 29
         self.__process__ = subprocess.Popen(["librespot", "-v", "--name", config.DEVICE_NAME],
30 30
                                shell=False,
31 31
                                # We pipe the output to an internal pipe
32
-                               stderr=subprocess.PIPE)
32
+                               stdout=subprocess.PIPE,
33
+                               stderr=subprocess.STDOUT)
33 34
         self.__state__ = None
34 35
         self.__preload_state__ = False
35 36
         self.__title__ = None
36
-        self.__spot_id__ = None
37
-        self.__spot_id_preload__ = None
37
+        self.__title_preload__ = None
38
+        self.__title_published__ = None
38 39
         self.set_state(False)
39 40
         self.set_title("")
40 41
 
41 42
     def run(self):
42 43
         while True:
43
-            output = self.__process__.stderr.readline()
44
+            output = self.__process__.stdout.readline()
44 45
             # Polling returns None when the program is still running, return_code otherwise
45 46
             return_code = self.__process__.poll()
46 47
             if return_code is not None:
@@ -53,15 +54,15 @@ class librespot(object):
53 54
                 out_txt = output.decode('utf-8').strip('\n').strip()
54 55
                 out_txt = out_txt[out_txt.find(']') + 2:]
55 56
                 #logger.debug("librespot output: %s", out_txt)
56
-                # TODO: Parse for "librespot output: Loading <Here Ever After> with Spotify URI <spotify:track:0zckHMfaB6vT5o23ZVBLHJ>"
57 57
                 if out_txt.lower().startswith("loading"):
58 58
                     logger.debug("librespot: %s", out_txt)
59
+                    title = out_txt[out_txt.index("<") + 1:out_txt.index(">")]
59 60
                     if self.__preload_state__:
60
-                        self.__spot_id_preload__ = out_txt.split("<")[2][:-1]
61
-                        logger.info("Upcomming Track-ID %s identified", self.__spot_id__)
61
+                        self.__title_preload__ = title
62
+                        logger.info("Upcomming Title %s identified", title)
62 63
                     else:
63
-                        self.__spot_id__ = out_txt.split("<")[2][:-1]
64
-                        logger.info("Current Track-ID %s identified", self.__spot_id__)
64
+                        self.__title__ = title
65
+                        logger.info("Current Title %s identified", title)
65 66
                 if "command=" in out_txt:
66 67
                     command = out_txt[out_txt.find('command=') + 8:].strip().lower()
67 68
                     logger.debug("librespot command: %s", command)
@@ -71,29 +72,19 @@ class librespot(object):
71 72
                         self.set_state(command.split(',')[2].strip() == 'true')
72 73
                         #
73 74
                         self.__preload_state__ = False
74
-                        self.__spot_id__ = self.__spot_id_preload__
75
+                        if self.__title_preload__ is not None:
76
+                            self.__title__ = self.__title_preload__
77
+                            self.__title_preload__ = None
75 78
                     #
76 79
                     elif command in self.ON_CMD:
77 80
                         self.set_state(True)
78 81
                     elif command in self.OFF_CMD:
79 82
                         self.set_state(False)
80 83
                 if self.__state__:
81
-                    self.set_title(self.get_title_by_id())
84
+                    self.set_title(self.__title__)
82 85
                 else:
83 86
                     self.set_title("")
84 87
 
85
-
86
-    def get_title_by_id(self):
87
-        if self.__spot_id__ is None:
88
-            return ""
89
-        else:
90
-            sp = spotipy.Spotify(auth_manager=SpotifyClientCredentials(client_id=config.SPOTIFY_CLIENT_ID,client_secret=config.SPOTIFY_CLIENT_SECRET))
91
-            try:
92
-                track = sp.track(self.__spot_id__)
93
-            except Exception:
94
-                return None
95
-            return track["artists"][0]["name"] + " - " + track["name"]
96
-
97 88
     def set_state(self, target_state):
98 89
         if target_state != self.__state__:
99 90
             self.__state__ = target_state
@@ -101,14 +92,14 @@ class librespot(object):
101 92
             self.__state_callback__(self.__state__)
102 93
 
103 94
     def set_title(self, title):
104
-        if title != self.__title__:
105
-            self.__title__ = title
106
-            logger.info("spotify title changed to \"%s\"", self.__title__)
107
-            self.__title_callback__(self.__title__)
95
+        if self.__title_published__ != title:
96
+            self.__title_published__= title
97
+            logger.info("spotify title changed to \"%s\"", title)
98
+            self.__title_callback__(title)
108 99
             
109 100
 
110 101
 def send_state_msg_mqtt(state):
111
-    client= paho.Client("spotify")
102
+    client= paho.Client(config.APP_NAME)
112 103
     client.username_pw_set(config.MQTT_USER, config.MQTT_PASS)
113 104
     try:
114 105
         client.connect(config.MQTT_SERVER, 1883)
@@ -119,7 +110,7 @@ def send_state_msg_mqtt(state):
119 110
         logger.warning("Erro while sending state information")
120 111
 
121 112
 def send_title_msg_mqtt(title):
122
-    client= paho.Client("spotify")
113
+    client= paho.Client(config.APP_NAME)
123 114
     client.username_pw_set(config.MQTT_USER, config.MQTT_PASS)
124 115
     try:
125 116
         client.connect(config.MQTT_SERVER, 1883)

Loading…
Cancel
Save