-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtermify.py
166 lines (133 loc) · 4.88 KB
/
termify.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
"""
Termify main module
"""
from curses import endwin, initscr, newwin, noecho, textpad
# pylint: disable=W0611
from readline import insert_text
from pynput import keyboard
from sys import exit
from player import Player
class Termify:
"""
Class Termify contains all the GUI creation/management methods
"""
player = Player()
main_screen = initscr()
exit_termify = False
def main(self):
"""
The main method render the correct screen login/player
and the keyboard listener
"""
last_token_information = self.player.get_last_token_used()
logged = self.player.is_logged(last_token_information)
noecho()
self.header(self.main_screen)
if not logged:
self.login_screen()
else:
self.player.login(last_token_information['access_token'])
self.footer(self.main_screen)
self.main_screen.refresh()
listener = keyboard.Listener(on_press=self.controls)
listener.start()
# if not self.exited:
self.player_screen()
def login_screen(self):
"""
This method will render the screen used that will ask the user username/id
and then login if everything asked is correct
"""
login_title = "Please login using your Spotify username/id:\n"
login_screen = newwin(0, 0)
login_screen.box()
v_dim, h_dim = login_screen.getmaxyx()
login_screen.addstr(round(v_dim / 2), round((h_dim - len(login_title)) / 2), login_title)
username_input = login_screen.subwin(1, 44, round(v_dim / 2 + 1), round((h_dim - 44) / 2))
username_text_input = textpad.Textbox(username_input)
username_input.refresh()
login_screen.addstr(v_dim - 1, h_dim - 20, "[Enter]Submit")
login_screen.refresh()
username = username_text_input.edit()
if str(username) != '':
oauth_win = newwin(0, 0)
oauth_win.box()
oauth_win.addstr(v_dim - 1, h_dim - 20, "[Enter]Submit")
oauth_win.refresh()
self.player.get_new_token(username)
endwin()
def player_screen(self):
"""
This method will render the main player screen GUI
with the current track playing and the associated timeline
"""
while True:
player = self.player.current_play()
current_track = next(player)
v_dim, h_dim = self.main_screen.getmaxyx()
self.main_screen.addstr(round(v_dim / 2), 1, ' ' * (h_dim - 2))
self.main_screen.addstr(
round(v_dim / 2),
round((h_dim - len(current_track)) / 2),
current_track
)
self.main_screen.refresh()
for player_state in player:
self.main_screen.addstr(
round(v_dim / 2) + 1,
round((h_dim - (len(player_state))) / 2),
player_state
)
self.main_screen.refresh()
if not self.player.is_playing:
break
if self.player.is_track_state_changed:
self.player.is_track_state_changed = False
break
if self.exit_termify:
endwin()
exit(0)
def controls(self, key):
"""
This method provide Termify player controls handler
113 is the virtual keycode of the "q" key
269025044, 269025046, 269025047 are the virtual keycodes
of media player (previous, start/stop and next) keys
:param key: The key pressed by the user, sent by the listener created in the main method
"""
try:
pressed_key = key.vk
except AttributeError:
pressed_key = None
if pressed_key == 113:
self.exit_termify = True
elif pressed_key == 269025047:
self.player.next_track()
elif pressed_key == 269025046:
self.player.previous_track()
elif pressed_key == 269025044:
self.player.start_stop_streaming()
@staticmethod
def header(screen):
"""
Add the same header on every screen you pass
:param screen: The screen where the header will be added
"""
screen.box()
titles = ["Welcome on Termify", "Manage your Spotify streaming directly from your terminal"]
_, h_dim = screen.getmaxyx()
for index, title in enumerate(titles):
screen.addstr(index + 1, round((h_dim - len(title)) / 2), title)
@staticmethod
def footer(screen):
"""
Add same footer on every screen you pass
:param screen: The screen where the footer will be added
"""
v_dim, _ = screen.getmaxyx()
screen.addstr(
v_dim - 1,
1,
"(q) quit"
)
Termify().main()