This repository has been 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 pathtest.py
550 lines (528 loc) · 16.1 KB
/
test.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 urllib3
import subprocess
import requests
import time
import os
import sys
import sqlite3
import platform
#top
global pcount
global TVGET
global LOGGEDIN
try:
input = raw_input
except NameError:
pass
MYDB = homedir + "myplex.db"
http = urllib3.PoolManager()
sql = sqlite3.connect(MYDB)
cur = sql.cursor()
def getsections():
plexlogin()
xlibrary = plex.library.sections()
foundsect = []
print ("The Following Sections are available off your server.")
for lib in xlibrary:
print lib.title
foundsect.append(lib.title)
return foundsect
def plexlogin():
global PLEXUN
global PLEXSVR
global PLEXCLIENT
global plex
global client
global LOGGEDIN
try:
cur.execute('SELECT setting FROM settings WHERE item LIKE \'PLEXUN\'')
PLEXUN = cur.fetchone()
PLEXUN = PLEXUN[0]
cur.execute('SELECT setting FROM settings WHERE item LIKE \'PLEXPW\'')
PLEXPW = cur.fetchone()
PLEXPW = PLEXPW[0]
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.")
from plexapi.myplex import MyPlexAccount
try:
LOGGEDIN
except Exception:
try:
from plexapi.server import PlexServer
baseurl = 'http://' + PLEXSERVERIP + ':' + PLEXSERVERPORT
plex = PlexServer(baseurl)
except Exception:
print ("Local Fail. Trying cloud access.")
user = MyPlexAccount.signin(PLEXUN,PLEXPW)
plex = user.resource(PLEXSVR).connect()
client = plex.client(PLEXCLIENT)
LOGGEDIN = "YES"
except IndexError:
print ("Error getting necessary plex api variables. Run system_setup.py.")
def getmovies():
mcheck = "fail"
command = "SELECT setting FROM settings WHERE item LIKE \"MOVIEGET\""
cur.execute(command)
if not cur.fetchone():
mcheck = "fail"
else:
cur.execute(command)
MOVIEGET = cur.fetchone()[0]
if "http" in MOVIEGET:
cur.execute("DELETE FROM settings WHERE item LIKE \"MOVIEGET\"")
sql.commit()
else:
mcheck = "pass"
if "fail" in mcheck:
print ("We need to update your settings to use the new movieget function. Input the name of the section that is your \"Movies\" section.")
foundsect = getsections()
MOVIEGET = str(input('Enter Section Name: '))
MOVIEGET = MOVIEGET.strip()
if MOVIEGET in foundsect:
print (MOVIEGET.strip())
cur.execute("INSERT INTO settings VALUES (?,?)",("MOVIEGET",MOVIEGET))
sql.commit()
else:
print ("Error: " + MOVIEGET + " not found as an available section in your library.")
else:
print ('Using the following movie library: ' + MOVIEGET)
plexlogin()
movies = []
mlist = plex.library.section(MOVIEGET)
mxlist = mlist.search("")
count = 0
xnum = int(len(mxlist)-1)
for video in mxlist:
try:
name = str(video.title)
except Exception:
name = video.title
name = name.encode("utf8")
name = str(name)
name = name.replace("'","''")
summary = ""
rating = str(video.contentRating)
try:
tagline = str(video.tagline)
except Exception:
try:
tagline = tagline.decode("ascii", "ignore")
except Exception:
tagline = ""
tagline = tagline.replace("'","''")
agenre = video.genres
gcmd = "SELECT Genre FROM backupmovies WHERE Movie LIKE \"" + name + "\""
cur.execute(gcmd)
if not cur.fetchone():
gcheck = []
else:
cur.execute(gcmd)
gcheck = cur.fetchone()[0]
gcheck = gcheck.split(' ')
for item in agenre:
if item == " ":
pass
elif (item not in gcheck):
try:
bgenre = bgenre + " " + item.tag
except NameError:
bgenre = item.tag
try:
bgenre
except NameError:
bgenre = ""
for item in gcheck:
if item == " ":
pass
elif ((item not in agenre) and (item not in bgenre)):
try:
bgenre = bgenre + " " + item.strip()
except NameError:
bgenre = item.strip()
bgenre = bgenre.strip()
directors = ""
bactors = ""
try:
cur.execute('SELECT * FROM Movies WHERE Movie LIKE \'' + name + '\'')
if not cur.fetchone():
cur.execute('INSERT INTO Movies VALUES(?, ?, ?, ?, ?, ?, ?)', (name, summary, rating, tagline, bgenre, directors, bactors))
sql.commit()
except IndexError:
print ("\nError adding " + name)
with open(PROBLEMS, 'a') as file:
file.write(name.decode("ascii", "ignore") + " " + bactors + "\n")
file.close()
progress(xnum)
del bgenre
clearprogress()
print ("\nDone.")
def getgenres(show):
global TVGET
command = "SELECT setting FROM settings WHERE item LIKE \"TVGENREFIX\""
cur.execute(command)
if not cur.fetchone():
cur.execute("SELECT setting FROM settings WHERE item LIKE \"PLEXSERVERIP\"")
wlink = cur.fetchone()[0]
cur.execute("SELECT setting FROM settings WHERE item LIKE \"PLEXSERVERPORT\"")
wip = cur.fetchone()[0]
slink = "http://" + wlink + ":" + wip + "/library/sections/"
response = http.urlopen('GET', slink, preload_content=False).read()
response = str(response)
response = response.split("Directory allowSync=")
print ("The Following Sections are available off your server.")
sctsn = []
for item in response:
name = item
section = item
try:
name = name.split("title=\"")
name = name[1]
name = name.split("\"")
name = name[0]
section = section.split("key=\"")
section = section[1]
section = section.split("\"")
section = section[0]
link = "http://" + wlink + ":" + wip + "/library/sections/" + section + "/all/"
sctsn.append(link)
if name == TVGET:
TVGENREFIX = link
print TVGENREFIX
if TVGENREFIX.strip() not in sctsn:
Print ("Error: Invalid Selection Choice. Alternate Method Will Fail!")
else:
cur.execute("INSERT INTO settings VALUES (?,?)",("TVGENREFIX",TVGENREFIX))
sql.commit()
cur.execute(command)
TVGENREFIX = cur.fetchone()[0]
response = http.urlopen('GET', TVGENREFIX, preload_content=False).read()
response = str(response)
shows = response.split('<Directory ratingKey=')
counter = 1
xnum = int(len(shows))-1
while counter <= int(len(shows)-1):
xshow = shows[counter]
genres = xshow
title = xshow
title = title.split('title="')
title = title[1]
title = title.split('"')
title = title[0]
title = title.replace(''','\'')
title = title.replace('&','&')
title = title.replace('?','')
title = title.replace('/',' ')
title = title.replace("'","'")
if (title == show):
genres = genres.split("<Genre tag=\"")
try:
genre = genres[1]
except IndexError:
genre = "none"
try:
genre2 = genres[2]
genre2 = genre2.split('" />')
genre2 = genre2[0]
except IndexError:
genre2 = "none"
try:
genre3 = genres[3]
genre3 = genre3.split('" />')
genre3 = genre3[0]
except IndexError:
genre3 = "none"
genre = genre.split('" />')
genre = genre[0] + ";" + genre2 + ";" + genre3 + ";"
genre = genre.replace('none;','')
genre = genre.split(";")
return genre
counter = counter + 1
return ("")
def getshows():
global TVGET
tcheck = "fail"
command = "SELECT setting FROM settings WHERE item LIKE \"TVGET\""
cur.execute(command)
if not cur.fetchone():
tcheck = "fail"
else:
cur.execute(command)
TVGET = cur.fetchone()[0]
if "http" in TVGET:
cur.execute("DELETE FROM settings WHERE item LIKE \"TVGET\"")
sql.commit()
else:
tcheck = "pass"
if "fail" in tcheck:
print ("We need to update your settings to use the new tvget function. Input the name of the section that is your \"TV Shows\" section.")
foundsect = getsections()
TVGET = str(input('Enter Section Name: '))
TVGET = TVGET.strip()
if TVGET in foundsect:
print (TVGET.strip())
cur.execute("INSERT INTO settings VALUES (?,?)",("TVGET",TVGET))
sql.commit()
else:
print ("Error: " + TVGET + " not found as an available section in your library.")
else:
print ('Using the following TV Show library: ' + TVGET)
plexlogin()
tlist = plex.library.section(TVGET)
tvlist = tlist.search("")
xnum = int(len(tvlist))-1
for show in tvlist:
name = str(show.title)
summary = show.summary
summary = str(summary.encode('ascii','ignore')).strip()
rating = str(show.contentRating)
rating = rating.replace("__NA__","NA")
duration = int(show.duration)/60000
agenre = show.genres
try:
agenre = str(agenre)
if agenre == "__NA__":
agenre = ""
#print ("Genre Get Failed. Trying alternate method")
agenre = getgenres(name)
except IndexError:
agenre = []
print ("Genre Get Failed.")
gcmd = "SELECT Genre FROM backshowlist WHERE TShow LIKE \"" + name + "\""
cur.execute(gcmd)
if not cur.fetchone():
gcheck = []
else:
cur.execute(gcmd)
gcheck = cur.fetchone()[0]
gcheck = gcheck.replace(";"," ")
gcheck = gcheck.split(' ')
for item in agenre:
if ((item == " ") or (item == "")):
pass
elif (item not in gcheck):
try:
bgenre = bgenre + " " + item
except NameError:
bgenre = item
try:
str(bgenre)
except NameError:
bgenre = ""
for item in gcheck:
if item == " ":
pass
elif (item not in bgenre):
try:
bgenre = bgenre + " " + item.strip()
except NameError:
bgenre = item.strip()
bgenre = bgenre.strip()
totalnum = int(show.leafCount)
cur.execute("SELECT * FROM TVshowlist WHERE TShow LIKE\"" + name + "\"")
if not cur.fetchone():
cur.execute("INSERT INTO TVshowlist VALUES (?,?,?,?,?,?)",(name,summary,bgenre,rating,duration,totalnum))
sql.commit()
progress(xnum)
del bgenre
clearprogress()
def getcustom(section):
if ("preroll" in section.lower()):
cur.execute("CREATE TABLE IF NOT EXISTS prerolls(name TEXT, duration INT)")
sql.commit()
mcheck = "fail"
command = "SELECT setting FROM settings WHERE item LIKE \"PREROLLPART\""
cur.execute(command)
if not cur.fetchone():
mcheck = "fail"
else:
cur.execute(command)
PREROLLPART = cur.fetchone()[0]
if "http" in PREROLLPART:
cur.execute("DELETE FROM settings WHERE item LIKE \"PREROLLPART\"")
sql.commit()
else:
mcheck = "pass"
if "fail" in mcheck:
print ("We need to update your settings to use the new preroll get function. Input the name of the section that is your \"Pre Rolls\" section.")
foundsect = getsections()
PREROLLPART = str(input('Enter Section Name: '))
PREROLLPART = PREROLLPART.strip()
if PREROLLPART in foundsect:
print (PREROLLPART.strip())
cur.execute("INSERT INTO settings VALUES (?,?)",("PREROLLPART",PREROLLPART))
sql.commit()
else:
print ("Error: " + PREROLLPART + " not found as an available section in your library.")
else:
print ('Using the following Preroll library: ' + PREROLLPART)
USEME = PREROLLPART
USETABLE = "prerolls"
elif ("commercial" in section.lower()):
cur.execute("CREATE TABLE IF NOT EXISTS commercials(name TEXT, duration INT)")
sql.commit()
mcheck = "fail"
command = "SELECT setting FROM settings WHERE item LIKE \"COMPART\""
cur.execute(command)
if not cur.fetchone():
mcheck = "fail"
else:
cur.execute(command)
COMPART = cur.fetchone()[0]
if "http" in COMPART:
cur.execute("DELETE FROM settings WHERE item LIKE \"COMPART\"")
sql.commit()
else:
mcheck = "pass"
if "fail" in mcheck:
print ("We need to update your settings to use the new Commercial get function. Input the name of the section that is your \"Commercials\" section.")
foundsect = getsections()
COMPART = str(input('Enter Section Name: '))
COMPART = COMPART.strip()
if COMPART in foundsect:
print (COMPART.strip())
cur.execute("INSERT INTO settings VALUES (?,?)",("COMPART",COMPART))
sql.commit()
else:
print ("Error: " + COMPART + " not found as an available section in your library.")
else:
print ('Using the following Commercial library: ' + COMPART)
USEME = COMPART
USETABLE = "commercials"
elif ("custom." in section.lower()):
section = section.lower()
section = section.replace("custom.","").strip()
item = "CUSTOM_" + section
cur.execute("CREATE TABLE IF NOT EXISTS " + item + "(name TEXT, duration INT, type TEXT)")
sql.commit()
command = "SELECT setting FROM settings WHERE item LIKE \"" + item + "\""
mcheck = "fail"
cur.execute(command)
if not cur.fetchone():
mcheck = "fail"
else:
cur.execute(command)
COMPART = cur.fetchone()[0]
if "http" in COMPART:
cur.execute("DELETE FROM settings WHERE item LIKE \"" + item + "\"")
sql.commit()
else:
mcheck = "pass"
if "fail" in mcheck:
print ("We need to update your settings to use the new Custom get function. Input the name of the section that is your \"" + item + "\" section.")
foundsect = getsections()
COMPART = str(input('Enter Section Name: '))
COMPART = COMPART.strip()
if COMPART in foundsect:
print (COMPART.strip())
cur.execute("INSERT INTO settings VALUES (?,?)",(item,COMPART))
sql.commit()
else:
print ("Error: " + COMPART + " not found as an available section in your library.")
else:
print ('Using the following Custom library: ' + COMPART)
USEME = COMPART
USETABLE = item
plexlogin()
mlist = plex.library.section(USEME)
mxlist = mlist.search("")
count = 0
xnum = int(len(mxlist)-1)
for video in mxlist:
try:
name = str(video.title)
except Exception:
name = video.title
name = name.encode("utf8")
name = str(name)
name = name.replace("'","''")
duration = int(video.duration)/1000
cur.execute("SELECT * FROM " + USETABLE + " WHERE name LIKE \"" + name + "\"")
if not cur.fetchone():
if (("prerolls" in USETABLE) or ("commercials" in USETABLE)):
cur.execute("INSERT INTO " + USETABLE + " VALUES (?,?)",(name, duration))
sql.commit()
print ("Found and added: " + name + ".")
elif ("CUSTOM_" in USETABLE):
cur.execute("INSERT INTO " + USETABLE + " VALUES (?,?,?)",(name, duration, section))
sql.commit()
print ("Found and added: " + name + ".")
progress(xnum)
clearprogress()
print ("Done.")
def progress(num):
num = int(num)
global pcount
try:
pcount
except NameError:
pcount = 0
pcount = pcount + 1
perc = round((float(pcount) / float(num)) * 100, 1)
sys.stdout.write("\r" + str(perc) + "%")
sys.stdout.flush()
def clearprogress():
global pcount
pcount = 0
def startupactionmovie():
command = "python " + homedir + "system.py backupmoviedb"
os.system(command)
cur.execute("DELETE FROM Movies")
sql.commit()
print ("Movie Table purged and ready for data.")
def startupactiontv():
command = "python " + homedir + "system.py backuptvdb"
os.system(command)
cur.execute("DELETE FROM TVshowlist")
sql.commit()
print ("TV Table purged and ready for data.")
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 TVshowlist(TShow TEXT, Summary TEXT, Genre TEXT, Rating TEXT, Duration INT, Totalnum INT)')
sql.commit()
print ("Database update starting...\n")
try:
if ("movie" in str(sys.argv)):
tpe = "movie"
startupactionmovie()
getmovies()
elif ("shows" in str(sys.argv)):
tpe = "tv"
startupactiontv()
getshows()
elif ("prerolls" in str(sys.argv)):
tpe = "prerolls"
getcustom("prerolls")
elif ("commercials" in str(sys.argv)):
tpe = "commercials"
getcustom("commercials")
elif ("custom." in str(sys.argv)):
item = str(sys.argv[1])
getcustom(item)
elif("all" in str(sys.argv).lower()):
tpe = "both"
startupactionmovie()
getmovies()
startupactiontv()
getshows()
except KeyboardInterrupt:
print ("Cancel request received. Restoring tables.")
if (("movie" in tpe) or ("both" in tpe)):
cmd = "python " + homedir + "system.py restoremoviedb"
os.system(cmd)
elif (("show" in tpe) or ("both" in tpe)):
cmd = "python " + homedir + "system.py restoretvdb"
os.system(cmd)
print("Cancelled.")