Wikia

Music Player Daemon Community Wiki

Python MPD alarm script

525pages on
this wiki
Talk0


Displays the output from a user shell script in a Tk window (create an executable sh script named 'wakeup' to use the defaults) and Queues a random playlist to mpd and starts playback. The -x command line option will clear the mpd playlist. The Tkinter python module must be installed prior to running this script. Keyboard presses to pause playback and to exit the script have been defined - other keys can be assigned by adding to the keypress function. To use as a wakeup alarm run in your cron table. Note: cron must pass the users X environment to this script, so run a shell script which passes the env then calls this script - e.g. DISPLAY=:0; XAUTHORITY=~username/.Xauthority; export DISPLAY XAUTHORITY

#!/usr/bin/env python 
from Tkinter import *
from mpd import MPDClient, MPDError
import subprocess as sub, thread 
def DisplayScript(script): 
   p = sub.Popen(script, stdout=sub.PIPE, stderr=sub.PIPE)
   output, errors = p.communicate()
   root = Tk()
   root.title('Time to Start Your Day!')
   label = Label(root, text=('\n\n'.join([output,
       'Press the spacebar to snooze, Q to exit'])))
   label.config(font=('roman', 10, 'bold'), foreground='red', bd=8,
        relief=SUNKEN, padx=25, pady=25)
   label.pack(expand=NO,fill=BOTH)
   return root 
from optparse import OptionParser 
usage = (
         __doc__,
         "%prog [options]"
)
usage = "\n".join(usage)
cli_parser = OptionParser(usage)
def create_cli_parser():

# Add the CLI options

cli_parser.add_option('-w', '--window', action='store', type='string',
help="display output from this sh script", default='./wakeup')
cli_parser.add_option('-x', '--clear', action='store_true', 
help="clear the mpd playlist on exit", default=False)
cli_parser.add_option('-b', '--album', action='store_true', 
help="select from a random album", default=False)
cli_parser.add_option('-a', '--artist', action='store_true', 
help="select from a random artist", default=False)
cli_parser.add_option('-g', '--genre', action='store_true', 
help="select from a random genre", default=False)
cli_parser.add_option('-s', '--snooze', action='store', 
type='int', help="specify the snooze interval", default=300)
cli_parser.add_option('-r', '--step', action='store', 
type='int', help="specify the volume ramp step", default=10)
cli_parser.add_option('-i', '--interval', action='store', 
type='int', help="specify the volume ramp interval", default=10)
cli_parser.add_option('-m', '--maximum', action='store', 
type='int', help="specify the maximum volume", default=100)
return cli_parser 




def main(argv): 


   import time
   from random import choice
   mp = MPDClient()
   # Create the command line parser.
   cli_parser = create_cli_parser()

   # Get the options and arguments.
   opts, args = cli_parser.parse_args(argv)
   # make sure opts.maximum is in a valid range
   if opts.maximum > 100:
      opts.maximum = 100

  def MPDstop():
  try:
      mp.connect('localhost','6600')
  except MPDError:
      mp.disconnect()
      mp.connect('localhost','6600')
  mp.stop()
  if opts.clear:
      mp.clear()
  mp.disconnect()
  return
  def MPDpause(seconds):
  try:
     mp.connect('localhost','6600')
  except MPDError:
     mp.disconnect()
     mp.connect('localhost','6600')
  mp.pause()
  time.sleep(seconds)
  try:
     mp.connect('localhost','6600')
  except MPDError:
     mp.disconnect()
     mp.connect('localhost','6600')
  mp.play()
  mp.disconnect()
  return
  def MPDqueue(songs):
  try:
     mp.connect('localhost','6600')
  except MPDError:
     mp.disconnect()
     mp.connect('localhost','6600')
  mp.setvol(0)
  if opts.artist:
     MPDOption(songs, 'artist')
     else:
        if opts.album:
           MPDOption(songs, 'album')
           else:
              if opts.genre:
                 MPDOption(songs, 'genre')
                 else:
                 while len(mp.playlist()) < songs:
                    for i in range(songs):
                        mp.add(choice(mp.list('file')))
                        mp.play()
  for vol in range(0,opts.maximum+1,opts.step):
     mp.setvol(vol)
    time.sleep(opts.interval)
  mp.disconnect()
  return
  def MPDOption(songs, option):
  try:
     mp.connect('localhost','6600')
  except MPDError:
     mp.disconnect()
     mp.connect('localhost','6600')
  mp.setvol(0)
  opt = choice(mp.list(option))
  while len(mp.playlist()) < songs:
     mp.add(choice(mp.find(option, opt))['file'])
  return

  # define keypress events
  def keypress(event):
  if event.keysym == 'space':
      thread.start_new(MPDpause, (opts.snooze,)) # run in thread
      # to return keypress control immediately to main window
  x = event.char
  if x == 'q' or x == 'Q':
      MPDstop()
  win.destroy()
  #if x == 'p': # for debugging
  # mp.connect('localhost','6600')
  # mp.play()
  # mp.disconnect()
  # if x ==
  # add additional keypress events here 

  # Display the Shell Script output
  win = DisplayScript(opts.window)
  # bind keypress events
  win.bind_all('<Key>', keypress)
  # queue 50 random songs, start playback
  thread.start_new(MPDqueue, (50,)) 

  win.mainloop()
  return

if __name__ == "__main__": </code>

  main(sys.argv[1:])

Around Wikia's network

Random Wiki