-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflickpick.py
319 lines (254 loc) · 6.73 KB
/
flickpick.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
from collections import OrderedDict
from prettytable import PrettyTable
import datetime
import os
import random
import sys
import time
from peewee import *
db = SqliteDatabase('flicklist.db')
class Flick(Model):
owner = CharField()
rank = IntegerField()
title = CharField()
genre = CharField()
avail = CharField()
watched = BooleanField(default=False)
date_added = DateField(default=datetime.datetime.now)
date_watched = DateField(null=True, default=None)
class Meta:
database = db
# === SOME FUNCTIONS ===
def initialize():
"""Create the database and the table if they don't exist"""
db.connect()
db.create_tables([Flick], safe=True)
def clear():
os.system('cls' if os.name == 'nt' else 'clear')
#def menu_loop():
def main_menu_loop():
"""Show the menu"""
choice = None
while choice not in ('q', 'quit', 'exit'):
list_all()
print("\n === main menu ===")
for key, value in menu.items():
print(" {}) {}".format(key, value.__doc__) if (len(key) == 1) else
" {}) {}".format(key, value.__doc__))
print(" q) Quit")
choice = input("\nChoice: ").lower().strip()
if choice in menu:
menu[choice]()
def pretty_table(q, owner):
"""Creates pretty table"""
rows = []
for __ in q:
rows.append([__.rank, __.title, __.genre, __.avail])
print("\n| {}'s List |".format(owner))
col_names = ['rank', 'title', 'genre', 'avail']
x = PrettyTable(col_names)
x.align[col_names[1]] = 'l'
x.align[col_names[2]] = 'r'
x.padding_width = 1
for row in rows:
x.add_row(row)
print(x)
# === MENU OPTIONS ===
def add_flick():
"""Add a flick"""
while True:
inp = input("Whose flick is this? ").title()
if inp in ('Iota', 'Phi'):
owner_ = inp
break
else:
print("I don't know that person!")
title_ = input("Title: ")
genre_ = input("Genre: ")
if input("Is this flick readily available? (y/n)\n>: ").lower() == 'y':
avail_ = input("Source (Netflix, file, etc): ")
else:
avail_ = '-'
list_all()
rank_ = input("Rank: ")
if input("Are you sure you want to add \"{}\"? (Y/n): "
.format(title_)).lower() != 'n':
if (Flick.select()
.where((Flick.owner == owner_) & (Flick.rank == rank_))
.count() > 0):
q = (Flick.update(rank=Flick.rank + 1)
.where((Flick.owner == owner_) & (Flick.rank >= rank_)))
q.execute()
Flick.create(owner=owner_,
title=title_,
genre=genre_,
rank=rank_,
avail=avail_)
list_all()
def list_all():
"""List all"""
def query(owner):
return ((Flick.select()
.where(Flick.owner == owner)
.order_by(Flick.rank)), owner)
clear()
pretty_table(*query("Phi"))
pretty_table(*query("Iota"))
def del_flick():
"""Delete a flick"""
list_all()
inp = input("Enter title of flick to delete: ")
d = Flick.get(Flick.title == inp)
if input("Are you sure you want to DELETE \"{}\"? (y/N): "
.format(d.title)).lower() == 'y':
q = (Flick.update(rank=Flick.rank - 1)
.where(Flick.rank >= d.rank))
q.execute()
d.delete_instance()
def edit_menu_loop():
"""Edit flick"""
def edit_apply(row, field):
cur_value = getattr(row, field)
print("Current {}: {}".format(field, cur_value))
new_value = input("New {}: ".format(field))
if field == 'rank':
new_value = int(new_value)
if new_value > cur_value:
q = (Flick.update(rank=Flick.rank - 1)
.where((Flick.owner == row.owner) &
(Flick.rank > cur_value) &
(Flick.rank <= new_value)))
q.execute()
elif new_value < cur_value:
q = (Flick.update(rank=Flick.rank + 1)
.where((Flick.owner == row.owner) &
(Flick.rank < cur_value) &
(Flick.rank >= new_value)))
q.execute()
setattr(row, field, new_value)
row.save()
list_all()
inp = input("Enter title of flick to edit: ")
row = Flick.get(Flick.title == inp)
choice = None
while choice not in ('x', 'exit'):
list_all()
print("\n === edit menu === {} ===".format(row.title))
for key, value in edit_menu.items():
print(" {}) {}".format(key, value))
print(" x) Exit to main menu")
choice = input('\nChoice: ').lower().strip()
if choice in edit_menu:
edit_apply(row, edit_menu[choice])
def list_selection():
"""List selection"""
global tq
global gq
global vq
if selections['t'] == "*":
tq = (Flick.rank != 666)
else:
tq = (Flick.rank <= selections['t'])
if selections['g'] == "*":
gq = (Flick.genre != "godisuck")
else:
gq = (Flick.genre == selections['g'])
if selections['v'] == False:
vq = (Flick.avail != "isthisreallythebestwaytodothis?")
else:
vq = (Flick.avail != "-")
def query(owner):
return ((Flick.select()
.where((Flick.owner == owner) &
gq & tq & vq)
.order_by(Flick.rank)), owner)
clear()
pretty_table(*query("Phi"))
pretty_table(*query("Iota"))
def flickpick_menu_loop():
"""Flickpick Menu"""
choice = None
while choice not in ('x', 'exit'):
list_selection()
print("\n === flickpick menu ===")
for key, value in flickpick_menu.items():
print(" {}) {} ({})".format(key, value.__doc__, selections[key]))
print(" ------------------")
print(" p) Pick a Flick!")
print(" ------------------")
print(" c) Clear all selections")
print(" x) Exit to main menu")
choice = input('\nChoice: ').lower().strip()
if choice in flickpick_menu:
flickpick_menu[choice]()
elif choice == "c":
selections['t'] = "*"
selections['g'] = "*"
selections['v'] = False
elif choice == "p":
flickpick_go()
def choose_genre():
"""Choose genre"""
list_selection()
selections['g'] = input("Pick genre ('*' for all): ")
def choose_top():
"""Choose top #"""
list_selection()
selections['t'] = input("Pick top # ('*' for all): ")
def toggle_avail():
"""Available"""
if selections['v'] == False:
selections['v'] = True
else:
selections['v'] = False
def test_func():
"""Test function"""
pass
def flickpick_go():
"""Pick a Flick!"""
pick = ((Flick.select()
.where(gq & tq & vq)
.order_by(fn.Random()).limit(1))
.get())
clear()
print("\n")
for c in "=========Your pick is=========":
sys.stdout.write( '%s' % c )
sys.stdout.flush()
time.sleep(0.05)
print("\n\n" + pick.title.upper().center(30," "))
print("\n==============================")
input(">")
# === LISTS ===
owners = 'pass'
selections = {
't': "*",
'g': "*",
'v': False
}
# === MENUS ===
menu = OrderedDict([
('a', add_flick),
('d', del_flick),
('e', edit_menu_loop),
# ('la', list_all),
('p', flickpick_menu_loop),
# ('lw', list_watched),
# ('test', test_func),
])
edit_menu = OrderedDict([
('r', 'rank'),
('t', 'title'),
('g', 'genre'),
('a', 'avail'),
# ('w', 'watched')
])
flickpick_menu = OrderedDict([
('t', choose_top),
('g', choose_genre),
('v', toggle_avail),
])
# === MAIN ===
if __name__ == '__main__':
initialize()
main_menu_loop()