Skip to content

Commit d78ad88

Browse files
committed
Added error alerts and removed debugging code
- Added alerts to let the user know about API connection failures and other errors - Removed some code for debugging reasons
1 parent 72596cb commit d78ad88

File tree

4 files changed

+53
-35
lines changed

4 files changed

+53
-35
lines changed

GAPS 2.py

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,6 @@ def save_tmdb_key():
8080

8181
@app.route('/link_plex_account', methods=['POST'])
8282
def 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>')
126117
def 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():
205196
def 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():
328324
moviesFromSelectedLibrary = {}
329325

330326
if __name__ == '__main__':
331-
app.run(debug=True)
327+
app.run(debug=False)

templates/configuration.html

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -229,17 +229,23 @@ <h4 class="alert-heading">Error!</h4>
229229
$('#loading').show();
230230

231231
$.post('/link_plex_account', function (response) {
232-
// Update the dropdown options with the server data
233-
var servers = response.servers; // No need to parse JSON, Flask's jsonify already does this.
234-
var dropdown = $('#server');
235-
dropdown.empty();
236-
servers.forEach(function (server) {
237-
dropdown.append($('<option>').text(server).attr('value', server));
238-
});
239-
240-
// Set the Plex token
241-
var token = response.token; // Assuming the response has a "token" key
242-
$('#plexToken').val(token);
232+
// Check if there's a message in the response
233+
if (response.message) {
234+
// Show an alert with the message
235+
alert(response.message);
236+
} else {
237+
// Update the dropdown options with the server data
238+
var servers = response.servers;
239+
var dropdown = $('#server');
240+
dropdown.empty();
241+
servers.forEach(function (server) {
242+
dropdown.append($('<option>').text(server).attr('value', server));
243+
});
244+
245+
// Set the Plex token
246+
var token = response.token;
247+
$('#plexToken').val(token);
248+
}
243249

244250
// Hide the loading text/spinner
245251
$('#loading').hide();

templates/libraries.html

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,20 @@ <h5 class="card-title">Your movies are really missing</h5>
125125

126126
<script>
127127
function getRecommendations(movieId) {
128-
//alert(movieId)
129128
var apiKey = localStorage.getItem('tmdbApiKey');
130129
fetch('/recommendations?movieId=' + movieId + '&apiKey=' + apiKey)
131130
.then(response => response.json())
132131
.then(data => {
133-
window.location.href = '/recommended';
132+
if (data.error) {
133+
// If there's an error in the response, show an alert
134+
alert(data.error);
135+
} else if (data.message) {
136+
// If there's a message in the response, show an alert
137+
alert(data.message);
138+
} else {
139+
// If there's no error or message, redirect as usual
140+
window.location.href = '/recommended';
141+
}
134142
})
135143
.catch((error) => {
136144
console.error('Error:', error);

templates/recommended.html

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,12 +135,20 @@ <h5 class="card-title">${movie.name} (${movie.year})</h5>
135135
}
136136

137137
function getRecommendations(movieId) {
138-
//alert(movieId)
139138
var apiKey = localStorage.getItem('tmdbApiKey');
140139
fetch('/recommendations?movieId=' + movieId + '&apiKey=' + apiKey)
141140
.then(response => response.json())
142141
.then(data => {
143-
window.location.href = '/recommended';
142+
if (data.error) {
143+
// If there's an error in the response, show an alert
144+
alert(data.error);
145+
} else if (data.message) {
146+
// If there's a message in the response, show an alert
147+
alert(data.message);
148+
} else {
149+
// If there's no error or message, redirect as usual
150+
window.location.href = '/recommended';
151+
}
144152
})
145153
.catch((error) => {
146154
console.error('Error:', error);

0 commit comments

Comments
 (0)