ompb.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #!/usr/bin/env python
  2. #
  3. # Author: Ben Holroyd <holroyd.ben@gmail.com>
  4. # License: GPL 3.0+
  5. #
  6. # This script requires python-mpd
  7. #
  8. # Usage:
  9. # Put an entry in ~/.config/openbox/menu.xml:
  10. # <menu id="mpd" label="MPD" execute="~/.config/openbox/scripts/ompb.py" />
  11. #
  12. import mpd, os, sys, socket
  13. mpdport = 6600
  14. musicfolder ='/home/eddie/Music'
  15. filelist = False #potentially slow and unwieldy with a large collection of music
  16. playlist = False #same for this
  17. program = sys.argv[0]
  18. client = mpd.MPDClient()
  19. try:
  20. client.connect("localhost", mpdport)
  21. except socket.error:
  22. print "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
  23. print "<openbox_pipe_menu>"
  24. print " <item label=\"MPD not running, click to start\">"
  25. print " <action name=\"Execute\"><execute>mpd</execute></action>"
  26. print " </item>"
  27. print "</openbox_pipe_menu>"
  28. sys.exit(0)
  29. song = client.currentsong()
  30. stats = client.stats()
  31. status = client.status()
  32. def play():
  33. if status['state'] == "stop" or status['state'] == "pause":
  34. client.play()
  35. elif status['state'] == "play":
  36. client.pause()
  37. def volume(vol):
  38. if vol == "up":
  39. client.setvol(int(status['volume'])+10)
  40. elif vol == "down":
  41. client.setvol(int(status['volume'])-10)
  42. try:
  43. if (sys.argv[1] == "play"): play()
  44. elif (sys.argv[1] == "stop"): client.stop()
  45. elif (sys.argv[1] == "prev"): client.previous()
  46. elif (sys.argv[1] == "next"): client.next()
  47. elif (sys.argv[1] == "add"): client.add(sys.argv[2]); client.play()
  48. elif (sys.argv[1] == "clear"): client.clear()
  49. elif (sys.argv[1] == "volume"): volume(sys.argv[2])
  50. elif (sys.argv[1] == "playlist"):
  51. client.delete(client.playlist().index(sys.argv[2]))
  52. elif sys.argv[1] == "random":
  53. client.random(int(not int(client.status()['random'])and True or False))
  54. elif sys.argv[1] == "repeat":
  55. client.repeat(int(not int(client.status()['repeat'])and True or False))
  56. except IndexError:
  57. pass
  58. def item_entry(indent, label, option = '', song = ''):
  59. """label = label on menu, option = play/pause/stop etc, song = path to song """
  60. print "%s<item label=\"%s\">"%(indent, label)
  61. print "%s <action name=\"Execute\"><execute>%s %s '%s'</execute></action>" % (indent, program, option, song)
  62. print "%s</item>" % (indent)
  63. def file_walk(dir,indent):
  64. """ walks through music directory building a menu to view albums"""
  65. files = os.listdir(dir)
  66. files.sort()
  67. for file in files:
  68. path = os.path.join(dir,file)
  69. if os.path.isdir(path):
  70. print "%s<menu id=\"%s\" label=\"%s\">"%(indent, file, file)
  71. item_entry(indent+' ','Add all to playlist','add' ,path.replace(musicfolder,''))
  72. print "%s <separator />" % indent
  73. file_walk(path,indent+' ')
  74. print "%s</menu>" % indent
  75. else:
  76. item_entry(indent,file,'add',path.replace(musicfolder,''))
  77. indent = indent[2:]
  78. def track_info(label):
  79. print " <menu id=\"%s\" label=\"%s\">"%(label,label)
  80. print " <item label=\"Artist: %s\"/>" % song['artist']
  81. print " <item label=\"Album: %s\"/>" % song['album']
  82. print " <item label=\"Tracklength: %.2f\"/>" % ((int(song['time'])/60)+(int(song['time'])%60.0/100))
  83. print " <item label=\"Track: %s\"/>" % song['track']
  84. print " <item label=\"filetype: %s\"/>" % song['file'][song['file'].rfind('.')+1:]
  85. #print " <item label=\"Genre: %s\"/>" % song['genre']
  86. print " </menu>"
  87. print "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
  88. print "<openbox_pipe_menu>"
  89. if status['state'] != "stop":
  90. track_info("Playing: %s - " % song['artist'])
  91. track_info(song['title'])
  92. print " <separator />"
  93. print " <item label=\"Status: %s\"/>" % {'play':'Playing','pause':'Paused','stop':'Stopped'}[status['state']]
  94. print " <separator />"
  95. item_entry(' ', 'Play/Pause', 'play')
  96. item_entry(' ', 'Stop', 'stop')
  97. item_entry(' ', 'Prev', 'prev')
  98. item_entry(' ', 'Next', 'next')
  99. print " <separator />"
  100. if filelist == True:
  101. print " <menu id=\"Albums\" label=\"Albums\">"
  102. file_walk(musicfolder,' ')
  103. print " </menu>"
  104. print " <separator />"
  105. if playlist == True:
  106. print " <menu id=\"Playlist\" label=\"Playlist\">"
  107. print " <item label=\"Click to remove from playlist\"/>"
  108. print " <separator />"
  109. for entries in client.playlist():
  110. item_entry(' ', entries, 'playlist', entries)
  111. print " </menu>"
  112. print " <separator />"
  113. item_entry(' ', 'Clear Playlist', 'clear')
  114. item_entry(' ', 'Random %s' % (int(status['random']) and '[On]' or '[Off]'), 'random')
  115. item_entry(' ', 'Repeat %s' % (int(status['repeat']) and '[On]' or '[Off]'), 'repeat')
  116. print " <menu id=\"volume\" label=\"Volume [%s]\">" % (int(status['volume']) > 0 and status['volume']+'%' or 'mute')
  117. item_entry(' ', 'Volume + 10\% ', 'volume up')
  118. item_entry(' ', 'Volume - 10\%', 'volume down')
  119. print " </menu>"
  120. print " <separator />"
  121. print " <menu id=\"stats\" label=\"Database Stats\">"
  122. print " <item label=\"Artists in database: %s\"/>" % stats['artists']
  123. print " <item label=\"Albums in database: %s\"/>" % stats['albums']
  124. print " <item label=\"Songs in database: %s\"/>" % stats['songs']
  125. print " </menu>"
  126. print "</openbox_pipe_menu>"
  127. sys.exit(0)