-
Notifications
You must be signed in to change notification settings - Fork 0
/
system_setup.py
550 lines (488 loc) · 18.2 KB
/
system_setup.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
import os
import sys
import getpass
import subprocess
import sqlite3
import urllib3
import platform
#urllib3.disable_warning()
def worklist(thearray):
if int(len(thearray) == 0):
return ("Error: No results found.")
movies = thearray
mcount = 1
mvcount = 0
mmin = 0
mmax = 9
mpmin = 1
if mmax >= len(movies):
mmax = int(len(movies))-1
exitc = ""
while "quit" not in exitc:
cls()
try:
print ("Error: " + Error + "\nThe Following Items Were Found:\n")
del Error
except NameError:
print ("The Following Items Were Found:\n")
while mmin <= mmax:
print (str(mmin+1) + ": " + movies[mmin])
mmin = mmin + 1
print ("\nShowing Items " + str(mpmin) + " out of " + str(mmax+1)+ " Total Found: " + str(len(movies)))
print ("\nSelect an item to return the corresponding item.\nTo see a description of an item enter \'desc number\', where number is the corresponding number.\nTo jump to a letter enter \'letter a\', where a is the desired letter.\nEnter \'Yes\' to go to the next page, and \'No\' to exit.\n")
getme = input('Choice:')
if (("yes" in getme.lower()) and ("letter" not in getme.lower())):
mvcount = mvcount + 10
mpmin = mmax + 1
mmax = mmax + 10
if (mmax > int(len(movies)-1)):
mcheck = int(mmax) - int(len(movies)-1)
if ((mcheck > 0) and (mcheck < 10)):
mmax = mmax-mcheck
elif mcheck > 10:
return ("Done.")
if (mmax == int(len(movies)+9)):
return ("Done")
elif (("no" in getme.lower()) and ("letter" not in getme.lower())):
exitc = "quit"
return ("done")
else:
try:
return movies[int(getme)-1]
except Exception:
Error = "Invalid Selection. Please Try Again."
mmin = mmin - 10
http = urllib3.PoolManager()
try:
input = raw_input
except NameError:
pass
def cls():
os.system('cls' if os.name=='nt' else 'clear')
ostype = platform.system()
try:
extraopt = str(sys.argv[1])
except Exception:
extraopt = "none"
if "Linux" in ostype:
user = getpass.getuser()
if ("root" in user):
print ("Run this command as your non-root user, without sudo. You will be prompted for sudo access where it is necessary in this setup.")
else:
newdir = "/home/" + user + "/"
homedir = "/home/" + user + "/hasystem/"
writeme = "homedir = \'" + homedir + "\'\n"
writemehome = writeme
print ("Hello " + user + ". I am now adding the alias add script now. You will be prompted for sudo for this.\n")
if not os.path.exists(homedir):
os.makedirs(homedir)
sayme = "The necessary directory has been created."
else:
sayme = ""
print (sayme)
try:
file0 = homedir + "add_to_bash.py"
with open (file0, "r") as file:
readme = file.read()
file.close()
print ("check pass. add_to_bash.py exists.")
except IOError:
try:
url = "https://raw.githubusercontent.com/amazingr4b/TBN-Plex/master/add_to_bash.py"
newfile = http.request('GET', url, preload_content=False)
newfile = writemehome + str(newfile.data)
print newfile
with open(file0, 'wb') as file:
file.write(newfile)
file.close()
print ("File successfully moved to the necessary directory.")
except IOError:
print ("warning add_to_bash.py does not exist. Alias commands will not work.")
try:
filex1 = homedir + "tbn_schedule.py"
with open (filex1, "r") as file:
readme = file.read()
file.close()
print ("check pass. tbn_schedule.py exists.")
except IOError:
try:
url = "https://raw.githubusercontent.com/amazingr4b/TBN-Plex/master/tbn_schedule.py"
newfile = http.request('GET', url, preload_content=False)
newfile = writemehome + str(newfile.data)
print newfile
with open(filex1, 'wb') as file:
file.write(newfile)
file.close()
print ("File successfully moved to the necessary directory.")
except IOError:
print ("warning tbn_schedule.py does not exist. Scheduling will not work.")
try:
file10 = homedir + "aliases"
with open (file10, "r") as file:
readme = file.read()
file.close()
print ("check pass. aliases exists.")
except IOError:
try:
url = "https://raw.githubusercontent.com/amazingr4b/TBN-Plex/master/aliases"
newfile = http.request('GET', url, preload_content=False)
#print (newfile)
with open(file10, 'wb') as file:
file.write(newfile.data)
file.close()
print ("File successfully moved to the necessary directory.")
except IOError:
print ("warning aliases does not exist. Alias commands will not work.")
try:
command = "sudo python " + file0 + " " + user.strip()
#print (command)
os.system(command)
except Exception:
print ("Failed to add aliases to Bash. see add_to_bash.py for list of alias it is recommended you add.\n")
updatecheck = "fail"
print ("Done. " + user + " " + sayme + " I am now trying to add a cron entry for the play checking script.")
try:
file00 = homedir + "add_to_cron.py"
with open (file00, "r") as file:
readme = file.read()
file.close()
print ("check pass. add_to_cron.py exists.")
except IOError:
try:
url = "https://raw.githubusercontent.com/amazingr4b/TBN-Plex/master/add_to_cron.py"
newfile = http.request('GET', url, preload_content=False)
newfile = writemehome + str(newfile.data)
with open(file00, 'wb') as file:
file.write(newfile)
file.close()
print ("File successfully moved to the necessary directory.")
except IOError:
print ("warning add_to_cron.py does not exist. Cron add will fail.")
try:
command = "sudo python " + file00 + " " + user.strip()
os.system(command)
except Exception:
print ("Failed to add cronjob. see add_to_cron.py for recommended job.\n")
updatecheck = "fail"
else:
print ("Unable to add cron or bash entries on a Windows system. I am skipping those steps.")
hcheck = "negative"
while ("goon" not in hcheck):
print ("In what directory would you like to place the /hasystem folder and associated files?\nFor Example: 'C:\\home\\user\\documents\\' Note: This location needs to currently exist.")
homedir = input('Path ')
homedir = homedir.replace("\\", "\\\\")
print (homedir)
if not os.path.exists(homedir):
cls()
print ("\nError. That directory does not exist. Please try again.\n")
else:
hcheck = "goon"
print ("Pass")
homedir = homedir + "hasystem\\"
writeme = "homedir = \'" + homedir + "\\'\n"
writemehome = writeme
if not os.path.exists(homedir):
os.makedirs(homedir)
print (homedir + " has been successfully created.\n")
else:
print (homedir + " already exists. Moving on.")
file1 = homedir + "playstate.txt"
try:
with open (file1, "r") as file:
readme = file.read()
file.close()
print ("playstate text file already exists. Checking pstate file now.")
except IOError:
with open (file1, "w") as file:
file.write("")
file.close()
print ("playstate text file successfully added.")
file2 = homedir + "pstate.txt"
try:
with open (file2, "r") as file:
readme = file.read()
file.close()
print ("pstate text file already exists. Checking perror file now.")
except IOError:
with open (file2, "w") as file:
file.write("")
file.close()
print ("pstate text file successfully added.")
file3 = homedir + "perror.txt"
try:
with open (file3, "r") as file:
readme = file.read()
file.close()
print ("perror text file already exists. Checking playstatestatus file now.")
except IOError:
with open (file3, "w") as file:
file.write("")
file.close()
print ("playstate text file successfully added.")
file4 = homedir + "playstatestatus.txt"
try:
with open (file4, "r") as file:
readme = file.read()
file.close()
print ("playstatestatus text file already exists. Checking script file now.")
except IOError:
file4 = homedir + "playstatestatus.txt"
with open (file4, "w") as file:
file.write("Off")
file.close()
print ("playstatestatus text file successfully added.")
file5 = homedir + "system.py"
try:
with open (file5, "r") as file:
readme = file.read()
file.close()
print ("check pass. system.py exists.")
except IOError:
try:
url = "https://raw.githubusercontent.com/amazingr4b/TBN-Plex/master/system.py"
newfile = http.request('GET', url, preload_content=False)
newfile = newfile.data
#print (newfile)
with open(file5, 'wb') as file:
file.write(newfile)
file.close()
with open (file5, 'r') as file:
rewrite = file.read()
file.close()
rewrite = writemehome + "\n" + rewrite
with open(file5, "w") as file:
file.write(rewrite)
file.close()
print ("File successfully moved to the necessary directory.")
except FileNotFoundError:
print ("warning system.py does not exist. The TBN controller will not work.")
file6 = homedir + "piplaystate.py"
try:
with open (file6, "r") as file:
readme = file.read()
file.close()
print ("check pass. piplaystate.py exists.")
except IOError:
try:
url = "https://raw.githubusercontent.com/amazingr4b/TBN-Plex/master/piplaystate.py"
newfile = http.request('GET', url, preload_content=False)
newfile = newfile.data
with open(file6, 'wb') as file:
file.write(newfile)
file.close()
with open (file6, 'r') as file:
rewrite = file.read()
file.close()
rewrite = writemehome + "\n" + rewrite
with open(file6, "w") as file:
file.write(rewrite)
file.close()
print ("File successfully moved to the necessary directory.")
except Exception:
print ("warning piplaystate.py does not exist. The play check status script will not work.")
file7 = homedir + "playstatus.py"
try:
with open (file7, "r") as file:
readme = file.read()
file.close()
print ("check pass. playstatus.py exists.")
except IOError:
try:
url = "https://raw.githubusercontent.com/amazingr4b/TBN-Plex/master/playstatus.py"
newfile = http.request('GET', url, preload_content=False)
newfile = newfile.data
with open(file7, 'wb') as file:
file.write(newfile)
file.close()
with open (file7, 'r') as file:
rewrite = file.read()
file.close()
rewrite = writeme + rewrite
with open(file7, "w") as file:
file.write(rewrite)
file.close()
print ("File successfully moved to the necessary directory.")
except Exception:
print ("warning playstatus.py does not exist. The play check status script will not work.")
file8 = homedir + "upddatedb_pi.py"
try:
with open (file8, "r") as file:
readme = file.read()
file.close()
print ("check pass. upddatedb_pi.py exists.")
updatecheck = "pass"
except IOError:
try:
url = "https://raw.githubusercontent.com/amazingr4b/TBN-Plex/master/upddatedb_pi.py"
newfile = http.request('GET', url, preload_content=False)
newfile = newfile.data
#print (newfile)
#print (writeme)
with open(file8, "wb") as file:
file.write(newfile)
file.close()
with open (file8, 'r') as file:
rewrite = file.read()
file.close()
rewrite = writeme + rewrite
with open(file8, "w") as file:
file.write(rewrite)
file.close()
print ("File successfully moved to the necessary directory.")
updatecheck = "pass"
except FileNotFoundError:
print ("warning updateddb_pi.py does not exist. The system will be unable to build the shows and movie tables in your database.")
updatecheck = "fail"
file8a = homedir + "getshow.py"
try:
with open (file8a, "r") as file:
readme = file.read()
file.close()
print ("check pass. getshow.py exists.")
updatecheck = "pass"
except IOError:
try:
url = "https://raw.githubusercontent.com/amazingr4b/TBN-Plex/master/getshow.py"
newfile = http.request('GET', url, preload_content=False)
newfile = newfile.data
#print (newfile)
#print (writeme)
with open(file8a, "wb") as file:
file.write(newfile)
file.close()
with open (file8a, 'r') as file:
rewrite = file.read()
file.close()
rewrite = writeme + rewrite
with open(file8a, "w") as file:
file.write(rewrite)
file.close()
print ("File successfully moved to the necessary directory.")
updatecheck = "pass"
except FileNotFoundError:
print ("warning getshow.py does not exist.")
updatecheck = "fail"
print ("Checking Database now.\n")
MYDB = homedir + "myplex.db"
sql = sqlite3.connect(MYDB)
cur = sql.cursor()
#generating settings table if it does not already exist.
cur.execute('CREATE TABLE IF NOT EXISTS settings(item TEXT, setting TEXT)')
sql.commit()
#deletes existing settings if the reset flag is present
if "reset" in extraopt:
cur.execute('DELETE FROM settings')
sql.commit()
#checks for plex user name and PW. Used for Plex API.
cur.execute('SELECT setting FROM settings WHERE item LIKE \'PLEXUN\'')
if not cur.fetchone():
PLEXUN = str(input('Plex Username: '))
writeme = str("PLEXUN")
cur.execute('INSERT INTO settings VALUES(?,?)', (writeme,PLEXUN))
sql.commit()
else:
cur.execute('SELECT setting FROM settings WHERE item LIKE \'PLEXUN\'')
PLEXUN = cur.fetchone()[0]
cur.execute('SELECT setting FROM settings WHERE item LIKE \'PLEXPW\'')
if not cur.fetchone():
PLEXPW = str(input('Plex Password: '))
cur.execute('INSERT INTO settings VALUES(?,?)', ('PLEXPW',PLEXPW))
sql.commit()
else:
cur.execute('SELECT setting FROM settings WHERE item LIKE \'PLEXPW\'')
PLEXPW = cur.fetchone()[0]
from plexapi.myplex import MyPlexAccount
user = MyPlexAccount.signin(PLEXUN,PLEXPW)
resces = user.resources()
servers = []
for item in resces:
if "Plex Media Server" in item.product:
servers.append(item.name)
server = worklist(servers)
for item in resces:
if "Plex Media Server" in item.product:
servers.append(item.name)
if item.name == server:
PLEXSVR = item.name
PLEXSERVERIP = str(item.connections[0].address)
PLEXSERVERPORT = str(item.connections[0].port)
baseurl = "http://" + PLEXSERVERIP + ":" + PLEXSERVERPORT
from plexapi.server import PlexServer
plex = PlexServer(baseurl)
cur.execute('SELECT setting FROM settings WHERE item LIKE \'PLEXSVR\'')
if not cur.fetchone():
cur.execute('INSERT INTO settings VALUES(?,?)', ('PLEXSVR',PLEXSVR))
sql.commit()
cur.execute('SELECT setting FROM settings WHERE item LIKE \'PLEXSERVERIP\'')
if not cur.fetchone():
cur.execute('INSERT INTO settings VALUES(?,?)', ('PLEXSERVERIP',PLEXSERVERIP))
sql.commit()
cur.execute('SELECT setting FROM settings WHERE item LIKE \'PLEXSERVERPORT\'')
if not cur.fetchone():
cur.execute('INSERT INTO settings VALUES(?,?)', ('PLEXSERVERPORT',PLEXSERVERPORT))
sql.commit()
cur.execute('SELECT setting FROM settings WHERE item LIKE \'PLEXCLIENT\'')
if not cur.fetchone():
daclients = []
for client in plex.clients():
daclients.append(client.title)
print ("Select a Client. The Following Clients are available:")
counter = 1
for client in daclients:
print (str(counter) + "- " + client.strip() + "\n")
counter = counter + 1
choice = int(input('New Client: '))
try:
client = daclients[choice-1].strip()
cur.execute('DELETE FROM settings WHERE item LIKE \'PLEXCLIENT\'')
sql.commit()
cur.execute('INSERT INTO settings VALUES(?,?)',('PLEXCLIENT',client))
sql.commit()
cur.execute("SELECT * FROM settings WHERE item LIKE \'PLEXCLIENT\'")
test = cur.fetchone()
print ("Client successfully set to: " + client.strip())
except Exception:
print ("Error. Unable to update client. Please try again.")
#get wildcard show name. Used as part of random media picking mechanism.
cur.execute('SELECT setting FROM settings WHERE item LIKE \'WILDCARD\'')
if not cur.fetchone():
WILDCARD = str(input('Wild Card Show: '))
cur.execute('INSERT INTO settings VALUES(?,?)', ('WILDCARD',WILDCARD))
sql.commit()
cur.execute('CREATE TABLE IF NOT EXISTS States(Option TEXT, State TEXT)')
sql.commit()
cur.execute('DELETE FROM States')
sql.commit()
cur.execute('INSERT INTO States VALUES(?,?)', ('Playmode','normal'))
sql.commit()
cur.execute('INSERT INTO States VALUES(?,?)', ('queue',' '))
sql.commit()
cur.execute('INSERT INTO States VALUES(?,?)', ('Pending', ''))
sql.commit()
cur.execute('INSERT INTO States VALUES(?,?)', ('Nowplaying','Stopped'))
sql.commit()
cur.execute('CREATE TABLE IF NOT EXISTS Movies(Movie TEXT, Summary TEXT, Rating TEXT, Tagline TEXT, Genre TEXT, Director TEXT, Actors TEXT)')
sql.commit()
cur.execute('CREATE TABLE IF NOT EXISTS backupmovies(Movie TEXT, Summary TEXT, Rating TEXT, Tagline TEXT, Genre TEXT, Director TEXT, Actors TEXT)')
sql.commit()
cur.execute("CREATE TABLE IF NOT EXISTS TVshowlist(TShow TEXT, Summary TEXT, Genre TEXT, Rating TEXT, Duration INT, Totalnum INT)")
sql.commit()
cur.execute("CREATE TABLE IF NOT EXISTS backshowlist(TShow TEXT, Summary TEXT, Genre TEXT, Rating TEXT, Duration INT, Totalnum INT)")
sql.commit()
cur.execute('CREATE TABLE IF NOT EXISTS TVCounts(Show TEXT, Number INT)')
sql.commit()
cur.execute('CREATE TABLE IF NOT EXISTS Blocks(Name TEXT, items TEXT, Count INT)')
sql.commit()
print ("Necessary File check complete.")
if ("pass" in updatecheck):
print ("Would you like to update your system database now to add the available shows and movies in your library?")
choice = str(input('Yes or No? '))
if "y" in choice.lower():
if "Windows" not in ostype:
command = "python " + homedir + "upddatedb_pi.py all"
else:
command = homedir + "upddatedb_pi.py all"
os.system(command)
print ("If you needed that entry for cron it was: @reboot python /home/pi/hasystem/piplaystate.py &")
else:
print ("The files system_setup.py can add have been added.\n Done!")