-
Notifications
You must be signed in to change notification settings - Fork 1
/
web_query.py
244 lines (220 loc) · 8.67 KB
/
web_query.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
__author__ = 'Aran'
import urllib
import time
from utilities import *
import os
import glob
import mp3play
import MySQLdb
import pyttsx
import pythoncom
from datetime import datetime
from random import choice
def get_mp3s():
mp3s =[]
os.chdir(r'PATH TO MUSIC LIBRARY')
for files in glob.glob("*.mp3"):
mp3s.append(files)
return mp3s
def get_playlists():
playlists =[]
os.chdir(r'PATH TO MUSIC LIBRARY WITH TXT PLAYLISTS')
for files in glob.glob("*.txt"):
playlists.append(files)
return playlists
class Playlist:
def __init__(self, listname):
self.file = r'PATH TO MUSIC LIBRARY WITH TXT PLAYLISTS'+listname
with open(self.file) as f:
self.songs = f.readlines()
def nextsong(self):
return choice(self.songs).rstrip('\n')+'.mp3'
def media_loop():
myDB = MySQLdb.connect(host="************",user="**********",passwd="*********",db="***********")
SONG = None
SONG_NAME = ""
LIST_NAME = ""
lastrecord = datetime.now()
updated = False
naturalpause = False
currentlist = None
while True:
cHandler = myDB.cursor()
# update past state list every 10 seconds
delta = datetime.now() - lastrecord
if delta.seconds > 10 or updated:
action = '0'
if SONG is not None:
currentsong = str(SONG_NAME)
if SONG.isplaying():
action = '1'
else:
currentsong = None
if currentlist is not None:
playinglist = str(LIST_NAME)
else:
playinglist = None
trip = HueCommand(None, None).get_states([1])
if not trip[0]['on']:
on = '0'
else:
on = '1'
try:
if currentsong is not None:
if playinglist is not None:
cHandler.execute("INSERT INTO paststates (song, playlist, action, lightstate1, lighthue1, lightbri1) VALUES ('"+currentsong.replace("'", "\\'")+"', '"+playinglist.replace("'", "\\'")+"', '"+action+"', '"+on+"', '"+str(trip[0]['hue'])+"', '"+str(trip[0]['bri'])+"')")
else:
cHandler.execute("INSERT INTO paststates (song, action, lightstate1, lighthue1, lightbri1) VALUES ('"+currentsong.replace("'", "\\'")+"', '"+action+"', '"+on+"', '"+str(trip[0]['hue'])+"', '"+str(trip[0]['bri'])+"')")
else:
cHandler.execute("INSERT INTO paststates (lightstate1, lighthue1, lightbri1) VALUES ('"+on+"', '"+str(trip[0]['hue'])+"', '"+str(trip[0]['bri'])+"')")
except Exception as e:
myDB = MySQLdb.connect(host="****************",user="*********",passwd="*********",db="************")
print("disconnected... with "+ e.message)
continue
# Reset events
lastrecord = datetime.now()
if updated:
updated = False
# handle song ending
if SONG is not None and not SONG.isplaying() and not naturalpause:
if currentlist is not None:
song_name = currentlist.nextsong()
else:
song_name = choice(get_mp3s())
SONG.stop()
base_path = r'PATH TO MUSIC'
path = base_path + song_name
try:
mp3 = mp3play.load(path)
mp3.play()
except:
print "error playing "+song_name
continue
SONG = mp3
SONG_NAME = song_name
time.sleep(1.5)
# Scan for web commands
try:
cHandler.execute("SELECT * FROM goalstates ORDER BY time")
except:
myDB = MySQLdb.connect(host="************",user="**********",passwd="*******",db="**********")
print("disconnected... retrying")
continue
command = cHandler.fetchone()
# Execute commands
if command is not None:
updated = True
# Clear command from db
try:
cHandler.execute("DELETE FROM goalstates WHERE id = '"+str(command[0])+"'")
except:
myDB = MySQLdb.connect(host="**********",user="********",passwd="***********",db="************88")
print("disconnected... retrying")
continue
# Pull all command data
song = str(command[1])
playlist = str(command[2])
action = int(command[3])
voice = str(command[4])
light1state = int(command[5])
light1hue = int(command[6])
light1bri = int(command[7])
# Lights Stuff
light1command = {}
if light1state == 1:
light1command['on'] = True
elif light1state == 0:
light1command['on'] = False
if light1hue != -1:
light1command['hue'] = int(light1hue)
if light1bri != -1:
light1command['bri'] = int(light1bri)
if len(light1command.keys()) > 0:
try:
HueCommand(0, [BulbState(1, light1command),
BulbState(2, light1command),
BulbState(3, light1command)]).execute()
except:
print "unable to change lights"
# VOICE STUFF
if len(voice) > 0:
pythoncom.CoInitialize()
engine = pyttsx.init()
rate = engine.getProperty('rate')
engine.setProperty('rate', rate-13)
engine.say(voice)
if SONG is not None:
SONG.pause()
engine.runAndWait()
if SONG is not None:
SONG.unpause()
# MP3 STUFF
if playlist is not None and len(playlist) > 0:
currentlist = Playlist(playlist)
if SONG is not None:
SONG.stop()
list_song = currentlist.nextsong()
try:
mp3 = mp3play.load(r'PATH TO MUSIC' + list_song)
mp3.play()
except:
print "error playing "+list_song
continue
LIST_NAME = playlist
SONG_NAME = list_song
SONG = mp3
elif song is not None and len(song) > 0:
if SONG is not None:
SONG.stop()
try:
mp3 = mp3play.load(r'PATH TO MUSIC' + song)
mp3.play()
except:
print "error playing "+song
continue
SONG = mp3
SONG_NAME = song
currentlist = None
elif action != 0 and SONG != None:
if action == 1:
if SONG.ispaused():
SONG.unpause()
naturalpause = False
elif action == 2:
if SONG.isplaying():
SONG.pause()
naturalpause = True
elif action == 4:
SONG.stop()
SONG = None
currentlist = None
elif action == 3:
if currentlist is not None:
song_name = currentlist.nextsong()
else:
song_name = choice(get_mp3s())
SONG.stop()
base_path = r'PATH TO MUSIC'
path = base_path + song_name
try:
mp3 = mp3play.load(path)
mp3.play()
except:
print "error playing "+song_name
continue
SONG_NAME = song_name
SONG = mp3
def update_stored_state():
myDB = MySQLdb.connect(host="**********",user="*********",passwd="*********",db="***********")
cHandler = myDB.cursor()
cHandler.execute("DELETE FROM paststates")
cHandler.execute("DELETE FROM musiclibrary")
cHandler.execute("DELETE FROM playlists")
for mp3 in get_mp3s():
sane_mp3 = str(mp3).replace("'", "\\'")
cHandler.execute("INSERT INTO musiclibrary (song) VALUES ('"+sane_mp3+"')")
print "inserting song "+sane_mp3+" into library"
for plist in get_playlists():
sane_plist = str(plist).replace("'", "\\'")
cHandler.execute("INSERT INTO playlists (list) VALUES ('"+sane_plist+"')")
print "inserting playlist "+sane_plist+" into library"