-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlexPlaylistMakerGUI.py
487 lines (408 loc) · 21 KB
/
PlexPlaylistMakerGUI.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
import customtkinter as ctk
from CTkMessagebox import CTkMessagebox
import tkinter as tk
import os
import threading
from PIL import Image
from PlexPlaylistMakerController import PlexIMDbApp, PlexLetterboxdApp, check_updates
VERSION = 1.1
class PlexPlaylistMakerGUI(ctk.CTk):
def __init__(self):
super().__init__()
self.controller = None
self.server_connection = None
self.title(check_updates(VERSION))
self.geometry("450x350")
self.resizable(False, False)
self.font = ("MS Sans Serif", 12, "bold")
self.grid_rowconfigure(0, weight=1)
self.grid_columnconfigure(1, weight=1)
self.servers = [] # Initialize an empty list to store server names
# Initialize the server variable
self.server_var = tk.StringVar(self)
# region nav frame
self.navigation_frame = ctk.CTkFrame(self, corner_radius=0)
self.navigation_frame.grid(row=0, column=0, sticky="nsew")
self.navigation_frame.grid_rowconfigure(4, weight=1)
image_path = os.path.join(os.path.dirname(
os.path.realpath(__file__)), "icons")
self.IMDb_image = ctk.CTkImage(
Image.open(image_path + "/IMDb.png"), size=(20, 20)
)
self.Letterboxd_image = ctk.CTkImage(
Image.open(image_path + "/Letterboxd.png"), size=(20, 20)
)
self.IMDB = ctk.CTkButton(
self.navigation_frame,
corner_radius=0,
height=40,
border_spacing=10,
font=self.font,
text="IMDb",
fg_color="transparent",
text_color=("gray10", "gray90"),
hover_color=("gray70", "gray30"),
anchor="w",
image=self.IMDb_image,
command=lambda: self.select_frame_by_name("imdb_frame"),
)
self.IMDB.grid(row=1, column=0, sticky="ew")
self.Letterboxd = ctk.CTkButton(
self.navigation_frame,
corner_radius=0,
height=40,
border_spacing=10,
font=self.font,
text="Letterboxd",
fg_color="transparent",
text_color=("gray10", "gray90"),
hover_color=("gray70", "gray30"),
anchor="w",
image=self.Letterboxd_image,
command=lambda: self.select_frame_by_name("letterboxd_frame"),
)
self.Letterboxd.grid(row=2, column=0, sticky="ew")
# endregion
# region IMDb frame
self.IMDB_frame = ctk.CTkFrame(
self, corner_radius=0, fg_color="transparent"
)
self.IMDB_frame.library_var = tk.StringVar(self) # Initialize library_var for the IMDb frame
# IMDb List URL textbox
self.IMDB_playlist_url_textbox = ctk.CTkEntry(
self.IMDB_frame, placeholder_text="IMDb List URL", width=200
)
self.IMDB_playlist_url_textbox.grid(
row=0, column=0, padx=10, pady=10, sticky="w"
)
# Playlist name textbox
self.IMDB_playlist_name_textbox = ctk.CTkEntry(
self.IMDB_frame, placeholder_text="Playlist Name", width=200
)
self.IMDB_playlist_name_textbox.grid(
row=1, column=0, padx=10, pady=10, sticky="w"
)
# Dropdown menu for Plex Servers
self.IMDB_server_menu = ctk.CTkOptionMenu(
self.IMDB_frame,
variable=self.server_var,
values=["Loading servers..."] # Placeholder text
)
self.IMDB_server_menu.grid(row=2, column=0, padx=10, pady=10, sticky="w")
# Dropdown menu for selecting a library
self.library_var = tk.StringVar(self)
self.library_menu = ctk.CTkOptionMenu(
self.IMDB_frame,
variable=self.library_var,
values=["Loading libraries..."] # Placeholder text
)
self.library_menu.grid(row=3, column=0, padx=10, pady=10, sticky="w")
# IMDb Create Playlist Button
self.imdb_create_playlist_button = ctk.CTkButton(
self.IMDB_frame,
text="Create Playlist",
command=lambda: self.start_playlist_creation(
self.IMDB_playlist_url_textbox.get(),
self.IMDB_playlist_name_textbox.get(),
self.imdb_create_playlist_button
)
)
self.imdb_create_playlist_button.grid(row=6, column=0, padx=10, pady=10, sticky="w")
# endregion
# region Letterboxd frame
self.Letterboxd_frame = ctk.CTkFrame(
self, corner_radius=0, fg_color="transparent")
self.Letterboxd_frame.library_var = tk.StringVar(self) # Initialize library_var for the Letterboxd frame
# Letterboxd List URL textbox
self.Letterboxd_playlist_url_textbox = ctk.CTkEntry(
self.Letterboxd_frame, placeholder_text="Letterboxd List URL", width=200
)
self.Letterboxd_playlist_url_textbox.grid(
row=0, column=0, padx=10, pady=10, sticky="w"
)
# Playlist name textbox
self.Letterboxd_playlist_name_textbox = ctk.CTkEntry(
self.Letterboxd_frame, placeholder_text="Playlist Name", width=200
)
self.Letterboxd_playlist_name_textbox.grid(
row=1, column=0, padx=10, pady=10, sticky="w"
)
# Dropdown menu for Plex Servers
self.Letterboxd_server_menu = ctk.CTkOptionMenu(
self.Letterboxd_frame,
variable=self.server_var,
values=["Loading servers..."] # Placeholder text
)
self.Letterboxd_server_menu.grid(row=2, column=0, padx=10, pady=10, sticky="w")
# Dropdown menu for selecting a library
self.library_var = tk.StringVar(self)
self.library_menu = ctk.CTkOptionMenu(
self.Letterboxd_frame,
variable=self.library_var,
values=["Loading libraries..."] # Placeholder text
)
self.library_menu.grid(row=3, column=0, padx=10, pady=10, sticky="w")
# Letterboxd Create Playlist Button
self.letterboxd_create_playlist_button = ctk.CTkButton(
self.Letterboxd_frame,
text="Create Playlist",
command=lambda: self.start_playlist_creation(
self.Letterboxd_playlist_url_textbox.get(),
self.Letterboxd_playlist_name_textbox.get(),
self.letterboxd_create_playlist_button
)
)
self.letterboxd_create_playlist_button.grid(row=6, column=0, padx=10, pady=10, sticky="w")
# endregion
self.current_frame = "imdb_frame" # Default to IMDb frame
# Overlay Frame for loading indication
self.loading_overlay = ctk.CTkFrame(self, width=450, height=350, corner_radius=10)
self.loading_overlay.place(x=0, y=0, relwidth=1, relheight=1)
self.loading_overlay_label = ctk.CTkLabel(self.loading_overlay, text="Loading...", font=("MS Sans Serif", 16, "bold"))
self.loading_overlay_label.place(relx=0.5, rely=0.5, anchor=tk.CENTER)
self.loading_dots = 0
self.loading_animation_id = None # Will store the ID of the scheduled after() call
# Hide the overlay immediately after creating it
self.hide_overlay() # This line ensures the overlay is hidden by default
def select_frame_by_name(self, frame_name):
"""Selects and displays the specified frame, and performs common post-selection actions."""
self.current_frame = frame_name # Update the current frame name
# Configure button appearances based on selected frame
self.IMDB.configure(fg_color=("gray75", "gray25") if frame_name == "imdb_frame" else "transparent")
self.Letterboxd.configure(fg_color=("gray75", "gray25") if frame_name == "letterboxd_frame" else "transparent")
"""Selects and displays the specified frame, and updates the controller."""
if frame_name == "imdb_frame":
self.switch_to_imdb_controller()
elif frame_name == "letterboxd_frame":
self.switch_to_letterboxd_controller()
# Display the selected frame and hide the other
if frame_name == "imdb_frame":
self.IMDB_frame.grid(row=0, column=1, sticky="nsew")
self.Letterboxd_frame.grid_forget()
elif frame_name == "letterboxd_frame":
self.Letterboxd_frame.grid(row=0, column=1, sticky="nsew")
self.IMDB_frame.grid_forget()
if not self.server_connection:
threading.Thread(target=self.async_login_and_fetch_servers).start()
def switch_to_imdb_controller(self):
"""Switches the current controller to the IMDb controller."""
if not isinstance(self.controller, PlexIMDbApp):
self.controller = PlexIMDbApp(server=self.server_connection)
# Ensure the server connection is set in the controller
self.controller.server = self.server_connection
# Ensure the correct library_var is used for IMDb
self.library_var = self.IMDB_frame.library_var
def switch_to_letterboxd_controller(self):
"""Switches the current controller to the Letterboxd controller."""
if not isinstance(self.controller, PlexLetterboxdApp):
self.controller = PlexLetterboxdApp(server=self.server_connection)
# Ensure the server connection is set in the controller
self.controller.server = self.server_connection
# Ensure the correct library_var is used for Letterboxd
self.library_var = self.Letterboxd_frame.library_var
def imdb_button_event(self):
self.select_frame_by_name("imdb_frame")
# Check if a server is already connected before attempting to log in again
if not self.controller.server:
# No server connection exists, so attempt to log in and fetch servers
# Run the login and fetch operation in a separate thread
threading.Thread(target=self.async_login_and_fetch_servers).start()
def letterboxd_button_event(self):
self.select_frame_by_name("letterboxd_frame")
# Check if a server is already connected before attempting to log in again
if not self.controller.server:
# No server connection exists, so attempt to log in and fetch servers
# Run the login and fetch operation in a separate thread
threading.Thread(target=self.async_login_and_fetch_servers).start()
def async_login_and_fetch_servers(self):
"""Fetch servers without blocking the UI, updating both menus on completion."""
# Disable the buttons to prevent multiple clicks
self.IMDB.configure(state=ctk.DISABLED)
self.Letterboxd.configure(state=ctk.DISABLED)
def fetch_servers():
# Show the loading overlay
self.show_overlay()
# Perform the login and fetch operation. This method will run on a separate thread
self.controller.login_and_fetch_servers(self.server_login_callback)
self.server_connection = self.controller.server # Update the shared server connection
# Start the server fetching process
threading.Thread(target=fetch_servers).start()
def server_login_callback(self, servers, success):
# Re-enable the IMDb and Letterboxd buttons after server fetch completes.
self.IMDB.configure(state=ctk.NORMAL)
self.Letterboxd.configure(state=ctk.NORMAL)
if success:
# Update the UI with the server list if login was successful.
self.update_server_menus(servers)
filtered_libraries = [lib['name'] for lib in self.controller.libraries if lib['type'] in ('movie', 'show')]
self.update_library_dropdown(filtered_libraries, self.IMDB_frame)
self.update_library_dropdown(filtered_libraries, self.Letterboxd_frame)
else:
# Show an error message if login failed.
CTkMessagebox.show_error("Login Failed", "Could not log in to Plex account.")
# Hide the overlay in all cases.
self.hide_overlay()
def re_enable_buttons_and_hide_overlay(self):
"""Re-enable the IMDb and Letterboxd buttons after server fetch completes."""
self.IMDB.configure(state=ctk.NORMAL)
self.Letterboxd.configure(state=ctk.NORMAL)
self.hide_overlay()
def show_overlay(self):
self.loading_overlay.configure(width=450, height=350)
self.loading_dots = 0
# Determine the frame on which to show the overlay based on the parameter
if self.current_frame == "imdb_frame":
frame = self.IMDB_frame
elif self.current_frame == "letterboxd_frame":
frame = self.Letterboxd_frame
else:
return # If the frame is not recognized, do not show the overlay
self.loading_overlay_label.place(relx=0.3, rely=0.4, anchor=tk.CENTER) # Center the label
# Update layout to get current dimensions and positions
frame.update_idletasks()
x = frame.winfo_x()
y = frame.winfo_y()
# Place the overlay without trying to set width and height here
self.loading_overlay.place(x=x, y=y, relwidth=1, relheight=1)
# Start the loading text animation
self.update_loading_text()
def hide_overlay(self):
self.loading_overlay.place_forget()
# Stop the loading text animation by canceling the scheduled update
if self.loading_animation_id is not None:
self.after_cancel(self.loading_animation_id)
self.loading_animation_id = None
def update_loading_text(self):
"""Update the loading text with animated dots."""
if not self.loading_overlay.winfo_ismapped():
# If the overlay is no longer displayed, cancel further updates
if self.loading_animation_id is not None:
self.after_cancel(self.loading_animation_id)
self.loading_animation_id = None
return
# Update the label with an increasing number of dots
self.loading_dots = (self.loading_dots + 1) % 4
loading_text = "Loading" + "." * self.loading_dots + " " * (3 - self.loading_dots)
self.loading_overlay_label.configure(text=loading_text)
# Reschedule this method to update the text again
self.loading_animation_id = self.after(500, self.update_loading_text)
def update_button_text(self, text, button):
button.configure(text=text)
def update_library_dropdown(self, libraries, target_frame):
# Check if the library menu already exists in the target frame and destroy it if it does
if hasattr(target_frame, 'library_menu'):
target_frame.library_menu.destroy()
# Create a new dropdown for the updated libraries in the target frame
library_var = tk.StringVar(self)
library_menu = ctk.CTkOptionMenu(
target_frame,
variable=library_var,
values=libraries
)
library_menu.grid(row=3, column=0, padx=10, pady=10, sticky="w")
# Set the default value if libraries are available
if libraries:
library_var.set(libraries[0])
# Update the target frame attribute to hold the new library menu and variable
target_frame.library_menu = library_menu
target_frame.library_var = library_var
def recreate_server_dropdown(self, frame, variable, servers, row, column, padx, pady, sticky):
"""
Recreate a server dropdown menu with updated servers.
Args:
frame: The parent frame where the dropdown will be placed.
variable: The tkinter variable associated with the dropdown.
servers: The list of server names to populate the dropdown.
row: The grid row where the dropdown will be placed.
column: The grid column where the dropdown will be placed.
padx: The padding along the x axis.
pady: The padding along the y axis.
sticky: The sticky option to define how the widget expands.
"""
# Create a new CTkOptionMenu with the updated server names
server_menu = ctk.CTkOptionMenu(
frame,
variable=variable,
values=servers
)
server_menu.grid(row=row, column=column, padx=padx, pady=pady, sticky=sticky)
# Set the default/selected server if the list is not empty
if servers:
variable.set(servers[0])
# Return the newly created dropdown menu
return server_menu
def update_server_menus(self, servers, server_var=None):
"""Recreate IMDb and Letterboxd server dropdown menus with updated servers."""
self.servers = servers # Store the updated list of servers
# Recreate the IMDb server dropdown menu with updated servers
self.IMDB_server_menu = self.recreate_server_dropdown(
frame=self.IMDB_frame,
variable=self.server_var,
servers=servers,
row=2,
column=0,
padx=10,
pady=10,
sticky="w"
)
# Recreate the Letterboxd server dropdown menu with updated servers
self.Letterboxd_server_menu = self.recreate_server_dropdown(
frame=self.Letterboxd_frame,
variable=self.server_var,
servers=servers,
row=2,
column=0,
padx=10,
pady=10,
sticky="w"
)
def start_playlist_creation(self, url, name, button):
# Determine which frame is currently active and get the selected library from the correct dropdown
if self.current_frame == "imdb_frame":
selected_library = self.IMDB_frame.library_var.get()
elif self.current_frame == "letterboxd_frame":
selected_library = self.Letterboxd_frame.library_var.get()
else:
CTkMessagebox(title="Error", message="Error: No active frame identified.", icon="cancel", option_1="OK")
return
def run():
# Update button text to indicate process start and disable it
self.after(0, lambda: self.update_button_text_dynamically("Creating Playlist", button, disable=True))
# Call the create playlist method with the selected library
self.controller.create_plex_playlist(url, name, selected_library, lambda success, message: self.after(0, self.playlist_creation_callback, success, message, button))
threading.Thread(target=run).start()
# Method to dynamically update button text with loading dots
def update_button_text_dynamically(self, base_text, button, disable=False):
# Cancel any existing animation
if hasattr(button, '_animation_id'):
self.after_cancel(button._animation_id)
delattr(button, '_animation_id')
if disable:
# Store the original command to restore it later
if not hasattr(button, '_original_command'):
button._original_command = button.cget('command')
button.configure(command=lambda: None, state=ctk.DISABLED) # Disable the button
def animate_dots(dots=1):
# Update the text with the next number of dots
button.configure(text=f"{base_text}{'.' * dots}")
# Schedule the next update, cycling back to 1 dot after 3
button._animation_id = self.after(500, animate_dots, (dots % 3) + 1)
animate_dots() # Start the animation
else:
# Stop the animation and restore the button to its original state
if hasattr(button, '_original_command'):
button.configure(command=button._original_command)
delattr(button, '_original_command')
button.configure(text=base_text, state=ctk.NORMAL) # Re-enable the button
# Callback method to be called once playlist creation is done
def playlist_creation_callback(self, success, message, button):
# Stop any ongoing text animation and reset the button text and state
self.update_button_text_dynamically("Create Playlist", button, disable=False)
# Display the message using CTkMessagebox based on success status
if success:
CTkMessagebox(title="Success", message=message, icon="check", option_1="OK")
else:
CTkMessagebox(title="Error", message=message, icon="cancel", option_1="OK")
if __name__ == "__main__":
app = PlexPlaylistMakerGUI()
app.mainloop()