-
Notifications
You must be signed in to change notification settings - Fork 1
/
box.py
executable file
·85 lines (74 loc) · 1.75 KB
/
box.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/env python
from mpd import MPDClient
import re
from CardList import CardList
import sys
import os
import RPi.GPIO as GPIO
import MFRC522
from Reader import readCard
import time
# Capture SIGINT for cleanup when the script is aborted
def end_read(signal,frame):
global continue_reading
print "Ctrl+C captured, ending read."
continue_reading = False
GPIO.cleanup()
def connectMPD():
try:
client = MPDClient() # create client object
client.timeout = 200 # network timeout in seconds (floats allowed), default: None
client.idletimeout = None
print "Connecting..."
client.connect("localhost", 6600)
print "Connected!"
return client
except:
print 'Could not connect to MPD server'
def playlist(client,plist):
try:
plist2 = re.sub('file:','',plist)
print plist
for root,dirs,files in os.walk(plist2):
for filename in files:
filename = plist + filename
print filename
client.add(filename)
return plist
except Exception as e:
print(e)
def play(client, plist):
try:
client.stop()
client.clear()
if re.search('file', plist):
playlist(client,plist)
client.shuffle()
if re.search('spotify',plist):
client.add(plist)
client.shuffle()
client.play()
except:
print 'Could not play playlist %s' % plist
MIFAREReader = MFRC522.MFRC522()
cardList = CardList()
print 'Ready: place a card on top of the reader'
while True:
try:
card = readCard(MIFAREReader)
print 'Read card', card
plist = cardList.getPlaylist(card)
if plist != '':
print 'Playlist', plist
client = connectMPD()
if plist=='pause':
client.pause()
else:
play(client, plist)
client.close()
time.sleep(1)
except KeyboardInterrupt:
GPIO.cleanup()
sys.exit(0)
except:
pass