Linux install and configuration management utilities
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

142 строки
3.6KB

  1. #!/usr/bin/env 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. parser.add_argument(
  39. '-q',
  40. '--quiet',
  41. action='store_true',
  42. help="if set, don't show any output when the current song is paused",
  43. dest='quiet',
  44. )
  45. args = parser.parse_args()
  46. def fix_string(string):
  47. # corrects encoding for the python version used
  48. if sys.version_info.major == 3:
  49. return string
  50. else:
  51. return string.encode('utf-8')
  52. def truncate(name, trunclen):
  53. if len(name) > trunclen:
  54. name = name[:trunclen]
  55. name += '...'
  56. if ('(' in name) and (')' not in name):
  57. name += ')'
  58. return name
  59. # Default parameters
  60. output = fix_string(u'{play_pause} {artist}: {song}')
  61. trunclen = 35
  62. play_pause = fix_string(u'\u25B6,\u23F8') # first character is play, second is paused
  63. label_with_font = '%{{T{font}}}{label}%{{T-}}'
  64. font = args.font
  65. play_pause_font = args.play_pause_font
  66. quiet = args.quiet
  67. # parameters can be overwritten by args
  68. if args.trunclen is not None:
  69. trunclen = args.trunclen
  70. if args.custom_format is not None:
  71. output = args.custom_format
  72. if args.play_pause is not None:
  73. play_pause = args.play_pause
  74. try:
  75. session_bus = dbus.SessionBus()
  76. spotify_bus = session_bus.get_object(
  77. 'org.mpris.MediaPlayer2.spotify',
  78. '/org/mpris/MediaPlayer2'
  79. )
  80. spotify_properties = dbus.Interface(
  81. spotify_bus,
  82. 'org.freedesktop.DBus.Properties'
  83. )
  84. metadata = spotify_properties.Get('org.mpris.MediaPlayer2.Player', 'Metadata')
  85. status = spotify_properties.Get('org.mpris.MediaPlayer2.Player', 'PlaybackStatus')
  86. # Handle play/pause label
  87. play_pause = play_pause.split(',')
  88. if status == 'Playing':
  89. play_pause = play_pause[0]
  90. elif status == 'Paused':
  91. play_pause = play_pause[1]
  92. else:
  93. play_pause = str()
  94. if play_pause_font:
  95. play_pause = label_with_font.format(font=play_pause_font, label=play_pause)
  96. # Handle main label
  97. artist = fix_string(metadata['xesam:artist'][0]) if metadata['xesam:artist'] else ''
  98. song = fix_string(metadata['xesam:title']) if metadata['xesam:title'] else ''
  99. album = fix_string(metadata['xesam:album']) if metadata['xesam:album'] else ''
  100. if (quiet and status == 'Paused') or (not artist and not song and not album):
  101. print('')
  102. else:
  103. if font:
  104. artist = label_with_font.format(font=font, label=artist)
  105. song = label_with_font.format(font=font, label=song)
  106. album = label_with_font.format(font=font, label=album)
  107. # Add 4 to trunclen to account for status symbol, spaces, and other padding characters
  108. print(truncate(output.format(artist=artist,
  109. song=song,
  110. play_pause=play_pause,
  111. album=album), trunclen + 4))
  112. except Exception as e:
  113. if isinstance(e, dbus.exceptions.DBusException):
  114. print('')
  115. else:
  116. print(e)