diff options
| author | Feuerfuchs <git@feuerfuchs.eu> | 2018-11-09 11:04:07 +0100 |
|---|---|---|
| committer | Feuerfuchs <git@feuerfuchs.eu> | 2018-11-09 11:04:07 +0100 |
| commit | d1e7ea624200f94feaf7b2e5fa65e280029e60bb (patch) | |
| tree | fa561ce270cc96fd75d6b4bb8f905a9c66c2dc0a | |
| parent | Add license (diff) | |
| download | mpris-to-text-d1e7ea624200f94feaf7b2e5fa65e280029e60bb.tar.gz mpris-to-text-d1e7ea624200f94feaf7b2e5fa65e280029e60bb.tar.bz2 mpris-to-text-d1e7ea624200f94feaf7b2e5fa65e280029e60bb.zip | |
Add support for cli arguments, better output, redraw on resize
| -rwxr-xr-x | music_update.py | 96 |
1 files changed, 84 insertions, 12 deletions
diff --git a/music_update.py b/music_update.py index da2635f..d9f14c3 100755 --- a/music_update.py +++ b/music_update.py | |||
| @@ -4,6 +4,8 @@ import sys | |||
| 4 | import asyncio | 4 | import asyncio |
| 5 | import re | 5 | import re |
| 6 | import threading | 6 | import threading |
| 7 | import argparse | ||
| 8 | import signal | ||
| 7 | 9 | ||
| 8 | from blessed import Terminal | 10 | from blessed import Terminal |
| 9 | 11 | ||
| @@ -26,25 +28,31 @@ player_id_to_index = {} | |||
| 26 | active_player = "" | 28 | active_player = "" |
| 27 | current_output = "" | 29 | current_output = "" |
| 28 | 30 | ||
| 31 | filename = "" | ||
| 32 | meta_format = "" | ||
| 33 | meta_format_artist = "" | ||
| 34 | meta_format_title = "" | ||
| 35 | meta_format_album = "" | ||
| 36 | |||
| 29 | 37 | ||
| 30 | def track_string(metadata): | 38 | def track_string(metadata): |
| 31 | artist = metadata["xesam:artist"][1][0] if "xesam:artist" in metadata else "" | 39 | artist = metadata["xesam:artist"][1][0] if "xesam:artist" in metadata else "" |
| 32 | title = metadata["xesam:title"][1] if "xesam:title" in metadata else "" | 40 | title = metadata["xesam:title"][1] if "xesam:title" in metadata else "" |
| 33 | album = metadata["xesam:album"][1] if "xesam:album" in metadata else "" | 41 | album = metadata["xesam:album"][1] if "xesam:album" in metadata else "" |
| 34 | 42 | ||
| 35 | track = "" | 43 | return meta_format.format( |
| 36 | if artist != "": track = artist + " " | 44 | artist = meta_format_artist.format(artist) if artist != "" else "", |
| 37 | track = track + "\"" + title + "\"" | 45 | title = meta_format_title.format(title) if title != "" else "", |
| 38 | if album != "": track = track + " from \"" + album + "\"" | 46 | album = meta_format_album.format(album) if title != "" else "" |
| 39 | 47 | ) | |
| 40 | return track + " " | ||
| 41 | 48 | ||
| 42 | 49 | ||
| 43 | def write_track(track): | 50 | def write_track(track): |
| 51 | global filename | ||
| 44 | global current_output | 52 | global current_output |
| 45 | 53 | ||
| 46 | current_output = track | 54 | current_output = track |
| 47 | f = open("/mnt/Data/stream_info.txt", "w") | 55 | f = open(filename, "w") |
| 48 | f.write(track) | 56 | f.write(track) |
| 49 | f.close() | 57 | f.close() |
| 50 | 58 | ||
| @@ -145,6 +153,7 @@ def draw_menu(): | |||
| 145 | global refresh_flag | 153 | global refresh_flag |
| 146 | global exit_flag | 154 | global exit_flag |
| 147 | global current_output | 155 | global current_output |
| 156 | global filename | ||
| 148 | 157 | ||
| 149 | while not exit_flag: | 158 | while not exit_flag: |
| 150 | with refresh_cond: | 159 | with refresh_cond: |
| @@ -154,25 +163,36 @@ def draw_menu(): | |||
| 154 | refresh_flag = False | 163 | refresh_flag = False |
| 155 | 164 | ||
| 156 | with term.fullscreen(): | 165 | with term.fullscreen(): |
| 157 | print(term.move(0, 0) + term.bold("MPRIS to Text") + "\n") | 166 | print(term.move(0, 0) + term.bold_bright_white_on_bright_black(("{0:<{width}}").format("MPRIS To Text", width=term.width)) + "\n") |
| 167 | print(term.move_x(2) + term.bold("Player: ") + term.move_up()) | ||
| 158 | 168 | ||
| 159 | for i in range(len(players)): | 169 | for i in range(len(players)): |
| 160 | player = players[i] | 170 | player = players[i] |
| 161 | output = "%d: %s" % (i, player[1]) | 171 | output = "%d: %s" % (i, player[1]) |
| 162 | 172 | ||
| 163 | if players[player_id_to_index[active_player]][0] == player[0]: | 173 | if players[player_id_to_index[active_player]][0] == player[0]: |
| 164 | print(term.move_x(8) + term.standout_bold(output)) | 174 | print(term.move_x(10) + term.standout(output)) |
| 165 | else: | 175 | else: |
| 166 | print(term.move_x(8) + output) | 176 | print(term.move_x(10) + output) |
| 167 | 177 | ||
| 168 | print(term.move_x(0) + "\n" + term.bold("Current output:") + " " + current_output) | 178 | print(term.move_x(2) + term.bold("File: ") + filename) |
| 179 | print(term.move_x(2) + term.bold("Output: ") + "\n".join(term.wrap(current_output, width=term.width - 10, subsequent_indent=" " * 10))) | ||
| 169 | 180 | ||
| 170 | print(term.move_x(0) + "\n\nEnter number to select player or q to exit." + term.move_up()) | 181 | print(term.move_x(0) + "\nEnter number to select player or q to exit." + term.move_up()) |
| 171 | 182 | ||
| 172 | with term.fullscreen(): | 183 | with term.fullscreen(): |
| 173 | print(term.move(0, 0) + "Exiting...") | 184 | print(term.move(0, 0) + "Exiting...") |
| 174 | 185 | ||
| 175 | 186 | ||
| 187 | def on_resize(*args): | ||
| 188 | global refresh_cond | ||
| 189 | global refresh_flag | ||
| 190 | |||
| 191 | with refresh_cond: | ||
| 192 | refresh_flag = True | ||
| 193 | refresh_cond.notify() | ||
| 194 | |||
| 195 | |||
| 176 | def init_dbus(): | 196 | def init_dbus(): |
| 177 | global bus | 197 | global bus |
| 178 | global loop | 198 | global loop |
| @@ -219,6 +239,58 @@ def init_blessed(): | |||
| 219 | exit_task.set_result(True) | 239 | exit_task.set_result(True) |
| 220 | 240 | ||
| 221 | 241 | ||
| 242 | def read_args(): | ||
| 243 | global filename | ||
| 244 | global meta_format | ||
| 245 | global meta_format_artist | ||
| 246 | global meta_format_title | ||
| 247 | global meta_format_album | ||
| 248 | |||
| 249 | parser = argparse.ArgumentParser(description="Write metadata from MPRIS-compliant media players into a text file.") | ||
| 250 | parser.add_argument("--file", | ||
| 251 | type = str, | ||
| 252 | dest = "filename", | ||
| 253 | default = "/tmp/mpris_info.txt", | ||
| 254 | help = "Full path to file (default: \"/tmp/mpris_info.txt\")" | ||
| 255 | ) | ||
| 256 | parser.add_argument("--format-order", | ||
| 257 | type = str, | ||
| 258 | dest = "format", | ||
| 259 | default = "{artist}{title}{album} ", | ||
| 260 | help = "Format string (default: \"{artist}{title}{album} \")" | ||
| 261 | ) | ||
| 262 | parser.add_argument("--format-artist", | ||
| 263 | type = str, | ||
| 264 | dest = "format_artist", | ||
| 265 | default = "{} ", | ||
| 266 | help = "Format string for the artist part (default: \"{} \")" | ||
| 267 | ) | ||
| 268 | parser.add_argument("--format-title", | ||
| 269 | type = str, | ||
| 270 | dest = "format_title", | ||
| 271 | default = "\"{}\"", | ||
| 272 | help = "Format string for the title part (default: \"\"{}\"\")" | ||
| 273 | ) | ||
| 274 | parser.add_argument("--format-album", | ||
| 275 | type = str, | ||
| 276 | dest = "format_album", | ||
| 277 | default = " from \"{}\"", | ||
| 278 | help = "Format string for the album part (default: \" from \"{}\"\")" | ||
| 279 | ) | ||
| 280 | |||
| 281 | args = parser.parse_args() | ||
| 282 | filename = args.filename | ||
| 283 | meta_format = args.format | ||
| 284 | meta_format_artist = args.format_artist | ||
| 285 | meta_format_title = args.format_title | ||
| 286 | meta_format_album = args.format_album | ||
| 287 | |||
| 288 | |||
| 289 | |||
| 290 | read_args() | ||
| 291 | |||
| 292 | signal.signal(signal.SIGWINCH, on_resize) | ||
| 293 | |||
| 222 | init_dbus() | 294 | init_dbus() |
| 223 | 295 | ||
| 224 | blessed_thread = threading.Thread(target=init_blessed) | 296 | blessed_thread = threading.Thread(target=init_blessed) |
