-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathradiator.py
executable file
·146 lines (118 loc) · 3.81 KB
/
radiator.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env python
from mpd import MPDClient
from subprocess import call
import sqlite3, time
import RPi.GPIO as GPIO
class Radiator:
def __init__(self, db = '/home/pi/radiator/radiator.sqlite3'):
self.conf = {}
self.stations = []
self.homeDir = "/home/pi/radiator"
#DB SETUP
self.db = sqlite3.connect(db)
self.c = self.db.cursor()
#GET CONFIG
self.c.execute('SELECT key, value FROM conf')
for row in self.c:
self.conf[row[0]] = row[1]
#MPD SETUP
self.mpd = MPDClient()
self.mpd.connect("/var/run/mpd/socket", 6600)
#SET STATIONS
self.c.execute('SELECT id, name, url FROM stations ORDER BY stations.id DESC')
for row in self.c:
self.stations.append({'id': row[0], 'name': row[1], 'url': row[2]})
if self.conf['stationsV'] != self.conf['stationsU'] or len(self.mpd.playlistinfo()) != len(self.stations):
self.mpd.clear()
for st in self.stations:
print st['url']
st['pl_id'] = self.mpd.addid(st['url'])
self.c.execute("UPDATE conf SET value = '%s' WHERE key = 'stationsU'" % self.conf['stationsV'])
self.db.commit()
else:
for st in self.stations:
st['pl_id'] = self.getPlStationByUrl(st['url'])['id']
#AUTO PLAY!
self.play()
#SET BUTTONS AND LED
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
#button[0] - pin, button[1] - last state
self.buttons = [[18, 0], [23, 0], [24, 0]]
self.LED = 4;
GPIO.setup(self.LED,GPIO.OUT)
GPIO.output(self.LED, False)
for button in self.buttons:
GPIO.setup(button[0],GPIO.IN)
def pressed(self):
for button in self.buttons:
input = GPIO.input(button[0])
if ((not button[1]) and input):
pressed = button[0]
else:
pressed = False;
button[1] = input
time.sleep(0.05)
if pressed:
return pressed
return False
def play(self):
self.mpd.play();
def next(self):
playlist = self.mpd.playlistinfo();
if self.mpd.currentsong()['id'] == playlist[len(playlist) - 1]['id']:
self.mpd.playid(playlist[0]['id'])
else:
self.mpd.next()
def previous(self):
playlist = self.mpd.playlistinfo();
if self.mpd.currentsong()['id'] == playlist[0]['id']:
self.mpd.playid(playlist[len(playlist) - 1]['id'])
else:
self.mpd.previous()
def getStByPl_id(self, id):
for st in self.stations:
if st['pl_id'] == id:
return st;
return False
def getPlStationByUrl(self, url):
plStations = self.mpd.playlistinfo()
for st in plStations:
if st['file'] == url:
return st;
return False
def bookmark(self):
if self.mpd.status()['state'] != 'play':
return
try:
station = self.getStByPl_id(self.mpd.status()['songid'])
songTitle = self.mpd.currentsong()['title']
sampleName = str(int(time.time()))
except:
return
GPIO.output(self.LED, True)
self.c.execute("INSERT INTO bookmarks (station_id, name) VALUES (?, ?)", [station['id'], songTitle])
bookmarkId = self.c.lastrowid
self.db.commit()
call(['streamripper', station['url'], '-d', "%s/www/bookmarks" % self.homeDir, '-A', '-a', "%s_" % sampleName ,'-l', '10'])
call(['lame', "%s/www/bookmarks/%s_.mp3" % (self.homeDir, sampleName), "%s/www/bookmarks/%s.mp3" % (self.homeDir, sampleName) ])
call(['rm', "%s/www/bookmarks/%s_.cue" % (self.homeDir, sampleName)])
call(['rm', "%s/www/bookmarks/%s_.mp3" % (self.homeDir, sampleName)])
self.c.execute("UPDATE bookmarks SET sample = '%s' WHERE id = %s" % (sampleName, bookmarkId))
self.db.commit()
GPIO.output(self.LED, False)
def action(self):
button = self.pressed()
if not button:
return
if button == 23:
self.next()
elif button == 24:
self.previous()
elif button == 18:
self.bookmark()
if __name__ == '__main__':
r = Radiator()
r.play()
while True:
r.action()