-
Notifications
You must be signed in to change notification settings - Fork 3
/
pum.py
2025 lines (1832 loc) · 90.8 KB
/
pum.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
# **********************************************************************************************************************
# *************************************************** imports **********************************************************
# **********************************************************************************************************************
import tkinter
from tkinter import *
from tkinter import messagebox
from tkinter import ttk
from configparser import ConfigParser
from tkinter.messagebox import askyesno
import mysql.connector, os, sys, time
from plexapi.server import PlexServer
import requests
import plexapi
import glob
import json
import threading
from config import *
# **********************************************************************************************************************
# ********************************************* GUI settings ***********************************************************
# **********************************************************************************************************************
root = Tk()
root.title('Plex User Manager')
root.geometry("1100x800")
root.configure(bg="#282828")
style = ttk.Style(root)
style.theme_create("pum_theme", parent="alt", settings={
"TNotebook": {"configure": {"tabmargins": [2, 5, 2, 0],
"tabposition": 'ne',
"background": "#1F1F1F",
"borderwidth": "0",
# "font": "Helvetica 10 bold",
}},
"TNotebook.Tab": {
"configure": {"padding": [30, 1],
"background": "#1F1F1F",
"foreground": "white",
"borderwidth": "0",
"fieldbackground": "#1F1F1F"},
"map": {"background": [("selected", "#383838")],
"foreground": [('selected', "#e5a00d")],
"borderwidth": "0"}}})
style.theme_use("pum_theme")
# set tabs
notebook = ttk.Notebook(root, style='TNotebook')
# user panel
user_tab: Frame = Frame(notebook) # frame for user page
notebook.add(user_tab, text="users")
user_tab.configure(background="#1F1F1F")
# server panel
server_tab: Frame = Frame(notebook) # frame for server page
notebook.add(server_tab, text="servers")
server_tab.configure(background="#1F1F1F")
# setting panel
conf_tab: Frame = Frame(notebook) # frame for conf page
notebook.add(conf_tab, text="Settings")
conf_tab.configure(background="#1F1F1F")
# help & info panel
help_and_info_tab: Frame = Frame(notebook) # frame for help & info page
notebook.add(help_and_info_tab, text="Help & info")
help_and_info_tab.configure(background="#1F1F1F")
notebook.pack(expand=True, fill="both") # expand to space not used
# Configure treeview header color
style.configure('Treeview.Heading',
background="#1F1F1F",
font="Helvetica 10 bold",
foreground="grey")
# Configure the Treeview Colors
style.configure("Treeview",
background="#282828",
foreground="white",
rowheight=25,
relief="flat",
borderwidth=0,
fieldbackground="#282828")
# Change Selected Color
style.map('Treeview',
background=[('selected', "#383838")],
#font=[('selected', "bold")],
foreground=[('selected', "#e5a00d")])
# **********************************************************************************************************************
# ************************************************* VARIABLES **********************************************************
# **********************************************************************************************************************
# var for new servers
NEW_PLEX_SERVER = ""
"""
# global config_path
config_path = ".config/"
api_path = "api/"
# Read config.ini file
config_object = ConfigParser()
config_object.read(config_path + "pum.ini")
# Get the conf info
userinfo = config_object["CONF"]
warn_user_near_expiration = userinfo["warn_user_near_expiration"]
warn_user_near_expiration_delay = userinfo["warn_user_near_expiration_delay"]
warn_user_account_expiration = userinfo["warn_user_account_expiration"]
remove_user_access = userinfo["remove_user_access"]
delete_user = userinfo["delete_user"]
delete_user_delay_str = userinfo["delete_user_delay"]
plex_db_sync_str = userinfo["plex_db_sync"]
hide_guest_str = userinfo["hide_guest"]
hide_no_lib_users_str = userinfo["hide_no_lib_users"]
# cron_conf_str = userinfo["cron_conf"]
nbr_backup_to_keep_str = userinfo["nbr_backup_to_keep"]
sync_plex_delai = userinfo["sync_plex_delai"]
# DB connection
# Read config.ini file
config_object = ConfigParser()
config_object.read(config_path + "pum.ini")
# Get the conf info
userinfo = config_object["DATABASE"]
db_host = userinfo["host"]
db_user = userinfo["user"]
db_passwd = userinfo["passwd"]
db_db = userinfo["db"]
"""
# connect to MySQL
mydb = mysql.connector.connect(
host=db_host,
user=db_user,
passwd=db_passwd,
database=db_db,
auth_plugin='mysql_native_password')
# Create a cursor and initialize it
cursor = mydb.cursor()
# **********************************************************************************************************************
# ************************************************* DB create **********************************************************
# **********************************************************************************************************************
def db_create():
# Create database if not exists
cursor.execute("CREATE DATABASE IF NOT EXISTS pum")
# Create table plexusers
cursor.execute("CREATE TABLE IF NOT EXISTS plexusers(first_name VARCHAR(255), \
last_name VARCHAR(255), \
username VARCHAR(255), \
email VARCHAR(255), \
serverName VARCHAR(255) NOT NULL, \
account_expire_date DATE, \
sections VARCHAR(255), \
allowSync VARCHAR(10), \
camera VARCHAR(10), \
channels VARCHAR(10), \
filterMovies VARCHAR(255) DEFAULT NULL, \
filterMusic VARCHAR(255) DEFAULT NULL, \
filterTelevision VARCHAR(255) DEFAULT NULL, \
title VARCHAR(255), \
is_on_plex INT(10), \
account_creation_date DATE, \
account_renewed_date DATE, \
userID INT NOT NULL, \
description VARCHAR(255), \
hidden INT(10) DEFAULT 0, \
PRIMARY KEY(userID, serverName) );")
# Create table tempusers
cursor.execute("CREATE TABLE IF NOT EXISTS tempusers(serverName VARCHAR(255) NOT NULL, \
userID INT NOT NULL, \
PRIMARY KEY(userID, serverName) );")
# Create table plexservers
cursor.execute("CREATE TABLE IF NOT EXISTS plexservers(serverName VARCHAR(255), \
token VARCHAR(255) NOT NULL, \
url VARCHAR(255) NOT NULL, \
server_offline INT(10) default NULL,\
PRIMARY KEY(url, token) );")
# Create table plexlibraries
cursor.execute("CREATE TABLE IF NOT EXISTS plexlibraries(serverName VARCHAR(255) NOT NULL, \
email VARCHAR(255) NOT NULL, \
library VARCHAR(255) NOT NULL, \
PRIMARY KEY(library, serverName, email) );")
# Create table plexfilterMovies
cursor.execute("CREATE TABLE IF NOT EXISTS plexfilterMovies(serverName VARCHAR(255) NOT NULL, \
email VARCHAR(255) NOT NULL, \
filterMovies VARCHAR(255) NOT NULL, \
PRIMARY KEY(filterMovies, serverName, email) );")
# Create table plexfilterMusic
cursor.execute("CREATE TABLE IF NOT EXISTS plexfilterMusic(serverName VARCHAR(255) NOT NULL, \
email VARCHAR(255) NOT NULL, \
filterMusic VARCHAR(255) NOT NULL, \
PRIMARY KEY(filterMusic, serverName, email) );")
# Create table plexfilterTelevision
cursor.execute("CREATE TABLE IF NOT EXISTS plexfilterTelevision(serverName VARCHAR(255) NOT NULL, \
email VARCHAR(255) NOT NULL, \
filterTelevision VARCHAR(255) NOT NULL, \
PRIMARY KEY(filterTelevision, serverName, email) );")
# **********************************************************************************************************************
# *********************************************** import data **********************************************************
# **********************************************************************************************************************
def import_data():
global NEW_PLEX_SERVER, NEW_PLEX_URL, NEW_PLEX_TOKEN
root.title('Plex User Manager... loading data...')
"""
config_path = ".config/"
api_path = "api/"
# DB connection
# Read config.ini file
config_object = ConfigParser()
config_object.read(config_path + "pum.ini")
# Get the conf info
userinfo = config_object["DATABASE"]
db_host = userinfo["host"]
db_user = userinfo["user"]
db_passwd = userinfo["passwd"]
db_db = userinfo["db"]
# Read config.ini file
config_object = ConfigParser()
config_object.read(config_path + "pum.ini")
# Get the conf info
userinfo = config_object["CONF"]
remove_user_access = userinfo["remove_user_access"]
"""
# connect to MySQL
mydb = mysql.connector.connect(
host=db_host,
user=db_user,
passwd=db_passwd,
database=db_db,
auth_plugin='mysql_native_password')
# Create a cursor and initialize it
cursor = mydb.cursor()
# clean old entries
cursor.execute(
"SELECT * FROM plexfilterMovies WHERE NOT EXISTS(SELECT NULL FROM plexusers WHERE plexusers.email = plexfilterMovies.email);")
deleted_filterMovies = cursor.fetchall()
for delet_filtMov in deleted_filterMovies:
cursor.execute("DELETE FROM plexfilterMovies WHERE serverName = %s AND email = %s AND library = %s;",
[delet_filtMov[0], delet_filtMov[1], delet_filtMov[2]])
print("Old entry : filterMovies " + delet_filtMov[2] + " has been removed on server " + delet_filtMov[0] + " for user " +
delet_filtMov[1])
cursor.execute(
"SELECT * FROM plexfilterMusic WHERE NOT EXISTS(SELECT NULL FROM plexusers WHERE plexusers.email = plexfilterMusic.email);")
deleted_filterMusic = cursor.fetchall()
for delet_filtMus in deleted_filterMusic:
cursor.execute("DELETE FROM plexfilterMusic WHERE serverName = %s AND email = %s AND library = %s;",
[delet_filtMus[0], delet_filtMus[1], delet_filtMus[2]])
print("Old entry : filterMusic " + delet_filtMus[2] + " has been removed on server " + delet_filtMus[0] + " for user " +
delet_filtMus[1])
cursor.execute(
"SELECT * FROM plexfilterTelevision WHERE NOT EXISTS(SELECT NULL FROM plexusers WHERE plexusers.email = plexfilterTelevision.email);")
deleted_filterTelevision = cursor.fetchall()
for delet_filtTV in deleted_filterTelevision:
cursor.execute("DELETE FROM plexfilterTelevision WHERE serverName = %s AND email = %s AND library = %s;",
[delet_filtTV[0], delet_filtTV[1], delet_filtTV[2]])
print("Old entry : filterTelevision " + delet_filtTV[2] + " has been removed on server " + delet_filtTV[0] + " for user " + delet_filtTV[1])
cursor.execute(
"SELECT * FROM plexlibraries WHERE NOT EXISTS(SELECT NULL FROM plexusers WHERE plexusers.email = plexlibraries.email);")
deleted_libraries = cursor.fetchall()
for delet_lib in deleted_libraries:
cursor.execute("DELETE FROM plexlibraries WHERE serverName = %s AND email = %s AND library = %s;", [delet_lib[0], delet_lib[1], delet_lib[2]])
print("Old entry : library " + delet_lib[2] + " has been removed on server " + delet_lib[0] + " for user " + delet_lib[1])
# remove user access if expired
if remove_user_access == "1":
cursor.execute("SELECT DISTINCT plexusers.email, plexusers.serverName FROM plexusers, plexlibraries WHERE plexusers.account_expire_date < CURDATE() AND plexusers.email = plexlibraries.email AND plexusers.serverName = plexlibraries.serverName;")
expired_user = cursor.fetchall()
print("following users will have access removed:")
print(expired_user)
for exp_usr in expired_user:
#print("deleting: " + exp_usr)
usr_job = expired_user[0]
#print(expired_user[0])
"""
config_path = ".config/"
# DB connection
# Read config.ini file
config_object = ConfigParser()
config_object.read(config_path + "pum.ini")
# Get the conf info
userinfo = config_object["DATABASE"]
db_host = userinfo["host"]
db_user = userinfo["user"]
db_passwd = userinfo["passwd"]
db_db = userinfo["db"]
"""
# connect to MySQL
mydb = mysql.connector.connect(
host=db_host,
user=db_user,
passwd=db_passwd,
database=db_db,
auth_plugin='mysql_native_password')
# Create a cursor and initialize it
cursor = mydb.cursor()
# get server token and url
cursor.execute("SELECT * FROM plexservers WHERE serverName = %s;", [usr_job[1]])
selected_plex_info = cursor.fetchall()
# print(type(selected_plex_info))
new_selected_plex_info = selected_plex_info[0]
PLEX_URL = str(new_selected_plex_info[2])
PLEX_TOKEN = str(new_selected_plex_info[1])
# write to plex_api config.ini
plex_config_object = ConfigParser()
plex_config_object.read(plexapi.CONFIG_PATH)
plex_config_object['auth']['server_baseurl'] = PLEX_URL
plex_config_object['auth']['server_token'] = PLEX_TOKEN
with open(plexapi.CONFIG_PATH, 'w') as plex_configfile:
plex_config_object.write(plex_configfile)
# export json from plex
os.system("python3 plex_api_share.py --unshare --user " + usr_job[0])
cursor.execute("UPDATE plexusers SET sections = 'NULL' WHERE email = %s AND serverName = %s;", [usr_job[0], usr_job[1]])
print("unshare all libraries for user " + usr_job[0] + " on server " + usr_job[1])
cursor.execute("DELETE FROM plexlibraries WHERE email = %s AND serverName = %s;", [usr_job[0], usr_job[1]])
if NEW_PLEX_SERVER:
records = [NEW_PLEX_SERVER, NEW_PLEX_URL, NEW_PLEX_TOKEN]
else:
cursor.execute("SELECT * FROM plexservers;")
records = cursor.fetchall()
for record in records:
#serverName = record[0]
PLEX_TOKEN = record[1]
PLEX_URL = record[2]
sess = requests.Session()
# Ignore verifying the SSL certificate
sess.verify = False # '/path/to/certfile'
# If verify is set to a path to a directory,
# the directory must have been processed using the c_rehash utility supplied
# with OpenSSL.
if sess.verify is False:
# Disable the warning that the request is insecure, we know that...
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
# test if server online
try:
plex = PlexServer(PLEX_URL, PLEX_TOKEN, session=sess)
except:
cursor.execute("UPDATE plexservers SET server_offline = 1 WHERE token = %s AND url = %s;",
[PLEX_TOKEN, PLEX_URL])
#return
else:
# if the name has changed then update
plex = PlexServer(PLEX_URL, PLEX_TOKEN, session=sess)
if record[0] != plex.friendlyName:
cursor.execute("UPDATE plexservers SET serverName = %s WHERE token = %s AND url = %s;", [plex.friendlyName, PLEX_TOKEN, PLEX_URL])
# server is online
cursor.execute("UPDATE plexservers SET server_offline = 0 WHERE token = %s AND url = %s;", [PLEX_TOKEN, PLEX_URL])
serverName = plex.friendlyName
#print(serverName)
#print(PLEX_TOKEN)
#print(PLEX_URL)
#print(PLEX_TOKEN)
plex_config_object = ConfigParser()
plex_config_object.read(plexapi.CONFIG_PATH)
plex_config_object['auth']['server_baseurl'] = PLEX_URL
plex_config_object['auth']['server_token'] = PLEX_TOKEN
with open(plexapi.CONFIG_PATH, 'w') as plex_configfile:
plex_config_object.write(plex_configfile)
# export json from plex
os.system("python3 plex_api_share.py --backup")
print(serverName + " json created")
NEW_PLEX_SERVER = ""
# empty table tempusers
cursor.execute("TRUNCATE TABLE tempusers;")
# import user data
path_to_json = glob.glob(r'./*.json')
# check json file
if not path_to_json:
print("No JSON files... ")
#quit()
# loop for every json file
for jfile in path_to_json:
# load json
json_data = open(jfile).read()
json_obj = json.loads(json_data)
# do validation and checks before insert
def validate_string(val):
if val != None:
if type(val) is int:
# for x in val:
# print(x)
return str(val).encode('utf-8')
else:
return val
# remove not wanted characters
for mydict in json_obj:
# check if filterMovies exist and put it in DB
if mydict['filterMovies']:
filtermovie_entrie = mydict['filterMovies']
cursor.execute("DELETE FROM plexfilterMovies WHERE email = %s AND serverName = %s;",
[mydict['email'], mydict['serverName']])
for key in filtermovie_entrie['label']:
cursor.execute("INSERT INTO plexfilterMovies SET filterMovies = %s, email = %s, serverName = %s;",
[key, mydict['email'], mydict['serverName']])
# check if filterMusic exist and put it in DB
if mydict['filterMusic']:
filterMusic_entrie = mydict['filterMusic']
cursor.execute("DELETE FROM plexfilterMusic WHERE email = %s AND serverName = %s;",
[mydict['email'], mydict['serverName']])
for key in filterMusic_entrie['label']:
cursor.execute("INSERT INTO plexfilterMusic SET filterMusic = %s, email = %s, serverName = %s;",
[key, mydict['email'], mydict['serverName']])
# check if filterTelevision exist and put it in DB
if mydict['filterTelevision']:
filterTelevision_entrie = mydict['filterTelevision']
cursor.execute("DELETE FROM plexfilterTelevision WHERE email = %s AND serverName = %s;",
[mydict['email'], mydict['serverName']])
for key in filterTelevision_entrie['label']:
cursor.execute(
"INSERT INTO plexfilterTelevision SET filterTelevision = %s, email = %s, serverName = %s;",
[key, mydict['email'], mydict['serverName']])
# check if sections exist and put it in DB
if mydict['sections']:
sections_entrie = mydict['sections']
#print(sections_entrie)
cursor.execute("DELETE FROM plexlibraries WHERE email = %s AND serverName = %s;",
[mydict['email'], mydict['serverName']])
for key in sections_entrie:
cursor.execute(
"INSERT INTO plexlibraries SET library = %s, email = %s, serverName = %s;",
[key, mydict['email'], mydict['serverName']])
# create or update user
#print(mydict['userID'])
#print(mydict['serverName'])
#print(mydict['allowSync'])
#print(mydict['camera'])
#print(mydict['channels'])
#print(mydict['email'])
#print(mydict['title'])
#print(mydict['username'])
cursor.execute("INSERT IGNORE INTO plexusers SET userID = %s, account_creation_date = CURDATE(), serverName = %s;", [mydict['userID'], mydict['serverName']])
cursor.execute("UPDATE plexusers SET allowSync = %s, camera = %s, channels = %s, email = %s, title = %s, username = %s, is_on_plex = 1 WHERE userID = %s AND serverName = %s;", [
mydict['allowSync'], mydict['camera'], mydict['channels'], mydict['email'], mydict['title'], mydict['username'], mydict['userID'], mydict['serverName']])
# add to temp db to compare
cursor.execute("INSERT IGNORE INTO tempusers SET userID = %s, serverName = %s;", [mydict['userID'], mydict['serverName']])
print("json files imported to db")
# delete old json file
app_dir = os.listdir(os.getcwd())
for json_item in app_dir:
if json_item.endswith(".json"):
os.remove(os.path.join(os.getcwd(), json_item))
# compare tempusers and plexusers and remove old user
cursor.execute("DELETE FROM plexusers WHERE is_on_plex = 0 AND userID NOT in (SELECT userID FROM tempusers) AND serverName IN (SELECT serverName FROM tempusers);")
# Commit changes
mydb.commit()
# Close connexion
mydb.close()
'''# clear my_server_tree
my_user_tree.delete(*my_user_tree.get_children())
# get server data back
query_user_info()'''
# clear my_server_tree
my_user_tree.delete(*my_user_tree.get_children())
# get server data back
root.title('Plex User Manager')
query_user_info()
multithreading_sync_data()
print("All data imported")
def multithreading_import_data():
thread_import_data = threading.Thread(target=import_data, name="import data")
thread_import_data.start()
def sync_data():
#time.sleep(86400)
time.sleep(int(sync_plex_delai)*3600)
multithreading_import_data()
print("waited for " + sync_plex_delai + " Hours, data synced")
# clear my_server_tree
my_user_tree.delete(*my_user_tree.get_children())
# get server data back
query_user_info()
def multithreading_sync_data():
thread_sync_data = threading.Thread(target=sync_data, name="sync data")
thread_sync_data.daemon = True
thread_sync_data.start()
db_create()
# check if server is configured to sync
cursor.execute("SELECT serverName FROM plexservers;")
records = cursor.fetchall()
if records:
multithreading_import_data()
else:
tkinter.messagebox.showinfo(title="Server missing", message="please add server in server panel")
# **********************************************************************************************************************
# ********************************************* user panel *************************************************************
# **********************************************************************************************************************
# missing if server_offline == 1 then do not update user
def query_user_info():
# DB connection
"""
# Read config.ini file
config_object = ConfigParser()
config_object.read(config_path + "pum.ini")
# Get the conf info
userinfo = config_object["DATABASE"]
db_host = userinfo["host"]
db_user = userinfo["user"]
db_passwd = userinfo["passwd"]
db_db = userinfo["db"]
"""
# connect to MySQL
mydb = mysql.connector.connect(
host=db_host,
user=db_user,
passwd=db_passwd,
database=db_db,
auth_plugin='mysql_native_password')
# Create a cursor and initialize it
cursor = mydb.cursor()
# Select info from db
if hide_guest_str == "1" and hide_no_lib_users_str != "1":
cursor.execute("SELECT * FROM plexusers WHERE title != 'Guest';")
if hide_no_lib_users_str == "1" and hide_guest_str != "1":
#cursor.execute("SELECT * FROM plexusers WHERE hidden != '1';")
cursor.execute("SELECT DISTINCT plexusers.* FROM plexusers, plexlibraries WHERE plexusers.email = plexlibraries.email AND plexusers.serverName = plexlibraries.serverName;")
if hide_no_lib_users_str == "1" and hide_guest_str == "1":
cursor.execute("SELECT DISTINCT plexusers.* FROM plexusers, plexlibraries WHERE plexusers.email = plexlibraries.email AND plexusers.serverName = plexlibraries.serverName AND plexusers.title != 'Guest';")
if hide_no_lib_users_str != "1" and hide_guest_str != "1":
cursor.execute("SELECT * FROM plexusers;")
records = cursor.fetchall()
# Add our data to the screen
global count
count = 0
for record in records:
if count % 2 == 0:
my_user_tree.insert(parent='', index='end', iid=str(count), text='',
values=(record[0], record[1], record[2], record[3], record[4], record[5], record[6], record[7], record[8], record[9], record[10], record[11], record[12], record[13], record[14], record[15], record[16], record[17], record[18]),
tags=('evenrow',))
else:
my_user_tree.insert(parent='', index='end', iid=str(count), text='',
values=(record[0], record[1], record[2], record[3], record[4], record[5], record[6], record[7], record[8], record[9], record[10], record[11], record[12], record[13], record[14], record[15], record[16], record[17], record[18]),
tags=('oddrow',))
# increment counter
count += 1
# Commit changes
mydb.commit()
# Close connexion
mydb.close()
# Create a Treeview Frame
user_tree_frame = Frame(user_tab)
user_tree_frame.configure(background="#1F1F1F")
user_tree_frame.pack(fill="x", expand=1, pady=10)
# Create a Treeview Scrollbar
user_tree_scroll = Scrollbar(user_tree_frame)
user_tree_scroll.configure(background="#1F1F1F")
user_tree_scroll.pack(side=RIGHT, fill=Y)
# Create The Treeview
my_user_tree = ttk.Treeview(user_tree_frame, yscrollcommand=user_tree_scroll.set, selectmode="extended")
my_user_tree.pack(fill="x")
# Configure the Scrollbar
user_tree_scroll.config(command=my_user_tree.yview)
# Define Our Columns
my_user_tree['columns'] = ("First Name", "Last Name", "Username", "email", "server_name", "expire_date") #, "libraries")
# Format Our Columns
my_user_tree.column("#0", width=0, stretch=NO)
my_user_tree.column("First Name", anchor=W, width=130)
my_user_tree.column("Last Name", anchor=W, width=140)
my_user_tree.column("Username", anchor=CENTER, width=180)
my_user_tree.column("email", anchor=CENTER, width=260)
my_user_tree.column("server_name", anchor=CENTER, width=160)
my_user_tree.column("expire_date", anchor=CENTER, width=120)
#my_user_tree.column("libraries", anchor=CENTER, width=140) 40
# Create Headings
my_user_tree.heading("#0", text="", anchor=W)
my_user_tree.heading("First Name", text="First Name", anchor=W)
my_user_tree.heading("Last Name", text="Last Name", anchor=W)
my_user_tree.heading("Username", text="Username", anchor=CENTER)
my_user_tree.heading("email", text="email", anchor=CENTER)
my_user_tree.heading("server_name", text="server name", anchor=CENTER)
my_user_tree.heading("expire_date", text="expire date", anchor=CENTER)
#my_user_tree.heading("libraries", text="libraries", anchor=CENTER)
# Select record
def select_user_record(e):
config_path = ".config/"
api_path = "api/"
# DB connection
"""
# Read config.ini file
config_object = ConfigParser()
config_object.read(config_path + "pum.ini")
# Get the conf info
userinfo = config_object["DATABASE"]
db_host = userinfo["host"]
db_user = userinfo["user"]
db_passwd = userinfo["passwd"]
db_db = userinfo["db"]
"""
# connect to MySQL
mydb = mysql.connector.connect(
host=db_host,
user=db_user,
passwd=db_passwd,
database=db_db,
auth_plugin='mysql_native_password')
# Create a cursor and initialize it
cursor = mydb.cursor()
# Clear entry boxes
# mettre state en disabled pour avoir les case grise, mais napplique plus le entry
first_name_entry.delete(0,END)
last_name_entry.delete(0, END)
username_entry.config(disabledbackground="#282828",
disabledforeground="white",
state="normal")
username_entry.delete(0, END)
email_entry.config(disabledbackground="#282828",
disabledforeground="white",
state="normal")
email_entry.delete(0, END)
serverName_entry.config(disabledbackground="#282828",
disabledforeground="white",
state="normal")
serverName_entry.delete(0, END)
account_expire_date_entry.delete(0, END)
sections_entry.config(disabledbackground="#282828",
disabledforeground="white",
state="normal")
sections_entry.delete(0, END)
allowSync_entry.config(disabledbackground="#282828",
disabledforeground="white",
state="normal")
allowSync_entry.delete(0, END)
camera_entry.config(disabledbackground="#282828",
disabledforeground="white",
state="normal")
camera_entry.delete(0, END)
channels_entry.config(disabledbackground="#282828",
disabledforeground="white",
state="normal")
channels_entry.delete(0, END)
filterMovies_entry.config(disabledbackground="#282828",
disabledforeground="white",
state="normal")
filterMovies_entry.delete(0, END)
filterMusic_entry.config(disabledbackground="#282828",
disabledforeground="white",
state="normal")
filterMusic_entry.delete(0, END)
filterTelevision_entry.config(disabledbackground="#282828",
disabledforeground="white",
state="normal")
filterTelevision_entry.delete(0, END)
title_entry.config(disabledbackground="#282828",
disabledforeground="white",
state="normal")
title_entry.delete(0, END)
is_on_plex_entry.config(disabledbackground="#282828",
disabledforeground="white",
state="normal")
is_on_plex_entry.delete(0, END)
userID_entry.config(disabledbackground="#282828",
disabledforeground="white",
state="normal")
userID_entry.delete(0, END)
account_creation_date_entry.delete(0, END)
account_renewed_date_entry.delete(0, END)
#userID_entry.delete(0, END)
description_entry.delete(0, END)
# Grab record number
selected = my_user_tree.focus()
# Grab record values
values = my_user_tree.item(selected, 'values')
# print(values)
# Output entry boxes
try:
first_name_entry.insert(0, values[0])
last_name_entry.insert(0, values[1])
username_entry.insert(0, values[2])
username_entry.config(state="disabled",
disabledbackground="#282828")
email_entry.insert(0, values[3])
email_entry.config(state="disabled",
disabledbackground="#282828")
serverName_entry.insert(0, values[4])
serverName_entry.config(state="disabled",
disabledbackground="#282828")
account_expire_date_entry.insert(0, values[5])
#print("user sections unknown: " + values[6])
# get the libraries for selected user
cursor.execute("SELECT library FROM plexlibraries WHERE email = %s AND serverName = %s;", [values[3], values[4]])
user_libraries = cursor.fetchall()
# display libraries in clean way
for libraries in user_libraries:
sections_entry.insert(0, libraries[0] + " - ")
sections_entry.config(state="disabled",
disabledbackground="#282828")
allowSync_entry.insert(0, values[7])
allowSync_entry.config(state="disabled",
disabledbackground="#282828")
camera_entry.insert(0, values[8])
camera_entry.config(state="disabled",
disabledbackground="#282828")
channels_entry.insert(0, values[9])
channels_entry.config(state="disabled",
disabledbackground="#282828")
# get the filterMovies for selected user
cursor.execute("SELECT filterMovies FROM plexfilterMovies WHERE email = %s AND serverName = %s;",
[values[3], values[4]])
user_filterMovies = cursor.fetchall()
# display filterMovies in clean way
for filterMovies in user_filterMovies:
filterMovies_entry.insert(0, filterMovies[0] + " - ")
filterMovies_entry.config(state="disabled",
disabledbackground="#282828")
# get the filterMusic for selected user
cursor.execute("SELECT filterMusic FROM plexfilterMusic WHERE email = %s AND serverName = %s;",
[values[3], values[4]])
user_filterMusic = cursor.fetchall()
# display filterMovies in clean way
for filterMusic in user_filterMusic:
filterMusic_entry.insert(0, filterMusic[0] + " - ")
filterMusic_entry.config(state="disabled",
disabledbackground="#282828")
# get the filterTelevision for selected user
cursor.execute("SELECT filterTelevision FROM plexfilterTelevision WHERE email = %s AND serverName = %s;",
[values[3], values[4]])
user_filterTelevision = cursor.fetchall()
# display filterTelevision in clean way
for filterTelevision in user_filterTelevision:
filterTelevision_entry.insert(0, filterTelevision[0] + " - ")
filterTelevision_entry.config(state="disabled",
disabledbackground="#282828")
title_entry.insert(0, values[13])
title_entry.config(state="disabled",
disabledbackground="#282828")
is_on_plex_entry.insert(0, values[14])
is_on_plex_entry.config(state="disabled",
disabledbackground="#282828")
account_creation_date_entry.insert(0, values[15])
account_renewed_date_entry.insert(0, values[16])
userID_entry.insert(0, values[17])
userID_entry.config(state="disabled",
disabledbackground="#282828")
description_entry.insert(0, values[18])
# keep values to avoid request if identical
global old_account_creation_date_entry
old_account_creation_date_entry = values[15]
global old_account_renewed_date_entry
old_account_renewed_date_entry = values[16]
global old_account_expire_date_entry
old_account_expire_date_entry = values[5]
# Commit changes
mydb.commit()
# Close connexion
mydb.close()
except:
pass
# Update Record
def update_user_record():
config_path = ".config/"
api_path = "api/"
# DB connection
"""
# Read config.ini file
config_object = ConfigParser()
config_object.read(config_path + "pum.ini")
# Get the conf info
userinfo = config_object["DATABASE"]
db_host = userinfo["host"]
db_user = userinfo["user"]
db_passwd = userinfo["passwd"]
db_db = userinfo["db"]
"""
# connect to MySQL
mydb = mysql.connector.connect(
host=db_host,
user=db_user,
passwd=db_passwd,
database=db_db,
auth_plugin='mysql_native_password')
# Create a cursor and initialize it
cursor = mydb.cursor()
# Grab the record number
selected = my_user_tree.focus()
# Update record
my_user_tree.item(selected, text="", values=(
first_name_entry.get(), last_name_entry.get(), username_entry.get(), email_entry.get(), serverName_entry.get(),
account_expire_date_entry.get(), sections_entry.get(), allowSync_entry.get(), camera_entry.get(),
channels_entry.get(), filterMovies_entry.get(), filterMusic_entry.get(), filterTelevision_entry.get(),
title_entry.get(), is_on_plex_entry.get(), account_creation_date_entry.get(), account_renewed_date_entry.get(),
userID_entry.get(), description_entry.get(),))
if account_creation_date_entry.get() != "None" and account_creation_date_entry.get() != old_account_creation_date_entry:
cursor.execute("UPDATE plexusers SET account_creation_date = %s WHERE userID = %s;",
[
account_creation_date_entry.get(),
userID_entry.get()
])
if account_renewed_date_entry.get() != "None" and account_renewed_date_entry.get() != old_account_renewed_date_entry:
cursor.execute("UPDATE plexusers SET account_renewed_date = %s WHERE userID = %s AND serverName = %s;",
[
account_renewed_date_entry.get(),
userID_entry.get(),
serverName_entry.get()
])
if account_expire_date_entry.get() != "None" and account_expire_date_entry.get() != old_account_expire_date_entry:
cursor.execute("UPDATE plexusers SET account_expire_date = %s WHERE userID = %s AND serverName = %s;",
[
account_expire_date_entry.get(),
userID_entry.get(),
serverName_entry.get()
])
cursor.execute("UPDATE plexusers SET first_name = %s, last_name = %s, description = %s WHERE email = %s;",
[
first_name_entry.get(),
last_name_entry.get(),
description_entry.get(),
email_entry.get()
])
# Commit changes
mydb.commit()
# Close connexion
mydb.close()
# delete user
def delete_user():
# DB connection
"""
# Read config.ini file
config_object = ConfigParser()
config_object.read(config_path + "pum.ini")
# Get the conf info
userinfo = config_object["DATABASE"]
db_host = userinfo["host"]
db_user = userinfo["user"]
db_passwd = userinfo["passwd"]
db_db = userinfo["db"]
"""
# connect to MySQL
mydb = mysql.connector.connect(
host=db_host,
user=db_user,
passwd=db_passwd,
database=db_db,
auth_plugin='mysql_native_password')
# Create a cursor and initialize it
cursor = mydb.cursor()
# Grab the record number
selected = my_user_tree.focus()
if not email_entry.get() == '':
# message box to be sure
delete_user_confirmation_box = askyesno("delete selected user", "you are going to delete " + email_entry.get() + " are you sure?")
#print(delete_user_confirmation_box)
# confirmation box
if delete_user_confirmation_box:
current_user_serverName = serverName_entry.get()
current_user_email = email_entry.get()
# delete user from db
cursor.execute("DELETE FROM plexusers WHERE email = %s;", [email_entry.get()])
print("user " + email_entry.get() + " deleted from DB")
first_name_entry.delete(0, END)
last_name_entry.delete(0, END)
username_entry.delete(0, END)
email_entry.config(disabledbackground="#282828",
disabledforeground="white",
state="normal")
email_entry.delete(0, END)
serverName_entry.config(disabledbackground="#282828",
disabledforeground="white",
state="normal")
serverName_entry.delete(0, END)
account_expire_date_entry.delete(0, END)
sections_entry.config(disabledbackground="#282828",
disabledforeground="white",
state="normal")
sections_entry.delete(0, END)
allowSync_entry.config(disabledbackground="#282828",
disabledforeground="white",
state="normal")
allowSync_entry.delete(0, END)
camera_entry.config(disabledbackground="#282828",
disabledforeground="white",
state="normal")
camera_entry.delete(0, END)
channels_entry.config(disabledbackground="#282828",
disabledforeground="white",
state="normal")
channels_entry.delete(0, END)
filterMovies_entry.config(disabledbackground="#282828",
disabledforeground="white",
state="normal")
filterMovies_entry.delete(0, END)
filterMusic_entry.config(disabledbackground="#282828",
disabledforeground="white",
state="normal")
filterMusic_entry.delete(0, END)
filterTelevision_entry.config(disabledbackground="#282828",
disabledforeground="white",
state="normal")
filterTelevision_entry.delete(0, END)
title_entry.config(disabledbackground="#282828",
disabledforeground="white",
state="normal")
title_entry.delete(0, END)
is_on_plex_entry.config(disabledbackground="#282828",
disabledforeground="white",
state="normal")
is_on_plex_entry.delete(0, END)
userID_entry.config(disabledbackground="#282828",
disabledforeground="white",
state="normal")
userID_entry.delete(0, END)
account_creation_date_entry.delete(0, END)
account_renewed_date_entry.delete(0, END)
# userID_entry.delete(0, END)
description_entry.delete(0, END)
# delete user from plex
try:
cursor.execute("SELECT * FROM plexservers WHERE serverName = %s;", [current_user_serverName])
delete_user_srv_data = cursor.fetchall()
#print(current_user_serverName)
#print(delete_user_srv_data)
#print(delete_user_srv_data[0])
for current_servers in delete_user_srv_data:
from plexapi.server import PlexServer
from requests import Session
CURRENT_PLEX_TOKEN = current_servers[1]
CURRENT_PLEX_URL = current_servers[2]
#print(current_servers[1])
#print(current_servers[2])
if CURRENT_PLEX_TOKEN == '' or CURRENT_PLEX_URL == '':
return
SESSION = Session()
# Ignore verifying the SSL certificate
SESSION.verify = False # '/path/to/certfile'
# If verify is set to a path to a directory,
# the directory must have been processed using the c_rehash utility supplied with OpenSSL.
if not SESSION.verify:
# Disable the warning that the request is insecure, we know that...
from urllib3 import disable_warnings
from urllib3.exceptions import InsecureRequestWarning
disable_warnings(InsecureRequestWarning)
SERVER = PlexServer(baseurl=CURRENT_PLEX_URL, token=CURRENT_PLEX_TOKEN, session=SESSION)
ACCOUNT = SERVER.myPlexAccount()
#PLEX_USERS = {user.id: user.title for user in ACCOUNT.users()}
#PLEX_USERS.update({int(ACCOUNT.id): ACCOUNT.title})
#print(ACCOUNT.id)
#print(ACCOUNT.title)
#print(ACCOUNT.users)
#print(current_user_email)
#print(PLEX_USERS)
ACCOUNT.removeFriend(current_user_email)
print("user " + current_user_email + " deleted from Plex")
except:
pass
# Commit changes
mydb.commit()
# Close connexion
mydb.close()