This repository was archived by the owner on Oct 5, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsystem.py
10079 lines (9529 loc) · 321 KB
/
system.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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
homedir = '/home/pi/hasystem/'
import os
import getpass
import time
import calendar
import sys
import requests
import sqlite3
import urllib3
import subprocess
import platform
try:
import enchant
except Exception:
print ("Warning: pyenchant no installed or has a path problem. Spell checking will not work.\n")
from os import listdir
from os.path import isfile, join
from random import randint, shuffle
#top
user = getpass.getuser()
http = urllib3.PoolManager()
global file
global show
global play
global pcmd
global pcount
global ttlchk
global commandcheck
global xlib
global PRINTMODE
commandcheck = "no"
ttlchk = ""
#SLEEPTIME = 20
try:
input = raw_input
except NameError:
pass
MYDB = homedir + "myplex.db"
sql = sqlite3.connect(MYDB)
cur = sql.cursor()
cur.execute("SELECT State FROM States WHERE Option LIKE \"SLEEPTIME\"")
if not cur.fetchone():
cur.execute("INSERT INTO States VALUES (?,?)",("SLEEPTIME","20"))
sql.commit()
cur.execute("SELECT State FROM States WHERE Option LIKE\"SLEEPTIME\"")
else:
cur.execute("SELECT State FROM States WHERE Option LIKE\"SLEEPTIME\"")
SLEEPTIME = int(cur.fetchone()[0])
global PLEXSVR
global PLEXCLIENT
global plex
global client
def plexlogin():
global PLEXSVR
global PLEXCLIENT
global plex
global client
global LOGGEDIN
try:
cur.execute('SELECT setting FROM settings WHERE item LIKE \'PLEXSVR\'')
PLEXSVR = cur.fetchone()
PLEXSVR = PLEXSVR[0]
cur.execute('SELECT setting FROM settings WHERE item LIKE \'PLEXCLIENT\'')
PLEXCLIENT = cur.fetchone()
PLEXCLIENT = PLEXCLIENT[0]
try:
cur.execute('SELECT setting FROM settings WHERE item LIKE \'PLEXSERVERIP\'')
PLEXSERVERIP = cur.fetchone()
PLEXSERVERIP = PLEXSERVERIP[0]
cur.execute('SELECT setting FROM settings WHERE item LIKE \'PLEXSERVERPORT\'')
PLEXSERVERPORT = cur.fetchone()
PLEXSERVERPORT = PLEXSERVERPORT[0]
except Exception:
print ("Local Variables not set. Run setup to use local access.")
try:
LOGGEDIN
except Exception:
try:
from plexapi.server import PlexServer
from plexapi.myplex import MyPlexUser
baseurl = 'http://' + PLEXSERVERIP + ':' + PLEXSERVERPORT
plex = PlexServer(baseurl)
except IndexError:
from plexapi.myplex import MyPlexAccount
print ("Local Fail. Trying cloud access.")
cur.execute('SELECT setting FROM settings WHERE item LIKE \'PLEXUN\'')
PLEXUN = cur.fetchone()
PLEXUN = PLEXUN[0]
try:
cur.execute('SELECT setting FROM settings WHERE item LIKE \'PLEXPW\'')
PLEXPW = cur.fetchone()
PLEXPW = PLEXPW[0]
import base64
PLEXPW = str(base64.b64decode(PLEXPW))
except Exception:
print ("Your Plex Password is temporarly needed to proceed:\n")
PLEXPW = str(getpass.getpass("Password: "))
user = MyPlexAccount.signin(PLEXUN,PLEXPW)
print ("\rSuccessfully logged into Plex cloud.\n")
plex = user.resource(PLEXSVR).connect()
if ("changeclient" not in sys.argv):
try:
client = plex.client(PLEXCLIENT)
except Exception:
print ("Retrying Client Get.\n")
time.sleep(1)
client = plex.client(PLEXCLIENT)
LOGGEDIN = "YES"
except IndexError:
print ("Error getting necessary plex api variables. Run system_setup.py.")
def setsleeptime(num):
num = str(num)
cur.execute("DELETE FROM States WHERE Option LIKE \"SLEEPTIME\"")
sql.commit()
cur.execute("INSERT INTO States VALUES (?,?)",("SLEEPTIME",num))
sql.commit()
return ("SLEEPTIME setting has been adjusted to: " + num + ".\n")
def gethonorific(): #Returns the value of the name the script will refer to the user by in certain outputs.
command = "SELECT State FROM States WHERE Option LIKE \"CALLMETHIS\""
cur.execute(command)
if not cur.fetchone():
CALLMETHIS = "Sir"
cur.execute("INSERT INTO States VALUES (?,?)",("CALLMETHIS",CALLMETHIS))
sql.commit()
cur.execute(command)
CALLMETHIS = cur.fetchone()[0]
return (CALLMETHIS)
def sethonorific(myname): #Sets the name the script will refer to the user by. Defaults to "Sir"
if myname == "none":
print ("What shall I call thee?")
CALLMETHIS = str(raw_input('TItle: ')).strip()
else:
CALLMETHIS = myname.strip()
cur.execute("DELETE FROM States WHERE Option LIKE \"CALLMETHIS\"")
sql.commit()
cur.execute("INSERT INTO States VALUES (?,?)",("CALLMETHIS",CALLMETHIS))
sql.commit()
return ("I shall now call thee " + CALLMETHIS + ". Good Day.")
def changeplexip(): #Changes the stored IP address for the Plex Server
plexip = str(raw_input('Plex Server IP: '))
plexip = plexip.strip()
plexport = str(raw_input('Plex Port: '))
plexport = plexport.strip()
cur.execute("DELETE FROM settings WHERE item LIKE \'PLEXSERVERIP\'")
sql.commit()
cur.execute("DELETE FROM settings WHERE item LIKE \'PLEXSERVERPORT\'")
sql.commit()
cur.execute("INSERT INTO settings VALUES (?,?)",("PLEXSERVERIP",plexip))
sql.commit()
cur.execute("INSERT INTO settings VALUES (?,?)",("PLEXSERVERPORT",plexport))
sql.commit()
def changeplexpw(password): #Changes the stored password for Plex. Obsolete as storing passwords is no longer needed
password = password.strip()
cur.execute("DELETE FROM settings WHERE item LIKE \'PLEXPW\'")
sql.commit()
password = str(password.encode('base64','strict'))
cur.execute("INSERT INTO settings VALUES(?,?)",('PLEXPW',password))
sql.commit()
return ("The Plex PW has been changed.")
def cls():
os.system('cls' if os.name=='nt' else 'clear')
def checkcustomtables(show):
command = "SELECT name FROM sqlite_master WHERE type='table'"
cur.execute(command)
list = cur.fetchall()
for item in list:
if ("CUSTOM_" in item[0]):
command = "SELECT name, type FROM " + item[0] + " WHERE name LIKE \"" + show + "\""
cur.execute(command)
if not cur.fetchone():
pass
else:
cur.execute(command)
found = cur.fetchone()
type = found[1]
found = found[0]
return ("CUSTOM."+found, type)
return show
def checkprecomm(show):
oshow = show
if ((show == "playcommercial") or (show == "preroll")):
return show
show = show.replace("preroll.","")
show = show.replace("playcommercial.","")
com1 = "SELECT name FROM prerolls WHERE name LIKE \"" + show + "\""
com2 = "SELECT name FROM commercials WHERE name LIKE \"" + show + "\""
try:
cur.execute(com1)
if not cur.fetchone():
pass
else:
cur.execute(com1)
show = cur.fetchone()[0]
show = "preroll." + show
return show
cur.execute(com2)
if not cur.fetchone():
pass
else:
cur.execute(com2)
show = cur.fetchone()[0]
show = "playcommercial." + show
return show
return oshow
except sqlite3.OperationalError:
return oshow
def getcustomtable(table):
item = "CUSTOM_" + table.strip()
command = "SELECT name FROM "+ item
try:
cur.execute(command)
except Exception:
try:
plexlogin()
ssec = plex.library.sections()
for lib in ssec:
if lib.title ==table:
stuff = lib.search("")
found = []
for item in stuff:
found.append(item.title)
return found
except Exception:
return ("Error: " + table + " not found.")
stuff = []
if not cur.fetchall():
return ("Error: " + table + " not found.")
else:
cur.execute(command)
found = cur.fetchall()
for item in found:
stuff.append(item[0])
return stuff
def listcustomtables(): #Displays a list of custom Plex libraries imported into the controller database
command = "SELECT name FROM sqlite_master WHERE type='table'"
cur.execute(command)
list = cur.fetchall()
stuff = []
for item in list:
if ("CUSTOM_" in item[0]):
stuff.append(item[0])
return stuff
def muteaudio(): #Mutes the audio
global client
plexlogin()
client.setVolume(0, 'Video')
def mutemusic(): #Mutes audio output to the music client
plexlogin()
PLEXMUSICCLIENT = musiccheck()
client = plex.client(PLEXMUSICCLIENT)
client.setVolume(0, 'music')
def unmutemusic(): #Unmutes audio output to the music client
plexlogin()
PLEXMUSICCLIENT = musiccheck()
client = plex.client(PLEXMUSICCLIENT)
client.setVolume(100, 'music')
def unmuteaudio(): #Unmutes audio
global client
plexlogin()
client.setVolume(100, 'Video')
def lowaudio(): #Sets volume to 25%
global client
plexlogin()
client.setVolume(25, 'Video')
def mediumaudio(): #Sets volume to 50%
global client
plexlogin()
client.setVolume(50, 'Video')
def highaudio(): #Sets volume to 75%
global client
plexlogin()
client.setVolume(75, 'Video')
def maxaudio(): #Sets audio to 100%
global client
plexlogin()
client.setVolume(100, 'Video')
def schedchecker(command):
global commandcheck
command = command.replace("system.py schedchecker ","")
cmd = "SELECT name FROM help"
cur.execute(cmd)
xcmds = cur.fetchall()
cmds = []
for item in xcmds:
cmds.append(item[0])
ccheck = "no"
for itm in cmds:
if itm in command:
#print ("Pass: Commands")
commandcheck = "yes"
return command
if "no" in ccheck:
say = titlecheck(command)
if ("ERROR:" in say):
return ("ERROR: Command Fail. " + command + " does not appear to be valid. Check and try again.")
else:
#print ("Pass: Media")
return ("\\\"" + say + "\\\"")
def addschedule(action, time, day): #Adds an item to the schedule. These must be done one at a time, to add in bulk, use a shell script
action = action.strip()
time = time.strip()
day = day.strip()
global commandcheck
achk = schedchecker(action)
if "ERROR:" in achk:
return achk
try:
cur.execute("SELECT * FROM SCHEDULES")
except sqlite3.OperationalError:
cur.execute("CREATE TABLE IF NOT EXISTS SCHEDULES(action TEXT, time TEXT, day TEXT)")
sql.commit()
command = "SELECT * FROM SCHEDULES WHERE time LIKE \"" + time + "\" AND day LIKE \"" + day + "\""
cur.execute(command)
if not cur.fetchall():
addme = []
addme.append(action)
else:
cur.execute(command)
addme = cur.fetchall()[0][0]
addme = addme.split(";")
addme.append(action)
cur.execute("DELETE FROM SCHEDULES WHERE time LIKE \"" + time + "\" AND day LIKE \"" + day + "\"")
sql.commit()
for item in addme:
if item == "":
pass
else:
try:
adme = adme + item + ";"
except NameError:
if ((len(addme) == 1) and ("yes" not in commandcheck)):
item = "\"" + item + "\""
adme = item + ";"
cur.execute("INSERT INTO SCHEDULES VALUES(?,?,?)",(adme, time, day))
sql.commit()
return ("Successfully added item to schedule.")
def removeschedule(time, day): #Removes an item from the schedule
command = "SELECT * FROM SCHEDULES WHERE time LIKE \"" + time + "\" AND day LIKE \"" + day + "\""
cur.execute(command)
if not cur.fetchall():
return("Error: no schedules found for " + time + " on " + day + " found.")
cur.execute(command)
fnd = cur.fetchone()
print ("Found:\n" + fnd[0] + "\n" + fnd[1] + "\n" + fnd[2] + "\n\nRemoving Now.\n")
cur.execute("DELETE FROM SCHEDULES WHERE time LIKE \"" + time + "\" AND day LIKE \"" + day + "\"")
sql.commit()
return ("\nSuccessfully removed schedule for " + time + " on " + day + ".")
def argprocessor(thearg):
try:
xshow = show
except Exception:
sch = []
for thing in sys.argv:
if "system.py" in thing:
pass
elif thearg in thing:
pass
else:
achk = schedchecker(thing)
if "ERROR" not in achk:
return achk
#print achk
sch.append(thing)
if len(sch) == 1:
xshow = sch[0]
else:
print ("\nARGUMENT ERROR: TAKING A BEST GUESS!!\n")
xshow = sch[0]
return xshow
for thing in sys.argv:
if "system.py" in thing:
pass
elif thearg in thing:
pass
elif xshow in thing:
pass
else:
return thing
def viewschedules(): #Outputs the entire schedule list. Currently has pagination and will output the entire schedule at once.
try:
cur.execute("SELECT * FROM SCHEDULES")
except sqlite3.OperationalError:
cur.execute("CREATE TABLE IF NOT EXISTS SCHEDULES(action TEXT, time TEXT, day TEXT)")
sql.commit()
if (("-d" not in sys.argv) and ("-t" not in sys.argv) and ("-a" not in sys.argv)):
command = "SELECT * FROM SCHEDULES"
cur.execute(command)
if not cur.fetchall():
return ("Nothing is currently scheduled.")
cur.execute(command)
sched = cur.fetchall()
for item in sched:
print ("Action: " + item[0])
print ("Time: " + item[1])
print ("Day: " + item[2] + "\n")
elif ("-t" in sys.argv):
if len(sys.argv) == 3:
command = "SELECT time FROM SCHEDULES"
cur.execute(command)
list = cur.fetchall()
flist = []
for item in list:
itm = item[0]
if itm not in flist:
flist.append(itm)
if len(flist) > 0:
print ("The Following Times have items scheduled:\n")
for item in flist:
print (item)
else:
argv = argprocessor("-t")
argv = timechecker(argv)
command = "SELECT * FROM SCHEDULES WHERE time LIKE \"" + argv + "\""
cur.execute(command)
if not cur.fetchall():
return ("Error: No scheduled items found for time: " + argv)
cur.execute(command)
flist = cur.fetchall()
print ("The following items were found for time " + argv + ":\n")
for item in flist:
print ("Action: " + item[0])
print ("Day: " + item[2] + "\n")
elif ("-a" in sys.argv):
if len(sys.argv) == 3:
command = "SELECT action FROM SCHEDULES"
cur.execute(command)
list = cur.fetchall()
flist = []
for item in list:
itm = item[0]
if itm not in flist:
flist.append(itm)
if len(flist) > 0:
print ("The Following Actions are scheduled:\n")
for item in flist:
print (item)
else:
argv = argprocessor("-a")
command = "SELECT * FROM SCHEDULES WHERE action LIKE \"%" + argv + "%\""
cur.execute(command)
if not cur.fetchall():
return ("Error: No scheduled items found for action: " + argv)
cur.execute(command)
flist = cur.fetchall()
print ("The following items were found for action " + argv + ":\n")
for item in flist:
print ("Action: " + item[0])
print ("Time: " + item[1])
print ("Day: " + item[2] + "\n")
else:
if len(sys.argv) == 3:
command = "SELECT day FROM SCHEDULES"
cur.execute(command)
list = cur.fetchall()
flist = []
for item in list:
itm = item[0]
if itm not in flist:
flist.append(itm)
if len(flist) > 0:
print ("The Following Days have items scheduled:\n")
for item in flist:
print (item)
else:
argv = argprocessor("-d")
command = "SELECT * FROM SCHEDULES WHERE day LIKE \"" + argv + "\""
cur.execute(command)
if not cur.fetchall():
return ("Error: No scheduled items found for day: " + argv)
cur.execute(command)
flist = cur.fetchall()
print ("The following items were found for day " + argv + ":\n")
for item in flist:
print ("Action: " + item[0])
print ("Time: " + item[1] + "\n")
return ("\nDone.")
def clearschedules(): #Removes all scheduled items
cur.execute("DELETE FROM SCHEDULES")
sql.commit()
return ("Done.")
def holidaycheck(title):
title=title.lower().strip()
cur.execute("SELECT * FROM Holidays WHERE name LIKE \"" + title + "\"")
hcheck = cur.fetchone()
if not hcheck:
return ("Error: " + title + " does not exist as a holiday.")
return title
def checkholidays(holiday): #Returns custom holidays
try:
command = "SELECT * FROM Holidays"
cur.execute(command)
except sqlite3.OperationalError:
cur.execute("CREATE TABLE IF NOT EXISTS Holidays(name TEXT, items TEXT)")
sql.commit()
cur.execute(command)
if not cur.fetchall():
return ("Error: No Holidays are currently saved.")
else:
if holiday == "none":
command = "SELECT * FROM Holidays"
else:
command = "SELECT * FROM Holidays WHERE name LIKE \"" + holiday + "\""
cur.execute(command)
test = cur.fetchall()
if not test:
return ("Error: " + holiday + " not found in Holidays Table. Check and try again.")
for thing in test:
name = thing[0]
titles = thing[1]
titles = titles.split(";")
if ("none" in holiday):
print (name)
else:
print (name + ":")
for ttl in titles:
if ttl == "":
pass
else:
ttl = ttl.replace("movie.","the movie ")
print (ttl)
print ("---")
return ("\nDone.")
def removeholiday(holiday): #Delete custom holiday
print ("Warning: This will remove the " + holiday + " and all associations. Are you sure you want to proceed?")
validate = str(raw_input("Yes or No:"))
if "yes" not in validate.lower():
return ("Error: You must type yes to remove the holiday.")
name = holiday.lower()
cur.execute("SELECT FROM Holidays WHERE name LIKE \"" + name + "\"")
if not cur.fetchone():
return ("Error: " + holiday + " not found to remove.")
cur.execute("DELETE FROM Holidays WHERE name LIKE \"" + name + "\"")
sql.commit()
return ("Successfully removed " + holiday + ".")
def removefromholiday(holiday, title): #Removes a holiday designation from a title
holiday = holiday.lower()
if ":" not in title:
if ("Quit." in title):
return ("User Quit. No action taken.")
elif ("Error" in title):
return title
else:
title = title.split(":")
ssn = title[1].strip()
epn = title[2].strip()
title = title[0].strip()
title = titlecheck(title.strip())
shows = plex.library.section('TV Shows')
the_show = shows.get(title).episodes()
for ep in the_show:
if ((ep.seasonNumber == ssn) and (ep.index == epn)):
tcheck = ep.title
try:
tcheck
except NameError:
return ("Error: " + title + " not found to add.")
title = title + ":" + ssn + ":" + ep
cur.execute("SELECT * FROM Holidays WHERE name LIKE \"" + holiday.strip() + "\"")
hcheck = cur.fetchone()
name = hcheck[0]
items = hcheck[1]
if title in items:
items = items.replace(";;;",";")
items = items.replace(";;",";")
items = items.replace(title.strip()+";","")
cur.execute("DELETE FROM Holidays WHERE name LIKE \"" + name + "\"")
sql.commit()
cur.execute("INSERT INTO Holidays VALUES(?,?)",(name,items))
sql.commit()
return (title + " has been unassociated with the " + name + " holiday.")
else:
return (title + " not found associated with the " + name + " holiday.")
def addholiday(holiday, title):
holiday = holiday.lower()
if ":" not in title:
title = titlecheck(title.strip())
if ("Quit." in title):
return ("User Quit. No action taken.")
elif ("Error" in title):
return title
else:
title = title.split(":")
ssn = title[1].strip()
epn = title[2].strip()
title = title[0].strip()
title = titlecheck(title.strip())
shows = plex.library.section('TV Shows')
the_show = shows.get(title).episodes()
for ep in the_show:
if((ep.seasonNumber == ssn) and (ep.index == epn)):
tcheck = ep.title
try:
tcheck
except NameError:
return ("Error: " + title + " not found to add.")
title = title + ":" + ssn + ":" + epn
cur.execute("SELECT * FROM Holidays WHERE name LIKE \"" + holiday.strip() + "\"")
hcheck = cur.fetchone()
if not hcheck:
name = holiday.strip()
items = title + ";"
else:
name = hcheck[0]
items = hcheck[1]
if title in items:
return ("Error: " + title + " is already associated with the following holiday: " + holiday + ".")
items = items + title + ";"
cur.execute("DELETE FROM Holidays WHERE name LIKE \"" + holiday + "\"")
sql.commit()
cur.execute("INSERT INTO Holidays VALUES(?,?)",(holiday,items))
sql.commit()
return (title + " has been associated with the " + holiday + " holiday.")
def addholiauto(holiday,title):
cur.execute("SELECT * FROM Holidays WHERE name LIKE \"" + holiday.strip() + "\"")
hcheck = cur.fetchone()
if not hcheck:
name = holiday.strip()
items = title + ";"
else:
name = hcheck[0]
items = hcheck[1]
if title in items:
return ("Error: " + title + " is already associated with the following holiday: " + holiday + ".")
items = items + title + ";"
cur.execute("DELETE FROM Holidays WHERE name LIKE \"" + holiday + "\"")
sql.commit()
cur.execute("INSERT INTO Holidays VALUES(?,?)",(holiday,items))
sql.commit()
return (title + " has been associated with the " + holiday + " holiday.")
def checkmode(option):
option = option.lower()
if "show" in option:
command = "SELECT State FROM States WHERE Option LIKE \"ENABLEFAVORITESMODESHOW\""
cur.execute(command)
if not cur.fetchall():
cur.execute("INSERT INTO States VALUES (?,?)",("ENABLEFAVORITESMODESHOW","Off"))
sql.commit()
elif "movie" in option:
command = "SELECT State FROM States WHERE Option LIKE \"ENABLEFAVORITESMODEMOVIE\""
cur.execute(command)
if not cur.fetchall():
cur.execute("INSERT INTO States VALUES (?,?)",("ENABLEFAVORITESMODEMOVIE","Off"))
sql.commit()
cur.execute(command)
say = cur.fetchone()[0]
return (say)
def addapproved(title):
checkpw = checkkidspw()
if ("Error:" in checkpw):
return checkpw
title = titlecheck(title)
command = "SELECT State FROM States WHERE Option LIKE \"APPROVEDLIST\""
cur.execute(command)
if not cur.fetchone():
writeme = title + ";"
cur.execute("DELETE FROM States WHERE Option LIKE \"APPROVEDLIST\"")
sql.commit()
cur.execute("INSERT INTO States VALUES (?,?)",("APPROVEDLIST",writeme.strip()))
sql.commit()
else:
cur.execute(command)
writeme = cur.fetchone()[0]
check = writeme.split(";")
chks = []
for item in check:
chks.append(item)
if (title not in chks):
writeme = writeme + title + ";"
cur.execute("DELETE FROM States WHERE Option LIKE \"APPROVEDLIST\"")
sql.commit()
cur.execute("INSERT INTO States VALUES (?,?)",("APPROVEDLIST",writeme.strip()))
sql.commit()
else:
return (title + " is already in the approved list.")
return (title + " has been added to the approved list.")
def removeapproved(title):
title = titlecheck(title)
command = "SELECT State FROM States WHERE Option LIKE \"APPROVEDLIST\""
cur.execute(command)
if not cur.fetchone():
return ("No approvied list to modify.")
cur.execute(command)
writeme = cur.fetchone()[0]
check = writeme.split(";")
chks = []
for item in check:
chks.append(item)
if title in chks:
repl = title.strip() + ";"
writeme = writeme.replace(repl,"")
cur.execute("DELETE FROM States WHERE Option LIKE \"APPROVEDLIST\"")
sql.commit()
cur.execute("INSERT INTO States VALUES (?,?)",("APPROVEDLIST",writeme.strip()))
sql.commit()
#chks.remove(title.strip())
return (title + " has been removed from the approved list.")
else:
return (title + " not found in approved list to remove.")
def addrejected(title):
title = titlecheck(title)
command = "SELECT State FROM States WHERE Option LIKE \"REJECTEDLIST\""
cur.execute(command)
if not cur.fetchone():
writeme = title + ";"
cur.execute("DELETE FROM States WHERE Option LIKE \"REJECTEDLIST\"")
sql.commit()
cur.execute("INSERT INTO States VALUES (?,?)",("REJECTEDLIST",writeme.strip()))
sql.commit()
else:
cur.execute(command)
writeme = cur.fetchone()[0]
check = writeme.split(";")
chks = []
for item in check:
chks.append(item)
if (title not in chks):
writeme = writeme + title + ";"
cur.execute("DELETE FROM States WHERE Option LIKE \"REJECTEDLIST\"")
sql.commit()
cur.execute("INSERT INTO States VALUES (?,?)",("REJECTEDLIST",writeme.strip()))
sql.commit()
else:
return (title + " is already in the rejected list.")
return (title + " has been added to the rejected list.")
def showrejected():
command = "SELECT State FROM States WHERE Option LIKE \"REJECTEDLIST\""
cur.execute(command)
if not cur.fetchone():
print ("The Rejected List is currently empty.")
else:
cur.execute(command)
rlist = cur.fetchone()[0]
rlist = rlist.split(";")
print ("The following items are in the Rejected List:\n")
for item in rlist:
item = item.replace("movie.","the movie ")
if item == "":
pass
else:
print (item)
def showapproved():
command = "SELECT State FROM States WHERE Option LIKE \"APPROVEDLIST\""
cur.execute(command)
if not cur.fetchone():
print ("The Approved List is currently empty.")
else:
cur.execute(command)
rlist = cur.fetchone()[0]
rlist = rlist.split(";")
print ("The following items are in the Approved List:\n")
for item in rlist:
item = item.replace("movie.","the movie ")
if item == "":
pass
else:
print (item)
def addapprovedrating(rating):
checkpw = checkkidspw()
if ("Error:" in checkpw):
return checkpw
command = "SELECT State FROM States WHERE Option LIKE \"APPROVEDRATINGS\""
cur.execute(command)
if not cur.fetchone():
allowed = ['TV-Y','TV-Y7','TV-G','G','PG']
for item in allowed:
try:
xallowed = xallowed + ";" + item
except NameError:
xallowed = item
cur.execute("INSERT INTO States VALUES (?,?)",("APPROVEDRATINGS",xallowed))
sql.commit()
del xallowed
cur.execute(command)
allowed = cur.fetchone()[0]
allowed = allowed.split(";")
if rating in allowed:
return ("Error: " + rating + " is already in the allowed list.")
for item in allowed:
try:
if item == "":
pass
else:
xallowed = xallowed + ";" + item
except NameError:
xallowed = item
xallowed = xallowed + ";" + rating
cur.execute("DELETE FROM States WHERE Option LIKE \"APPROVEDRATINGS\"")
sql.commit()
cur.execute("INSERT INTO States VALUES (?,?)",("APPROVEDRATINGS",xallowed))
sql.commit()
return (rating + " has been added to the approved ratings list.")
def removeapprovedrating(rating):
rating = rating.strip()
command = "SELECT State FROM States WHERE Option LIKE \"APPROVEDRATINGS\""
cur.execute(command)
if not cur.fetchone():
allowed = ['TV-Y','TV-Y7','TV-G','G', 'PG']
for item in allowed:
try:
xallowed = xallowed + ";" + item
except NameError:
xallowed = item
cur.execute("INSERT INTO States VALUES (?,?)",("APPROVEDRATINGS",xallowed))
sql.commit()
del xallowed
cur.execute(command)
allowed = cur.fetchone()[0]
yallowed = allowed.split(";")
if rating not in yallowed:
return ("Error: " + rating + " is not in the allowed list.")
allowed = allowed.replace(rating,"")
allowed = allowed.replace(";;",";")
allowed = allowed.strip()
allowed = allowed.split(";")
for item in allowed:
try:
if item == "":
pass
else:
xallowed = xallowed + ";" + item
except NameError:
xallowed = item
cur.execute("DELETE FROM States WHERE Option LIKE \"APPROVEDRATINGS\"")
sql.commit()
cur.execute("INSERT INTO States VALUES (?,?)",("APPROVEDRATINGS",xallowed))
sql.commit()
return (rating + " has been removed from the approved ratings list.")
def approvedratings():
command = "SELECT State FROM States WHERE Option LIKE \"APPROVEDRATINGS\""
cur.execute(command)
if not cur.fetchone():
allowed = ['TV-Y','TV-Y7','TV-G','G', 'PG']
for item in allowed:
try:
xallowed = xallowed + ";" + item
except NameError:
xallowed = item
cur.execute("INSERT INTO States VALUES (?,?)",("APPROVEDRATINGS",xallowed))
sql.commit()
cur.execute(command)
allowed = cur.fetchone()[0]
allowed = allowed.split(";")
print ("The following ratings are currently allowed in kids mode:\n")
for item in allowed:
print (item)
return ("\nDone.")
def kidscheck(option, title):
command = "SELECT State FROM States WHERE Option LIKE \"APPROVEDLIST\""
command2 = "SELECT State FROM States WHERE Option LIKE \"REJECTEDLIST\""
command3 = "SELECT State FROM States WHERE Option LIKE \"APPROVEDRATINGS\""
cur.execute(command)
approved = []
rejected = []
try:
alist = cur.fetchone()[0]
except Exception:
alist = ""
cur.execute(command2)
try:
rlist = cur.fetchone()[0]
except Exception:
rlist = ""
alist = alist.split(";")
for item in alist:
approved.append(item)
rlist = rlist.split(";")
for item in rlist:
rejected.append(item)
if title in approved:
return ("pass")
if title in rejected:
return ("fail")
cur.execute(command3)
if not cur.fetchone():
allowed = ['TV-Y','TV-Y7','TV-G','G', 'PG']
for item in allowed:
try:
xallowed = xallowed + ";" + item
except NameError:
xallowed = item
cur.execute("INSERT INTO States VALUES (?,?)",("APPROVEDRATINGS",xallowed))
sql.commit()
cur.execute(command3)
allowed = cur.fetchone()[0]
allowed = allowed.split(";")
option = option.lower()
title = title.lower()
if option == "show":
command = "SELECT Rating FROM TVshowlist WHERE TShow LIKE \"" + title + "\""
elif option == "movie":
title = title.replace("movie.","")
command = "SELECT Rating FROM Movies WHERE Movie LIKE \"" + title + "\""
cur.execute(command)
found = cur.fetchone()[0]
if found in allowed:
return ("pass")
else:
return ("fail")
def setkidspassword(option):
checkpw = getkidspw()
checkme = getpass.getpass('Current Password: ')
if checkpw.strip() != checkme.strip():
return ("Error: Password Missmatch. No action taken.")
option = option.strip()
command = "DELETE FROM States WHERE Option LIKE \"KIDSPW\""
cur.execute(command)
sql.commit()
cur.execute("INSERT INTO States VALUES (?,?)",("KIDSPW",option))
sql.commit()
return ("The KIDSPW has been set.")
def getkidspw():
command = "SELECT State FROM States WHERE Option LIKE \"KIDSPW\""
cur.execute(command)
if not cur.fetchone():
password = "supersneakey"
cur.execute("INSERT INTO States VALUES (?,?)",("KIDSPW",password))
sql.commit()
cur.execute(command)
pw = cur.fetchone()[0]
return (pw)