-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsap.py
465 lines (393 loc) · 14.2 KB
/
sap.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
# importing ui components
from textual.app import App, ComposeResult
from textual.screen import ModalScreen
from textual.containers import Container
from textual.binding import Binding
from textual.widgets import (
Header,
ProgressBar,
MarkdownViewer,
Markdown,
TabPane,
TabbedContent,
)
from textual.containers import Container, Vertical, Center
from textual.command import Hit, Hits, Provider
from textual import work
from textual.reactive import reactive
from textual.message import Message
# backend
from json import load
# importing stuff to play the music
from pygame import mixer
# miscellaneous imports (is tht the right spelling tho?)
from time import sleep
from os import listdir
from functools import partial
from pathlib import Path
volume = 1
q = []
p = []
is_paused = False
is_running = True
song = ""
mixer.init()
DOWNLOADS = """\
# Downloaded Songs
| No. | Name | Path |
|-----|------|------|
"""
songs = listdir("songs")
for no, song in enumerate(songs, start=1):
if song != "◌󠇯.txt":
DOWNLOADS += f"|{no}|{song}|{str(Path().absolute())+f'\\songs\\{song}.mp3'}|\n"
QUEUE = """
# Queued Songs
| No. | Name | Path |
|-----|------|------|
"""
PLAYLISTS = """
# Playlists
"""
help_text = """
# Help Screen
## Keybinds
| Keybind | Description |
|---------|-------------|
| Ctrl+\ | Open the command prompt |
| Q | Quit the app |
| D | Toggle Dark / Light mode |
| ↑ | Increase Volume |
| ↓ | Decrease Volume |
| ← | Rewind by 10 seconds |
| → | Fast forward by 10 seconds |
| > | Skip the current song and play the next song in the queue |
| < | Play the previous song in the queue |
| esc |Close this help screen |
## Commands
| Command | Description |
|---------|-------------|
| queue {song} | Adds {song} to the queue |
| play {song} | Plays {song}, but removes items in the queue, if any |
| pause | Pauses the current song |
| resume | Resumes the current song |
| help | Opens this help screen|
| playlist queue {playlist_name} | Queues the playlist |
| switch {tab} | Switches to the mentioned tab |
| clear queue | Clears the queue |
## About
Check out the [GitHub](https://github.com/Avn3s/textsap) for more information.
Gimme a ⭐ if you like it plz uwuwu <3 (@_@)<-Cute pleading face.
Enjoy.
"""
ROWS = [
("No.", "Song"),
]
for no, song in enumerate(listdir("songs"), start=1):
ROWS.append(tuple([no, song]))
def get_playlists():
with open("playlists.json", "r") as file:
playlists = load(file)
return playlists
def format_playlists_for_display():
playlists = get_playlists()
formatted_playlists = "# Playlists\n\n"
for playlist_name, songs in playlists.items():
formatted_playlists += f"**{playlist_name}**\n\n"
for song in songs:
formatted_playlists += f"- {song[:-4]}\n"
formatted_playlists += "\n"
return formatted_playlists
class ReactiveMarkdown(Markdown):
content = reactive("")
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.update_content()
def update_content(self) -> None:
self.update(self.content)
def watch_content(self, new_content: str) -> None:
self.update_content()
class songsProvider(Provider):
async def search(self, query: str) -> Hits:
matcher = self.matcher(query)
for song in listdir("songs")[:-1:]:
score = matcher.match(f"play {song}")
if score > 0:
yield Hit(
score,
matcher.highlight(query),
partial(self.app.play_song, song),
help=f"Plays {song[:-3:]}",
text="play {song}",
)
for song in listdir("songs")[:-1:]:
score = matcher.match(f"queue {song}")
if score > 0:
yield Hit(
score,
matcher.highlight(query),
partial(self.app.queue_song, song),
help=f"Queues {song[:-3:]}",
text="queue {song}",
)
help_score = matcher.match("help")
if help_score > 0:
yield Hit(
help_score,
matcher.highlight(query),
partial(self.app.action_help),
help="Shows help regarding commands and keybinds",
text="Shows help regarding commands and keybinds",
)
pause_score = matcher.match("pause")
if pause_score > 0:
yield Hit(
pause_score,
matcher.highlight(query),
partial(self.app.action_toggle_pause),
help="Pauses the current song.",
text="Pauses the current song.",
)
resume_score = matcher.match("resume")
if resume_score > 0:
yield Hit(
resume_score,
matcher.highlight(query),
partial(self.app.action_toggle_pause),
help="Resumes the current song.",
text="Resumes the current song.",
)
for tab in ["Downloads", "Queue", "Playlists"]:
score = matcher.match(f"switch {tab}")
if score > 0:
yield Hit(
score,
matcher.highlight(query),
partial(self.app.action_switch_tab, tab),
help=f"Switches to the {tab} tab.",
text=f"Switches to the {tab} tab.",
)
playlists = get_playlists()
for playlist in playlists:
score = matcher.match(f"playlist queue {playlist}")
if score > 0:
yield Hit(
score,
matcher.highlight(query),
partial(self.app.queue_playlist, playlist),
help=f"Queues all songs in the {playlist} playlist.",
text=f"Queues all songs in the {playlist} playlist.",
)
score = matcher.match("clear queue")
if score > 0:
yield Hit(
score,
matcher.highlight(query),
partial(self.app.action_clear_queue),
help="Clears the queue.",
text="Clears the queue.",
)
class HelpScreen(ModalScreen[None]):
BINDINGS = [Binding(key="escape", action="pop_screen")]
DEFAULT_CSS = """
HelpScreen{
align: center middle;
}
#help{
width: auto;
max-width: 70%;
height: auto;
max-height: 80%;
background: $panel;
align: center middle;
padding: 2 4;
border: solid yellow;
}
"""
def compose(self) -> ComposeResult:
global help_text
with Container(id="help"):
yield MarkdownViewer(markdown=help_text, show_table_of_contents=True)
class Sappy(App):
global volume, q, p, is_paused, song, length
App.COMMANDS = {songsProvider} | App.COMMANDS
BINDINGS = [
Binding(key="q", action="quit", description="Quit the app"),
Binding(key="d", action="toggle_dark", description="Toggle Dark mode"),
Binding(key="down", action="decrease_volume", description="Decrease volume"),
Binding(key="up", action="increase_volume", description="Increase volume"),
Binding(
key="↑".lower(), action="increase_volume", description="Increase volume"
),
Binding(
key="↓".lower(), action="decrease_volume", description="Decrease volume"
),
Binding(key="space", action="toggle_pause", description="Pause/Resume"),
Binding(key=">", action="next", description="Next song"),
Binding(key="<", action="prev", description="Previous song"),
Binding(key="h", action="help", description="Help"),
Binding(key="?", action="help", description="Help"),
Binding(key="left", action="rewind", description="rewind"),
Binding(key="right", action="forward", description="forward"),
]
@work(exclusive=True, thread=True)
async def songplay(self) -> None:
global isPaused, q, is_running, p, song, QUEUE
while True:
if not mixer.music.get_busy() and not is_paused:
if len(q) != 0:
try:
mixer.music.unload()
song = q.pop(0)
p.append(song)
mixer.music.load(song)
# mixer.music.set_volume(volume)
mixer.music.play()
self.notify(title="Now Playing", message=song[8:-4:])
except:
pass
if not is_running:
quit()
sleep(2)
_queue_content = reactive(QUEUE)
_playlists_content = reactive(PLAYLISTS)
@property
def queue_content(self):
return self._queue_content
@queue_content.setter
def queue_content(self, value):
self._queue_content = value
self.query_one("#queue_content", ReactiveMarkdown).content = value
@property
def playlists_content(self):
return self._playlists_content
@playlists_content.setter
def playlists_content(self, value):
self._playlists_content = value
self.query_one("#playlists_content", ReactiveMarkdown).content = value
def on_mount(self) -> None:
self.query_one("#volume").advance(100)
self.songplay()
self.update_playlists_display()
def action_quit(self) -> None:
global is_running
is_running = False
exit()
def update_playlists_display(self):
playlists_content = format_playlists_for_display()
self.query_one(
"#playlists_content", ReactiveMarkdown
).content = playlists_content
def play_song(self, song: str) -> None:
q.clear()
# mixer.music.set_volume(volume)
mixer.music.unload()
mixer.music.load("./songs/" + song)
mixer.music.play()
self.notify(title="Now Playing", message=song[:-4:])
def queue_song(self, song: str) -> None:
global q, QUEUE, volume
# mixer.music.set_volume(volume)
q.append("./songs/" + song)
self.notify(title="Added to queue", message=song[:-4:], severity="warning")
self.update_queue_display()
def update_queue_display(self) -> None:
global QUEUE, q
QUEUE = "# Queued Songs\n\n| No. | Name | Path |\n|-----|------|------|\n"
for index, song in enumerate(q, start=1):
song_name = song.split("/")[-1][:-4] # Remove path and file extension
QUEUE += f"| {index} | {song_name} | {song} |\n"
self.query_one("#queue_content", ReactiveMarkdown).content = QUEUE
def action_increase_volume(self) -> None:
self.query_one("#volume").advance(5)
volume = mixer.music.get_volume()
volume += 0.05
mixer.music.set_volume(volume)
def queue_playlist(self, playlist_name) -> None:
global QUEUE, q
playlists = get_playlists()
for song in playlists[playlist_name]:
q.append("./songs/" + song)
QUEUE = "# Queued Songs\n\n| No. | Name | Path |\n|-----|------|------|\n"
for index, song in enumerate(q, start=1):
song_name = song.split("/")[-1][:-4] # Remove path and file extension
QUEUE += f"| {index} | {song_name} | {song} |\n"
self.query_one("#queue_content", ReactiveMarkdown).content = QUEUE
self.notify(
title="Playlist added to queue", message=playlist_name, severity="warning"
)
def action_decrease_volume(self) -> None:
self.query_one("#volume").advance(-5)
volume = mixer.music.get_volume()
volume -= 0.05
mixer.music.set_volume(volume)
def action_switch_tab(self, tab: str) -> None:
global QUEUE, q
self.query_one(TabbedContent).active = tab.lower()
for no, song in enumerate(q, start=1):
QUEUE += f"|{no}|{song}|bob|\n"
def action_toggle_pause(self) -> None:
global is_paused
if is_paused:
mixer.music.unpause()
else:
mixer.music.pause()
is_paused = not is_paused
def action_rewind(self) -> None:
position = mixer.music.get_pos()
mixer.music.set_pos((position - 5000) / 1000)
del position
def action_forward(self) -> None:
position = mixer.music.get_pos()
mixer.music.set_pos((position + 5000) / 1000)
del position
def action_next(self) -> None:
mixer.music.stop()
mixer.music.unload()
def action_prev(self) -> None:
global q, p
try:
q.insert(0, p.pop())
q.insert(0, p.pop())
mixer.music.stop()
mixer.music.unload()
except:
self.notify(
title="No previous songs",
message="There are no previous songs in the queue",
severity="error",
)
def action_help(self) -> None:
self.push_screen(HelpScreen())
def action_clear_queue(self) -> None:
global q, QUEUE
q.clear()
mixer.music.stop()
mixer.music.unload()
self.update_queue_display()
self.notify(
title="Queue Cleared",
message="All songs have been removed from the queue",
severity="information",
)
def action_toggle_dark(self) -> None:
self.dark = not self.dark
def compose(self) -> ComposeResult:
with Vertical():
self.title = "Sap.py"
self.sub_title = "~Avn3s"
yield Header(show_clock=True)
with TabbedContent(initial="downloads"):
with TabPane("Downloads", id="downloads"):
yield Markdown(DOWNLOADS)
with TabPane("Queue", id="queue"):
yield ReactiveMarkdown(QUEUE, id="queue_content")
with TabPane("Playlists", id="playlists"):
yield ReactiveMarkdown(PLAYLISTS, id="playlists_content")
yield MarkdownViewer(
DOWNLOADS, show_table_of_contents=False, classes="songlist"
)
with Center():
yield ProgressBar(total=100, show_eta=False, id="volume")
app = Sappy()
app.run()