55from ui .ui_print import *
66
77name = 'Plex'
8- session = requests . Session ()
8+ session = custom_session ( get_rate_limit = 1.0 , post_rate_limit = 1.0 ) # 1 second between requests = 60 requests/minute
99users = []
1010headers = {'Content-Type' : 'application/json' , 'Accept' : 'application/json' }
1111current_library = []
@@ -28,7 +28,7 @@ def logerror(response):
2828 else :
2929 ui_print ("plex error: (401 unauthorized): token for user '" + name + "' does not seem to work. check your plex user settings." )
3030
31- def get (url , timeout = 60 ):
31+ def get (session : requests . Session , url : str , timeout = 60 ):
3232 try :
3333 response = session .get (url , headers = headers , timeout = timeout )
3434 logerror (response )
@@ -38,7 +38,7 @@ def get(url, timeout=60):
3838 ui_print ("plex error: (json exception): " + str (e ), debug = ui_settings .debug )
3939 return None
4040
41- def post (url , data ):
41+ def post (session : requests . Session , url : str , data ):
4242 try :
4343 response = session .post (url , data = data , headers = headers )
4444 logerror (response )
@@ -346,7 +346,7 @@ def setup(cls, new=False):
346346 working = False
347347 while not working :
348348 try :
349- response = get (library .url + '/library/sections/?X-Plex-Token=' + users [0 ][1 ])
349+ response = get (session , library .url + '/library/sections/?X-Plex-Token=' + users [0 ][1 ])
350350 working = True
351351 if len (response .MediaContainer .Directory ) == 0 :
352352 print ("It looks like this server does not have any libraries set-up! Please open the plex webui, setup at least one library and point it to your mounted debrid service drive." )
@@ -480,7 +480,7 @@ def call(paths):
480480 while refreshing :
481481 refreshing = False
482482 url = library .url + '/library/sections/?X-Plex-Token=' + users [0 ][1 ]
483- response = get (url )
483+ response = get (session , url )
484484 for section_ in response .MediaContainer .Directory :
485485 if section_ .refreshing :
486486 refreshing = True
@@ -501,7 +501,7 @@ def __new__(cls, element):
501501 names = []
502502 element_type = ("show" if element .type in ["show" ,"season" ,"episode" ] else "movie" )
503503 url = library .url + '/library/sections/?X-Plex-Token=' + users [0 ][1 ]
504- response = get (url )
504+ response = get (session , url )
505505 paths = []
506506 for section_ in response .MediaContainer .Directory :
507507 if section_ .key in library .refresh .sections and element_type == section_ .type :
@@ -547,7 +547,7 @@ def setup(cls, new=False):
547547 working = False
548548 while not working :
549549 try :
550- response = get (library .url + '/library/sections/?X-Plex-Token=' + users [0 ][1 ])
550+ response = get (session , library .url + '/library/sections/?X-Plex-Token=' + users [0 ][1 ])
551551 working = True
552552 if len (response .MediaContainer .Directory ) == 0 :
553553 print ("It looks like this server does not have any libraries set-up! Please open the plex webui, setup at least one library and point it to your mounted debrid service drive." )
@@ -595,7 +595,7 @@ def call(element):
595595 url = library .url + '/library/sections/' + str (library_item .librarySectionID ) + '/all?type=' + type_string + '&id=' + library_item .ratingKey + '&label.locked=1' + tags_string + '&X-Plex-Token=' + users [0 ][1 ]
596596 response = session .put (url ,headers = headers )
597597 url = library .url + '/library/metadata/' + library_item .ratingKey + '?X-Plex-Token=' + users [0 ][1 ]
598- response = get (url )
598+ response = get (session , url )
599599 library_item .__dict__ .update (response .MediaContainer .Metadata [0 ].__dict__ )
600600 except Exception as e :
601601 ui_print ("[plex] error: couldnt add labels! Turn on debug printing for more info." )
@@ -785,7 +785,7 @@ def __new__(self,silent=False):
785785 if library .check == [['' ]]:
786786 library .check = []
787787 try :
788- response = get (library .url + '/library/sections/?X-Plex-Token=' + users [0 ][1 ])
788+ response = get (session , library .url + '/library/sections/?X-Plex-Token=' + users [0 ][1 ])
789789 for Directory in response .MediaContainer .Directory :
790790 if ([Directory .key ] in library .check or library .check == []) and Directory .type in ["movie" ,"show" ]:
791791 types = ['1' ] if Directory .type == "movie" else ['2' , '3' , '4' ]
@@ -804,7 +804,7 @@ def __new__(self,silent=False):
804804 section_title = ''
805805 for type in types :
806806 url = library .url + '/library/sections/' + section + '/all?type=' + type + '&X-Plex-Token=' + users [0 ][1 ]
807- response = get (url )
807+ response = get (session , url )
808808 if hasattr (response , 'MediaContainer' ):
809809 if hasattr (response .MediaContainer , 'Metadata' ):
810810 for element in response .MediaContainer .Metadata :
@@ -860,7 +860,7 @@ def __new__(self,silent=False):
860860 if not item in current_library :
861861 updated = True
862862 url = library .url + '/library/metadata/' + item .ratingKey + '?X-Plex-Token=' + users [0 ][1 ]
863- response = get (url )
863+ response = get (session , url )
864864 item .__dict__ .update (response .MediaContainer .Metadata [0 ].__dict__ )
865865 else :
866866 match = next ((x for x in current_library if item == x ), None )
@@ -934,7 +934,7 @@ def match(self):
934934 service ,query = EID .split ('://' )
935935 query = '-' .join ([service ,query ])
936936 url = current_module .library .url + '/library/metadata/' + some_local_media .ratingKey + '/matches?manual=1&title=' + query + '&agent=' + agent + '&language=en-US&X-Plex-Token=' + users [0 ][1 ]
937- response = get (url )
937+ response = get (session , url )
938938 try :
939939 match = response .MediaContainer .SearchResult [0 ]
940940 if match .type == 'show' :
0 commit comments