Another copy of my dotfiles. Because I don't completely trust GitHub.
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.

122 lines
2.9 KiB

4 years ago
  1. #!/bin/python
  2. import sys
  3. import dbus
  4. import argparse
  5. parser = argparse.ArgumentParser()
  6. parser.add_argument(
  7. '-t',
  8. '--trunclen',
  9. type=int,
  10. metavar='trunclen'
  11. )
  12. parser.add_argument(
  13. '-f',
  14. '--format',
  15. type=str,
  16. metavar='custom format',
  17. dest='custom_format'
  18. )
  19. parser.add_argument(
  20. '-p',
  21. '--playpause',
  22. type=str,
  23. metavar='play-pause indicator',
  24. dest='play_pause'
  25. )
  26. parser.add_argument(
  27. '--font',
  28. type=str,
  29. metavar='the index of the font to use for the main label',
  30. dest='font'
  31. )
  32. parser.add_argument(
  33. '--playpause-font',
  34. type=str,
  35. metavar='the index of the font to use to display the playpause indicator',
  36. dest='play_pause_font'
  37. )
  38. args = parser.parse_args()
  39. def fix_string(string):
  40. # corrects encoding for the python version used
  41. if sys.version_info.major == 3:
  42. return string
  43. else:
  44. return string.encode('utf-8')
  45. # Default parameters
  46. output = fix_string(u'{play_pause} {artist}: {song}')
  47. trunclen = 35
  48. play_pause = fix_string(u'\u25B6,\u23F8') # first character is play, second is paused
  49. label_with_font = '%{{T{font}}}{label}%{{T-}}'
  50. font = args.font
  51. play_pause_font = args.play_pause_font
  52. # parameters can be overwritten by args
  53. if args.trunclen is not None:
  54. trunclen = args.trunclen
  55. if args.custom_format is not None:
  56. output = args.custom_format
  57. if args.play_pause is not None:
  58. play_pause = args.play_pause
  59. try:
  60. session_bus = dbus.SessionBus()
  61. spotify_bus = session_bus.get_object(
  62. 'org.mpris.MediaPlayer2.spotify',
  63. '/org/mpris/MediaPlayer2'
  64. )
  65. spotify_properties = dbus.Interface(
  66. spotify_bus,
  67. 'org.freedesktop.DBus.Properties'
  68. )
  69. metadata = spotify_properties.Get('org.mpris.MediaPlayer2.Player', 'Metadata')
  70. status = spotify_properties.Get('org.mpris.MediaPlayer2.Player', 'PlaybackStatus')
  71. # Handle play/pause label
  72. play_pause = play_pause.split(',')
  73. if status == 'Playing':
  74. play_pause = play_pause[0]
  75. elif status == 'Paused':
  76. play_pause = play_pause[1]
  77. else:
  78. play_pause = str()
  79. if play_pause_font:
  80. play_pause = label_with_font.format(font=play_pause_font, label=play_pause)
  81. # Handle main label
  82. artist = fix_string(metadata['xesam:artist'][0]) if metadata['xesam:artist'] else ''
  83. song = fix_string(metadata['xesam:title']) if metadata['xesam:title'] else ''
  84. if not artist and not song:
  85. print('')
  86. else:
  87. if len(song) > trunclen:
  88. song = song[0:trunclen]
  89. song += '...'
  90. if ('(' in song) and (')' not in song):
  91. song += ')'
  92. if font:
  93. artist = label_with_font.format(font=font, label=artist)
  94. song = label_with_font.format(font=font, label=song)
  95. print(output.format(artist=artist, song=song, play_pause=play_pause))
  96. except Exception as e:
  97. if isinstance(e, dbus.exceptions.DBusException):
  98. print('')
  99. else:
  100. print(e)