@@ -80,8 +80,6 @@ def save_tmdb_key():
8080
8181@app .route ('/link_plex_account' , methods = ['POST' ])
8282def link_plex_account ():
83- print ("link_plex_account" )
84-
8583 try :
8684 headers = {'X-Plex-Client-Identifier' : app .config ['PLEX_CLIENT_IDENTIFIER' ]}
8785 pinlogin = MyPlexPinLogin (headers = headers , oauth = True )
@@ -96,17 +94,13 @@ def link_plex_account():
9694 resources = [resource for resource in plex_account .resources () if resource .owned and resource .connections ]
9795 servers = [f"{ resource .name } ({ resource .connections [0 ].address } )" for resource in resources if resource .connections ]
9896
99- print (f"servers: { servers } " )
100-
10197 # Store tokens in the dictionary
10298 for resource in resources :
10399 if resource .connections :
104100 server_name = f"{ resource .name } ({ resource .connections [0 ].address } )"
105101 tokens [server_name ] = pinlogin .token
106- print ("server name: " + server_name + " token: " + pinlogin .token )
107102 plex_data .add_token (server_name , pinlogin .token )
108103
109- print (f'Logged In As { username } ' )
110104 plex_data .set_servers (servers )
111105
112106 # Store the PlexAccountData object in the array
@@ -115,41 +109,38 @@ def link_plex_account():
115109 # Return the JSON response with servers and token
116110 return jsonify (servers = servers , token = pinlogin .token )
117111 else :
118- print ( 'Error' , 'Could not log in to Plex account' )
112+ return jsonify ({ 'message' : app . config [ 'RESPONSE_MESSAGES' ][ 'plex_account_error' ] , 'servers' : [], 'token' : None } )
119113 except Exception as e :
120- print ('Error' , f'Could not log in to Plex account: { str (e )} ' )
121-
122- # Return an empty JSON response if there was an error
123- return jsonify (servers = [], token = None )
114+ return jsonify ({'message' : app .config ['RESPONSE_MESSAGES' ]['plex_account_error' ], 'servers' : [], 'token' : None })
124115
125116@app .route ('/fetch_libraries/<serverName>' )
126117def fetch_libraries (serverName ):
127118 # Find the PlexAccountData object with the matching serverName
128119 plex_data = next ((data for data in plex_data_array if serverName in data .tokens ), None )
129120
130121 if plex_data is None :
131- print ("PlexAccountData not found" )
132- return jsonify ( error = "PlexAccountData not found" ), 404
122+ # print("PlexAccountData not found")
123+ return { 'message' : app . config [ 'RESPONSE_MESSAGES' ][ 'plex_data_not_found' ]}
133124
134125 token = plex_data .tokens .get (serverName )
135126
136- print ("Token: " + token )
127+ # print("Token: " + token)
137128 plex_account = MyPlexAccount (token = token )
138129
139130 server = None
140131 for resource in plex_account .resources ():
141132 if f"{ resource .name } ({ resource .connections [0 ].address } )" == serverName :
142- print (f"Attempting to connect to server { serverName } " )
133+ # print(f"Attempting to connect to server {serverName}")
143134 server = resource .connect ()
144135 break
145136
146137 if server is None :
147- print ("Server not found" )
138+ # print("Server not found")
148139 return jsonify (error = app .config ['RESPONSE_MESSAGES' ]['server_not_found' ]), 404
149140
150141 libraries = [section .title for section in server .library .sections ()]
151142
152- print (f"Libraries: { libraries } " )
143+ # print(f"Libraries: {libraries}")
153144
154145 plex_data .set_libraries (libraries )
155146
@@ -205,7 +196,7 @@ def save_plex_data():
205196def get_active_server ():
206197 try :
207198 global currentActiveServer
208- print (f"get_active_server libraries: { currentActiveServer .libraries } " )
199+ # print(f"get_active_server libraries: {currentActiveServer.libraries}")
209200 if currentActiveServer :
210201 return jsonify (server = currentActiveServer .selected_server , token = currentActiveServer .token , libraries = currentActiveServer .libraries )
211202 else :
@@ -233,7 +224,7 @@ def get_movies_from_plex_library():
233224 resources = [resource for resource in plex_account .resources () if resource .owned ]
234225 for resource in resources :
235226 if f"{ resource .name } ({ resource .connections [0 ].address } )" == currentActiveServer .selected_server :
236- print (f"resource: { resource .name } ({ resource .connections [0 ].address } ) == { currentActiveServer .selected_server } " )
227+ # print(f"resource: {resource.name} ({resource.connections[0].address}) == {currentActiveServer.selected_server}")
237228 server_resource = resource
238229 break
239230
@@ -291,10 +282,15 @@ def get_recommendations():
291282 global global_recommendations
292283 movie_id = request .args .get ('movieId' , default = 11 , type = int )
293284 api_key = request .args .get ('apiKey' , default = "" , type = str )
285+
294286 url = f"{ app .config ['TMDB_BASE_URL' ]} /movie/{ movie_id } /recommendations"
295287 params = {"api_key" : api_key }
296288
297289 response = requests .get (url , params = params )
290+
291+ if response .status_code != 200 :
292+ return {'message' : 'API request failed with status code ' + str (response .status_code )}, response .status_code
293+
298294 data = response .json ()
299295
300296 base_image_url = "https://image.tmdb.org/t/p/w500"
@@ -328,4 +324,4 @@ def get_recommendated_movies():
328324moviesFromSelectedLibrary = {}
329325
330326if __name__ == '__main__' :
331- app .run (debug = True )
327+ app .run (debug = False )
0 commit comments