-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
231 lines (178 loc) · 4.69 KB
/
main.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
#!/usr/bin/python3
import os
import sys
import vlc
import time
inputs = [
'\'/\'',
'\'*\'',
'\'7\'',
'\'8\'',
'\'9\'',
'\'4\'',
'\'5\'',
'\'6\'',
'\'1\'',
'\'2\'',
'\'3\'',
'\'0\'',
'\'.\'',
]
special = [
'\'+\'',
'\'-\'',
'\'\\x7f\'', # Backspace
'\'\\r\'', # Return
'\'\\x1b[H\'', # Home
'\'\\x1b[F\'', # End
'\'\\x1b[5\'', # PageUp
'\'\\x1b[6\'', # PageDown
'\'\\x1b[2\'', # Ins
'\'\\x1b[3~\'', # Del
]
#instance = vlc.Instance('--input-repeat=999999')
instance = vlc.Instance()
player = instance.media_player_new()
currentPresetIndex = 0
numPresets = 0
def loadPresets():
''' Get all the Presets in the current directory '''
l = []
for file in sorted(os.listdir(os.getcwd())):
if file.endswith(".preset"):
print("Preset Found:", os.path.join(os.getcwd(), file))
l.append(os.path.join(os.getcwd(), file))
return l
def getPresetTracks(preset):
''' Get all the links inside of a preset track '''
l = []
with open(preset) as file:
for line in file:
# Need to get rid of those pesky \n's
print(str(len(l)) + ': ', line[:-1])
l.append(line[:-1])
return l
def isYouTubeAudio(link):
import re
if re.match(r'http[s]:\/\/www\.youtube\.com/watch\?v=([\w-]{11})', link) == None:
return False
else:
return True
def getYouTubeAudioTrack(link):
''' Get Audio track of a link '''
import pafy
video = pafy.new(link)
bestaudio = video.getbestaudio()
# print(bestaudio.url)
return bestaudio.url
def playQuick(num):
''' Make quick sound '''
l = ['up.mp3', 'down.mp3', 'preset_change.mp3', 'startup.mp3']
s = instance.media_new(os.path.join(os.getcwd(), l[num]))
player.set_media(s)
player.play()
if num == 3:
time.sleep(4)
else:
time.sleep(1)
player.stop()
def switchPresets(readyPresets):
#playQuick(2)
print("Ready to swap the preset. Loaded presets:")
i = 0
for link in readyPresets:
print(i, "-", link)
i += 1
newPreset = input("What would you like your preset to be?: ")
if newPreset.isdigit() and int(newPreset) < len(presetList):
# Number preset. We're goood
numPre = int(newPreset)
print("New Preset: ", numPre)
#playQuick(0)
return numPre
else:
# It's a character. Stop
print("Invalid preset. Skipping.")
playQuick(1)
return None
def playFile():
with open("url.txt") as file:
for line in file:
print("Contents: " + line)
playTrack(line[:-1])
def playTrack(track):
''' Play an audio track indefinetly. Also awaits response so it can detect a change in information '''
from readchar import readkey
# Load and add media file
media = instance.media_new(track)
player.set_media(media)
# Play
player.play()
# Pause before getting the status, to update everything
print(str(player.get_state()) + " ", end='\r')
sys.stdout.flush()
time.sleep(1)
print(str(player.get_state()) + " ")
if __name__ == '__main__':
presetList = loadPresets()
numPresets = len(presetList)
preset = getPresetTracks(presetList[currentPresetIndex])
# Start Up and initial setup
active = False
playQuick(0)
import readchar
keyInput = '\'0\''
# Start Main loop
print("Ready for input")
while keyInput not in inputs or keyInput not in special:
keyInput = repr(readchar.readkey())
# print("Got key: " + keyInput)
# Sanitize input
if keyInput in inputs:
sanitized = inputs.index(keyInput)
player.stop()
active = True
if sanitized < len(preset):
if isYouTubeAudio(preset[sanitized]):
playTrack(getYouTubeAudioTrack(preset[sanitized]))
else:
playTrack(preset[sanitized])
else:
playQuick(1)
# Special Characters
elif keyInput == '\'+\'':
currentPresetIndex += 1
if currentPresetIndex == numPresets:
currentPresetIndex = 0
preset = getPresetTracks(presetList[currentPresetIndex])
elif keyInput == '\'-\'':
currentPresetIndex -= 1
if currentPresetIndex == -1:
currentPresetIndex = numPresets - 1
preset = getPresetTracks(presetList[currentPresetIndex])
elif keyInput == '\'\\x7f\'': # Backspace
currentPresetIndex = 0
preset = getPresetTracks(presetList[currentPresetIndex])
elif keyInput == '\'\\r\'': # Return
active = False
player.stop()
elif keyInput == '\'\\x1b[H\'': # Home (reboot)
print("Home")
os.system('sudo reboot')
elif keyInput == '\'\\x1b[F\'': # End (exit app)
print("End")
exit()
elif keyInput == '\'\\x1b[5\'': # PageUp (ifup)
print("PageUp")
os.system('sudo ifup wlan0')
elif keyInput == '\'\\x1b[6\'': # PageDown (ifdown)
print("PageDown")
os.system('sudo ifdown wlan0')
elif keyInput == '\'\\x1b[2\'': # Ins (unused)
print("Ins")
elif keyInput == '\'\\x1b[3~\'': # Del
print("Del")
playFile()
elif keyInput == '\'x\'':
# End case
exit()