Advertisement
Guest User

box.py

a guest
Apr 13th, 2018
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.22 KB | None | 0 0
  1. #!/usr/bin/env python
  2. from mpd import MPDClient
  3. #from readtest import *
  4. import re
  5. from CardList import CardList
  6. from Reader import Reader
  7. import sys
  8.  
  9.  
  10. def connectMPD():
  11.         try:
  12.                 client = MPDClient()               # create client object
  13.                 client.timeout = 200               # network timeout in seconds (floats allowed), default: None
  14.                 client.idletimeout = None
  15.                 print "Connecting..."
  16.                 client.connect("localhost", 6600)
  17.                 print "Connected!"
  18.                 return client
  19.         except:
  20.                 print 'Could not connect to MPD server'
  21.  
  22. def play(client, plist):
  23.         try:
  24.                 client.stop()
  25.                 client.clear()
  26.                 client.add(plist)
  27. #               if re.search('playlist',plist):     #un-comment for turning on shuffle at start directly
  28. #                       client.shuffle()            #un-comment this line as well for direct-shuffle
  29.                 client.play()
  30.         except:
  31.                 print 'Could not play playlist %s' % plist
  32.  
  33. reader = Reader()
  34. cardList = CardList()
  35.  
  36. print 'Ready: place a card on top of the reader'
  37.  
  38. while True:
  39.         try:
  40.                 card = reader.readCard()
  41.                 print 'Read card', card
  42.                 plist = cardList.getPlaylist(card)
  43.                 print 'Playlist', plist
  44.                 if plist != '':
  45.                         client = connectMPD()
  46.                         if plist=='pause':                  #pause
  47.                                 client.pause()
  48.                         elif plist=='next':                 #next song
  49.                                 client.next()
  50.                         elif plist=='play':                 #play song
  51.                                 client.play()
  52.                         elif plist=='previous':             #previous song
  53.                                 client.previous()
  54.                         elif plist=='shuffle':              #turn on/off shuffle
  55.                                 client.shuffle()
  56.                         elif plist=='voldown':              #volume down in steps
  57.                                 client.status()['volume']
  58.                                 client.status()['state']
  59.                                 level = int(client.status()['volume']) - 10 #change to desired amount for decreasing volume
  60.                                 level = max(min(level, 100), 0)
  61.                                 client.setvol(level)
  62.                                 client.status()['volume']
  63.                         elif plist=='volup':                #volume up in steps
  64.                                 client.status()['volume']
  65.                                 client.status()['state']
  66.                                 level = int(client.status()['volume']) + 10 #change to desired amount for increasing volume
  67.                                 level = max(min(level, 100), 0)
  68.                                 client.setvol(level)
  69.                                 client.status()['volume']
  70.                         elif plist=='mute':                 #volume mute
  71.                                 client.setvol(0)
  72.                         else:
  73.                                 play(client, plist)
  74.                         client.close()
  75.         except KeyboardInterrupt:
  76.                 sys.exit(0)
  77.         except:
  78.                 pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement