-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathuser_function.py
More file actions
192 lines (152 loc) · 6.18 KB
/
user_function.py
File metadata and controls
192 lines (152 loc) · 6.18 KB
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
import sqlite3
from typing import List
from ui_design.ui_user import *
from sql_cmd.sql_user import *
from user_song import Song
class UserMode():
def __init__(self, uid, conn: sqlite3.Connection, cursor: sqlite3.Cursor) -> None:
self.uid = uid
self.current_session = None
self.conn = conn
self.cur = cursor
def start_session(self) -> bool:
if self.current_session is not None:
print("Start session failed. A session is currectly running.")
return False
self.cur.execute(SQL_USER_START_SESSION_GET_ALL_SESSIONS, (self.uid,))
res = self.cur.fetchall()
if res[0][0] is None:
self.current_session = 1
else:
self.current_session = res[0][0] + 1
self.cur.execute(SQL_USER_START_SESSION_SUCCESS,
(self.uid, self.current_session))
self.conn.commit()
return True
def search_for_artists(self) -> None:
# get all keywords
while True:
keywords = input(
"Please enter your keywords, separated by spaces: ")
keywords = set(keywords.split(" "))
keywords.discard("")
if len(keywords) == 0:
print("Keyword field cannot be empty.")
continue
keywords = list(keywords)
break
# get all matched artists, order by the number of matched keywords
sql_artists, kw_input = get_sql_search_artists(keywords)
self.cur.execute(sql_artists, kw_input)
res = self.cur.fetchall()
# get user selection
user_select = search_artists_display(
("name", "nationality", "# of songs"), res, self.cur)
if user_select is None:
return
# expand artists to songs
self.cur.execute(SQL_USER_EXPAND_ARTIST, (user_select[0],))
song_res = self.cur.fetchall()
song_dict = {}
# print out the songs
print("All songs performed by {} (id | title | duration):".format(
user_select[1]))
for i in range(len(song_res)):
song_dict[str(i)] = song_res[i]
(sid, title, duration) = song_res[i]
print("-%2d. %10d | %50s | %10d" % (i, sid, title, duration))
# let user select one
while True:
selection = input("Choose a song or press ENTER to cancel: ")
if selection == "":
return
elif selection not in song_dict:
print("Please make a valid selection.")
else:
s_song = song_dict[selection]
user_select = ('songs', s_song[0], s_song[1], s_song[2], 0)
break
# start the song action
song_action = Song(
user_select[1], user_select[2], user_select[3], self)
song_action.select_song()
return
def search_for_songs_playlists(self) -> None:
# get all keywords
while True:
keywords = input(
"Please enter your keywords, separated by spaces: ")
keywords = set(keywords.split(" "))
keywords.discard("")
if len(keywords) == 0:
print("Keyword field cannot be empty.")
continue
keywords = list(keywords)
break
# get all matched songs & playlist, order by the number of matched keywords
sql_songs, kw_input = get_sql_search_songs_playlists(keywords)
self.cur.execute(sql_songs, kw_input)
res = self.cur.fetchall()
# get user selection
user_select = search_songs_playlists_display(
("type", "id", "title", "duration"), res)
if user_select is None:
return
# expand playlists to songs if user select a playlist
if user_select[0] == "playlist":
self.cur.execute(SQL_USER_EXPAND_PLAYLIST, (user_select[1],))
song_res = self.cur.fetchall()
song_dict = {}
# print out the songs
print("All songs in playlist {} (id | title | duration):".format(
user_select[2]))
for i in range(len(song_res)):
song_dict[str(i)] = song_res[i]
(sid, title, duration) = song_res[i]
print("-%2d. %10d | %50s | %10d" % (i, sid, title, duration))
# let user select one
while True:
selection = input("Choose a song or press ENTER to cancel: ")
if selection == "":
return
elif selection not in song_dict:
print("Please make a valid selection.")
else:
s_song = song_dict[selection]
user_select = ('songs', s_song[0], s_song[1], s_song[2], 0)
break
# start the song action
song_action = Song(
user_select[1], user_select[2], user_select[3], self)
song_action.select_song()
return
def end_session(self) -> bool:
if self.current_session is None:
return False
self.cur.execute(SQL_USER_END_SESSION_SUCCESS,
(self.uid, self.current_session))
self.conn.commit()
self.current_session = None
return True
def start_user(self) -> None:
user_action = {
"1": self.start_session,
"2": self.search_for_songs_playlists,
"3": self.search_for_artists,
"4": self.end_session,
"5": self.end_session,
"6": self.end_session
}
# select an action to proceed
while True:
print(UI_USER_MAIN)
action = input("Select an action to proceed: ")
if action not in user_action:
print("Please make a proper selection.")
continue
# run that action
user_action[action]()
if action == "5":
return
if action == "6":
exit()