-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcleanmyplex.py
executable file
·851 lines (739 loc) · 37.5 KB
/
cleanmyplex.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
from flask import Flask, render_template, request, redirect, url_for, flash, send_from_directory, jsonify, Response, send_file
import requests
import pandas as pd
from plexapi.server import PlexServer
from plexapi.myplex import MyPlexAccount
import json
import os
import threading
import time
from uuid import uuid4
app = Flask(__name__)
app.secret_key = 'supersecretkey'
# Charger la configuration depuis le fichier config.json
def load_config():
with open('config.json') as config_file:
return json.load(config_file)
config = load_config()
PLEX_URL = config['PLEX_URL']
PLEX_TOKEN = config['PLEX_TOKEN']
PLEX_USERNAME = config['PLEX_USERNAME']
PLEX_PASSWORD = config['PLEX_PASSWORD']
FRIEND_SERVER_NAME = config['FRIEND_SERVER_NAME']
CSV_FILE_FILMS = 'unwatched_movies.csv'
CSV_FILE_SERIES = 'unwatched_series.csv'
CSV_FILE_COMMON_MOVIES = 'common_movies.csv'
CSV_FILE_COMMON_SERIES = 'common_series.csv'
plex = PlexServer(PLEX_URL, PLEX_TOKEN, timeout=300)
account = MyPlexAccount(PLEX_USERNAME, PLEX_PASSWORD)
# Variables globales pour suivre les tâches en arrière-plan
tasks = {}
tasks_lock = threading.Lock()
# Assurez-vous que le répertoire de cache des affiches existe
if not os.path.exists('static/poster_cache'):
os.makedirs('static/poster_cache')
def get_active_sessions():
try:
sessions = plex.sessions()
session_data = []
for session in sessions:
last_active = session.startedAt.strftime('%Y-%m-%d %H:%M:%S') if hasattr(session, 'startedAt') and session.startedAt else 'Inconnu'
session_info = {
'username': session.usernames[0] if session.usernames else 'Inconnu',
'publicAddress': session.players[0].address if session.players else 'N/A',
'last_active': last_active
}
session_data.append(session_info)
return session_data
except Exception as e:
flash(f"Erreur lors de la récupération des sessions actives : {e}", 'danger')
return []
def get_view_history(user):
try:
view_history = []
for section in plex.library.sections():
for item in section.all():
if item.viewCount > 0:
view_history.append({
'title': item.title,
'watched': item.viewCount > 0,
'user': user.username
})
return view_history
except Exception as e:
flash(f"Erreur lors de la récupération de l'historique de lecture : {e}", 'danger')
return []
def get_last_activity(user):
try:
last_activity = None
for section in plex.library.sections():
for item in section.all():
history = item.history()
for view in history:
if view.user and view.user.username == user.username:
if not last_activity or view.viewedAt > last_activity:
last_activity = view.viewedAt
return last_activity.strftime('%Y-%m-%d %H:%M:%S') if last_activity else 'Inconnu'
except Exception as e:
flash(f"Erreur lors de la récupération de la dernière activité pour l'utilisateur {user.username} : {e}", 'danger')
return 'Inconnu'
# Fonction pour récupérer dynamiquement les sections de bibliothèque
def get_library_sections(plex_server, media_type):
sections = plex_server.library.sections()
if media_type == 'movie':
return [section.title for section in sections if section.type == 'movie']
elif media_type == 'show':
return [section.title for section in sections if section.type == 'show']
else:
return []
def compare_libraries(library_names_local, library_names_friend, media_type):
friend_server = account.resource(FRIEND_SERVER_NAME).connect()
if "ALL" in library_names_friend:
friend_libraries = friend_server.library.sections()
if media_type == 'movie':
friend_library_list = [lib for lib in friend_libraries if lib.type == 'movie']
elif media_type == 'show':
friend_library_list = [lib for lib in friend_libraries if lib.type == 'show']
else:
try:
friend_library_list = [friend_server.library.section(name) for name in library_names_friend]
except Exception as e:
flash(f"Erreur lors de l'accès à une bibliothèque de l'ami : {e}", 'danger')
friend_library_list = []
if "ALL" in library_names_local:
local_libraries = plex.library.sections()
if media_type == 'movie':
local_library_list = [lib for lib in local_libraries if lib.type == 'movie']
elif media_type == 'show':
local_library_list = [lib for lib in local_libraries if lib.type == 'show']
else:
try:
local_library_list = [plex.library.section(name) for name in library_names_local]
except Exception as e:
flash(f"Erreur lors de l'accès à une de vos bibliothèques : {e}", 'danger')
local_library_list = []
friend_items = {}
for lib in friend_library_list:
for item in lib.all():
friend_items[item.title] = item
duplicates = []
for lib in local_library_list:
for local_item in lib.all():
title = local_item.title
if title in friend_items:
friend_item = friend_items[title]
if media_type == 'movie':
local_file_size_gb = sum(
media_part.size for media in local_item.media for media_part in media.parts
) / (1024 ** 3)
remote_file_size_gb = sum(
media_part.size for media in friend_item.media for media_part in media.parts
) / (1024 ** 3)
number_of_local_episodes = None
number_of_remote_episodes = None
elif media_type == 'show':
local_file_size_gb = sum(
media_part.size
for episode in local_item.episodes()
for media in episode.media
for media_part in media.parts
) / (1024 ** 3)
remote_file_size_gb = sum(
media_part.size
for episode in friend_item.episodes()
for media in episode.media
for media_part in media.parts
) / (1024 ** 3)
number_of_local_episodes = len(local_item.episodes())
number_of_remote_episodes = len(friend_item.episodes())
else:
continue
largest_file_size_gb = max(local_file_size_gb, remote_file_size_gb)
added_at = local_item.addedAt.strftime('%Y-%m-%d') if local_item.addedAt else 'N/A'
release_date = local_item.originallyAvailableAt.strftime('%Y-%m-%d') if local_item.originallyAvailableAt else 'N/A'
rating = local_item.audienceRating if local_item.audienceRating else 0
if local_item.userRating is not None:
plex_rating = local_item.userRating
elif local_item.rating is not None:
plex_rating = local_item.rating
else:
plex_rating = 0
if media_type == 'movie':
view_count = local_item.viewCount if hasattr(local_item, 'viewCount') else 0
local_file_path = local_item.media[0].parts[0].file if local_item.media and local_item.media[0].parts else 'N/A'
elif media_type == 'show':
view_count = sum(episode.viewCount for episode in local_item.episodes() if hasattr(episode, 'viewCount'))
local_file_path = 'N/A'
else:
view_count = 0
local_file_path = 'N/A'
summary = local_item.summary if local_item.summary else 'N/A'
genres = ', '.join([genre.tag for genre in local_item.genres]) if hasattr(local_item, 'genres') else 'N/A'
directors = ', '.join([director.tag for director in local_item.directors]) if hasattr(local_item, 'directors') else 'N/A'
actors = ', '.join([actor.tag for actor in local_item.actors]) if hasattr(local_item, 'actors') else 'N/A'
# Gestion de l'affiche
if hasattr(local_item, 'thumb'):
poster_filename = f"poster_{local_item.ratingKey}.jpg"
poster_filepath = os.path.join('static', 'poster_cache', poster_filename)
poster_url = f"/static/poster_cache/{poster_filename}"
# Télécharger et stocker l'image si elle n'existe pas
if not os.path.exists(poster_filepath):
poster_url_full = plex.url(local_item.thumb)
headers = {'X-Plex-Token': PLEX_TOKEN}
response = requests.get(poster_url_full, headers=headers, stream=True)
if response.status_code == 200:
with open(poster_filepath, 'wb') as f:
for chunk in response.iter_content(1024):
f.write(chunk)
else:
poster_url = '/static/no_image_available.jpg'
else:
poster_url = '/static/no_image_available.jpg'
duplicate_entry = {
'title': title,
'rating': rating,
'plex_rating': plex_rating,
'view_count': view_count,
'local_path': local_file_path,
'added_at': added_at,
'release_date': release_date,
'local_file_size': f"{local_file_size_gb:.2f} Go",
'remote_file_size': f"{remote_file_size_gb:.2f} Go",
'largest_file_size': f"{largest_file_size_gb:.2f} Go",
'Bibliothèque': lib.title,
'Action': '',
'poster_url': poster_url,
'summary': summary,
'genres': genres,
'directors': directors,
'actors': actors
}
if media_type == 'show':
duplicate_entry['number_of_local_episodes'] = number_of_local_episodes
duplicate_entry['number_of_remote_episodes'] = number_of_remote_episodes
duplicates.append(duplicate_entry)
df = pd.DataFrame(duplicates)
output_file = CSV_FILE_COMMON_MOVIES if media_type == 'movie' else CSV_FILE_COMMON_SERIES
if media_type == 'movie':
columns_order = [
'title', 'rating', 'plex_rating', 'view_count', 'local_path',
'added_at', 'release_date',
'local_file_size', 'remote_file_size', 'largest_file_size', 'Bibliothèque', 'Action',
'poster_url',
'summary', 'genres', 'directors', 'actors'
]
else:
columns_order = [
'title', 'rating', 'plex_rating', 'view_count', 'local_path',
'added_at', 'release_date',
'local_file_size', 'remote_file_size', 'largest_file_size', 'Bibliothèque',
'number_of_local_episodes', 'number_of_remote_episodes', 'Action',
'poster_url',
'summary', 'genres', 'directors', 'actors'
]
df = df[columns_order]
total_space_saved = sum(float(size.replace(' Go', '')) for size in df['largest_file_size'])
df.to_csv(output_file, index=False)
return df, output_file, len(duplicates), total_space_saved
def generate_csv(library_names, csv_file, media_type):
print(f"generate_csv appelé avec library_names={library_names}, csv_file='{csv_file}', media_type='{media_type}'")
if os.path.exists(csv_file):
print(f"Le fichier CSV '{csv_file}' existe déjà. Chargement des données existantes.")
existing_df = pd.read_csv(csv_file, encoding='utf-8', delimiter=',', quotechar='"')
existing_df['file_size'] = existing_df['file_size'].replace('N/A', '0')
existing_df['file_size'] = existing_df['file_size'].str.replace(' Go', '', regex=False).astype(float)
else:
print(f"Le fichier CSV '{csv_file}' n'existe pas. Création d'un nouveau DataFrame.")
columns = [
'title', 'ratingKey', 'rating', 'plex_rating', 'view_count', 'local_path',
'added_at', 'release_date', 'file_size', 'Bibliothèque', 'Action',
'poster_url', 'summary', 'genres', 'directors', 'actors'
]
existing_df = pd.DataFrame(columns=columns)
# Déterminer les bibliothèques à traiter
if "ALL" in library_names:
if media_type == 'movie':
libraries = [section.title for section in plex.library.sections() if section.type == 'movie']
elif media_type == 'show':
libraries = [section.title for section in plex.library.sections() if section.type == 'show']
else:
libraries = []
else:
libraries = library_names
new_items = []
total_libraries = len(libraries)
for lib_idx, library_name in enumerate(libraries):
print(f"Traitement de la bibliothèque {lib_idx + 1}/{total_libraries} : '{library_name}'")
try:
library = plex.library.section(library_name)
except Exception as e:
flash(f"Erreur lors de l'accès à la bibliothèque '{library_name}': {e}", 'danger')
print(f"Erreur lors de l'accès à la bibliothèque '{library_name}': {e}")
continue
try:
all_items = library.all()
except Exception as e:
flash(f"Erreur lors de la récupération des éléments de la bibliothèque '{library_name}': {e}", 'danger')
print(f"Erreur lors de la récupération des éléments de la bibliothèque '{library_name}': {e}")
continue
total_items = len(all_items)
for idx, item in enumerate(all_items):
print(f"Traitement de l'élément {idx + 1}/{total_items} : '{item.title}'")
try:
release_date = item.originallyAvailableAt if item.originallyAvailableAt else None
rating = item.audienceRating if item.audienceRating else 0
if item.userRating is not None:
plex_rating = item.userRating
elif item.rating is not None:
plex_rating = item.rating
else:
plex_rating = 0
summary = item.summary if item.summary else 'N/A'
genres = ', '.join([genre.tag for genre in item.genres]) if hasattr(item, 'genres') else 'N/A'
directors = ', '.join([director.tag for director in item.directors]) if hasattr(item, 'directors') else 'N/A'
actors = ', '.join([actor.tag for actor in item.actors]) if hasattr(item, 'actors') else 'N/A'
# Gestion de l'affiche
if hasattr(item, 'thumb'):
poster_filename = f"poster_{item.ratingKey}.jpg"
poster_filepath = os.path.join('static', 'poster_cache', poster_filename)
poster_url = f"/static/poster_cache/{poster_filename}"
# Télécharger et stocker l'image si elle n'existe pas
if not os.path.exists(poster_filepath):
poster_url_full = plex.url(item.thumb)
headers = {'X-Plex-Token': PLEX_TOKEN}
try:
response = requests.get(poster_url_full, headers=headers, stream=True, timeout=10)
if response.status_code == 200:
with open(poster_filepath, 'wb') as f:
for chunk in response.iter_content(1024):
f.write(chunk)
else:
print(f"Erreur lors du téléchargement de l'image pour '{item.title}': {response.status_code}")
poster_url = '/static/no_image_available.jpg'
except Exception as e:
print(f"Exception lors du téléchargement de l'image pour '{item.title}': {e}")
poster_url = '/static/no_image_available.jpg'
else:
poster_url = '/static/no_image_available.jpg'
if item.TYPE == 'movie':
view_count = item.viewCount if hasattr(item, 'viewCount') else 0
local_path = item.media[0].parts[0].file if item.media and item.media[0].parts else 'N/A'
file_size_gb = sum(media_part.size for media in item.media for media_part in media.parts) / (1024 ** 3)
elif item.TYPE == 'show':
view_count = sum(
episode.viewCount for episode in item.episodes() if hasattr(episode, 'viewCount')
)
local_path = 'N/A'
file_size_gb = sum(
media_part.size
for episode in item.episodes()
for media in episode.media
for media_part in media.parts
) / (1024 ** 3)
else:
view_count = 0
local_path = 'N/A'
file_size_gb = 0.0
new_items.append({
'title': item.title,
'ratingKey': item.ratingKey,
'rating': rating,
'plex_rating': plex_rating,
'view_count': view_count,
'local_path': local_path,
'added_at': item.addedAt.strftime('%Y-%m-%d') if item.addedAt else 'N/A',
'release_date': release_date.strftime('%Y-%m-%d') if release_date else 'N/A',
'file_size': file_size_gb,
'Bibliothèque': library_name,
'Action': '',
'poster_url': poster_url,
'summary': summary,
'genres': genres,
'directors': directors,
'actors': actors
})
except Exception as e:
print(f"Erreur lors du traitement de l'élément '{item.title}': {e}")
continue
new_df = pd.DataFrame(new_items)
print("Tous les éléments ont été traités. Création du DataFrame.")
if not existing_df.empty and not new_df.empty:
combined_df = pd.concat([existing_df, new_df]).drop_duplicates(subset='title', keep='first').reset_index(drop=True)
elif not existing_df.empty:
combined_df = existing_df
elif not new_df.empty:
combined_df = new_df
else:
combined_df = pd.DataFrame(columns=[
'title', 'ratingKey', 'rating', 'plex_rating', 'view_count', 'local_path',
'added_at', 'release_date', 'file_size', 'Bibliothèque', 'Action',
'poster_url', 'summary', 'genres', 'directors', 'actors'
])
combined_df['Action'] = combined_df['Action'].fillna('')
combined_df['file_size'] = pd.to_numeric(combined_df['file_size'], errors='coerce').fillna(0)
combined_df = combined_df.sort_values(by=['added_at'], ascending=True)
combined_df['file_size'] = combined_df['file_size'].apply(lambda x: f"{x:.2f} Go")
columns_order = [
'title', 'ratingKey', 'rating', 'plex_rating', 'view_count', 'local_path',
'added_at', 'release_date', 'file_size', 'Bibliothèque', 'Action',
'poster_url', 'summary', 'genres', 'directors', 'actors'
]
combined_df = combined_df[columns_order]
print(f"Écriture du DataFrame dans le fichier CSV '{csv_file}'.")
combined_df.to_csv(csv_file, index=False)
print(f"CSV '{csv_file}' généré avec succès.")
return combined_df, csv_file
# Fonction de génération de CSV en arrière-plan avec thread
def generate_csv_thread(library_names, csv_file, media_type, task_id):
try:
generate_csv(library_names, csv_file, media_type)
with tasks_lock:
tasks[task_id]['status'] = 'completed'
tasks[task_id]['message'] = f"CSV {csv_file} généré avec succès."
print(f"CSV {csv_file} généré avec succès.")
except Exception as e:
with tasks_lock:
tasks[task_id]['status'] = 'failed'
tasks[task_id]['message'] = f"Erreur lors de la génération du CSV : {e}"
print(f"Erreur lors de la génération du CSV : {e}")
# Nouvelle fonction pour la suppression en tâche de fond
def delete_items_from_csv_thread(csv_file, task_id):
try:
if not os.path.exists(csv_file):
with tasks_lock:
tasks[task_id]['status'] = 'failed'
tasks[task_id]['message'] = f"Le fichier CSV {csv_file} n'existe pas."
return
df = pd.read_csv(csv_file)
items_to_delete = df[df['Action'] == 'D']
total_items = len(items_to_delete)
deleted_items = 0
for index, row in items_to_delete.iterrows():
try:
rating_key = row.get('ratingKey')
if pd.notna(rating_key):
item = plex.fetchItem(int(rating_key))
item.delete()
df.drop(index, inplace=True)
deleted_items += 1
with tasks_lock:
tasks[task_id]['progress'] = f"{deleted_items}/{total_items} éléments supprimés."
else:
with tasks_lock:
tasks[task_id]['errors'].append(f"Clé de notation invalide pour {row['title']}.")
except Exception as e:
with tasks_lock:
tasks[task_id]['errors'].append(f"Erreur lors de la suppression de {row['title']}: {e}")
df.to_csv(csv_file, index=False)
with tasks_lock:
if tasks[task_id]['errors']:
tasks[task_id]['status'] = 'completed_with_errors'
tasks[task_id]['message'] = f"Suppression terminée avec des erreurs. {deleted_items}/{total_items} éléments supprimés."
else:
tasks[task_id]['status'] = 'completed'
tasks[task_id]['message'] = f"Suppression terminée avec succès. {deleted_items} éléments supprimés."
except Exception as e:
with tasks_lock:
tasks[task_id]['status'] = 'failed'
tasks[task_id]['message'] = f"Erreur lors de la suppression : {e}"
def compare_libraries_thread(local_library_names, friend_library_names, media_type, task_id):
try:
df, output_file, num_items, total_space_saved_gb = compare_libraries(local_library_names, friend_library_names, media_type)
with tasks_lock:
tasks[task_id]['status'] = 'completed'
tasks[task_id]['message'] = f"CSV {output_file} généré avec succès."
except Exception as e:
with tasks_lock:
tasks[task_id]['status'] = 'failed'
tasks[task_id]['message'] = f"Erreur lors de la comparaison des bibliothèques : {e}"
@app.route('/test_token', methods=['POST'])
def test_token():
try:
test_token = request.form['PLEX_TOKEN']
plex_test = PlexServer(config['PLEX_URL'], test_token)
plex_test.library.sections()
return jsonify({'status': 'success', 'message': 'Connexion réussie avec ce token !'})
except Exception as e:
return jsonify({'status': 'error', 'message': f'Erreur : {str(e)}'})
@app.route('/test_login', methods=['POST'])
def test_login():
try:
plex_username = request.form['PLEX_USERNAME']
plex_password = request.form['PLEX_PASSWORD']
account_test = MyPlexAccount(plex_username, plex_password)
account_test.devices()
return jsonify({'status': 'success', 'message': 'Connexion réussie avec ces identifiants !'})
except Exception as e:
return jsonify({'status': 'error', 'message': f'Erreur : {str(e)}'})
@app.route('/manage_users')
def manage_users():
try:
users = account.users()
sessions = get_active_sessions()
user_data = []
for user in users:
session_info = next((session for session in sessions if session['username'] == user.username), None)
is_active = "Oui" if session_info else "Non"
user_info = {
'username': user.username,
'email': user.email if hasattr(user, 'email') else 'N/A',
'title': user.title if hasattr(user, 'title') else 'N/A',
'userID': user.id,
'homeUser': 'Oui' if user.home else 'Non',
'role': 'Invité',
'is_active': is_active,
'publicAddress': session_info['publicAddress'] if session_info else 'N/A',
}
user_data.append(user_info)
return render_template('manage_users.html', users=user_data)
except Exception as e:
flash(f"Erreur lors de la récupération des utilisateurs : {e}", 'danger')
return redirect(url_for('index'))
@app.route('/user_details/<username>')
def user_details(username):
try:
user = next((u for u in account.users() if u.username == username), None)
if not user:
flash(f"Utilisateur {username} non trouvé", 'danger')
return redirect(url_for('manage_users'))
sessions = get_active_sessions()
session_info = next((session for session in sessions if session['username'] == user.username), None)
user_info = {
'username': user.username,
'email': user.email if hasattr(user, 'email') else 'N/A',
'title': user.title if hasattr(user, 'title') else 'N/A',
'userID': user.id,
'homeUser': 'Oui' if user.home else 'Non',
'publicAddress': session_info['publicAddress'] if session_info else 'N/A',
'subscriptionType': user.subscriptionType if hasattr(user, 'subscriptionType') else 'Gratuit',
'is_active': "Oui" if session_info else "Non"
}
return render_template('user_details.html', user=user_info)
except Exception as e:
flash(f"Erreur lors de la récupération des détails de l'utilisateur {username} : {e}", 'danger')
return redirect(url_for('manage_users'))
@app.route('/')
def index():
films_csv_exists = os.path.exists(CSV_FILE_FILMS)
series_csv_exists = os.path.exists(CSV_FILE_SERIES)
common_movies_csv_exists = os.path.exists(CSV_FILE_COMMON_MOVIES)
common_series_csv_exists = os.path.exists(CSV_FILE_COMMON_SERIES)
tasks_list = []
if 'tasks' in request.args:
tasks_list = request.args.get('tasks').split(',')
return render_template('index.html',
films_csv_exists=films_csv_exists,
series_csv_exists=series_csv_exists,
common_movies_csv_exists=common_movies_csv_exists,
common_series_csv_exists=common_series_csv_exists,
tasks_list=tasks_list)
@app.route('/delete_csv', methods=['POST'])
def delete_csv():
csv_file = request.form.get('csv_file')
if not csv_file:
flash(f"Aucun fichier spécifié pour la suppression.", 'danger')
return redirect(url_for('index'))
valid_files = [CSV_FILE_FILMS, CSV_FILE_SERIES, CSV_FILE_COMMON_MOVIES, CSV_FILE_COMMON_SERIES]
if csv_file in valid_files:
file_path = os.path.join(os.getcwd(), csv_file)
if os.path.isfile(file_path):
os.remove(csv_file)
flash(f"Fichier {csv_file} supprimé avec succès.", 'success')
else:
flash(f"Le fichier {csv_file} n'existe pas.", 'danger')
else:
flash(f"Le fichier {csv_file} spécifié est invalide.", 'danger')
return redirect(url_for('index'))
@app.route('/clean', methods=['GET', 'POST'])
def clean():
local_movie_libraries = get_library_sections(plex, 'movie')
local_show_libraries = get_library_sections(plex, 'show')
if "ALL" not in local_movie_libraries:
local_movie_libraries.insert(0, "ALL")
if "ALL" not in local_show_libraries:
local_show_libraries.insert(0, "ALL")
films_csv_mtime = time.ctime(os.path.getmtime(CSV_FILE_FILMS)) if os.path.exists(CSV_FILE_FILMS) else None
series_csv_mtime = time.ctime(os.path.getmtime(CSV_FILE_SERIES)) if os.path.exists(CSV_FILE_SERIES) else None
if request.method == 'POST':
selected_movie_libraries = request.form.getlist('library_names_films')
selected_series_libraries = request.form.getlist('library_names_series')
tasks_list = []
if selected_movie_libraries:
csv_file = CSV_FILE_FILMS
media_type = 'movie'
task_id = str(uuid4())
with tasks_lock:
tasks[task_id] = {'status': 'running', 'message': 'La génération du CSV des films a démarré.'}
threading.Thread(target=generate_csv_thread, args=(selected_movie_libraries, csv_file, media_type, task_id)).start()
tasks_list.append(task_id)
flash('La génération du CSV des films a démarré en arrière-plan.', 'info')
if selected_series_libraries:
csv_file = CSV_FILE_SERIES
media_type = 'show'
task_id = str(uuid4())
with tasks_lock:
tasks[task_id] = {'status': 'running', 'message': 'La génération du CSV des séries a démarré.'}
threading.Thread(target=generate_csv_thread, args=(selected_series_libraries, csv_file, media_type, task_id)).start()
tasks_list.append(task_id)
flash('La génération du CSV des séries a démarré en arrière-plan.', 'info')
return redirect(url_for('index', tasks=','.join(tasks_list)))
return render_template(
'clean.html',
local_movie_libraries=local_movie_libraries,
local_show_libraries=local_show_libraries,
films_csv_exists=os.path.exists(CSV_FILE_FILMS),
series_csv_exists=os.path.exists(CSV_FILE_SERIES),
films_csv_mtime=films_csv_mtime,
series_csv_mtime=series_csv_mtime
)
@app.route('/duplicates', methods=['GET', 'POST'])
def duplicates():
try:
friend_server = account.resource(FRIEND_SERVER_NAME).connect()
friend_movie_libraries = get_library_sections(friend_server, 'movie')
friend_show_libraries = get_library_sections(friend_server, 'show')
except Exception as e:
flash(f"Erreur lors de la connexion au serveur de l'ami : {e}", 'danger')
friend_movie_libraries = []
friend_show_libraries = []
local_movie_libraries = get_library_sections(plex, 'movie')
local_show_libraries = get_library_sections(plex, 'show')
for lib_list in [friend_movie_libraries, friend_show_libraries, local_movie_libraries, local_show_libraries]:
if "ALL" not in lib_list:
lib_list.insert(0, "ALL")
common_movies_csv_exists = os.path.exists(CSV_FILE_COMMON_MOVIES)
common_series_csv_exists = os.path.exists(CSV_FILE_COMMON_SERIES)
common_movies_csv_mtime = time.ctime(os.path.getmtime(CSV_FILE_COMMON_MOVIES)) if common_movies_csv_exists else None
common_series_csv_mtime = time.ctime(os.path.getmtime(CSV_FILE_COMMON_SERIES)) if common_series_csv_exists else None
if request.method == 'POST':
selected_local_movie_libraries = request.form.getlist('local_library_movies')
selected_friend_movie_libraries = request.form.getlist('friend_library_movies')
selected_local_series_libraries = request.form.getlist('local_library_series')
selected_friend_series_libraries = request.form.getlist('friend_library_series')
tasks_list = []
if selected_local_movie_libraries and selected_friend_movie_libraries:
media_type = 'movie'
task_id = str(uuid4())
with tasks_lock:
tasks[task_id] = {'status': 'running', 'message': 'La comparaison des films a démarré.'}
threading.Thread(target=compare_libraries_thread, args=(selected_local_movie_libraries, selected_friend_movie_libraries, media_type, task_id)).start()
tasks_list.append(task_id)
flash('La comparaison des films a démarré en arrière-plan.', 'info')
if selected_local_series_libraries and selected_friend_series_libraries:
media_type = 'show'
task_id = str(uuid4())
with tasks_lock:
tasks[task_id] = {'status': 'running', 'message': 'La comparaison des séries a démarré.'}
threading.Thread(target=compare_libraries_thread, args=(selected_local_series_libraries, selected_friend_series_libraries, media_type, task_id)).start()
tasks_list.append(task_id)
flash('La comparaison des séries a démarré en arrière-plan.', 'info')
return redirect(url_for('index', tasks=','.join(tasks_list)))
return render_template(
'duplicates.html',
friend_movie_libraries=friend_movie_libraries,
friend_show_libraries=friend_show_libraries,
local_movie_libraries=local_movie_libraries,
local_show_libraries=local_show_libraries,
common_movies_csv_exists=common_movies_csv_exists,
common_series_csv_exists=common_series_csv_exists,
common_movies_csv_mtime=common_movies_csv_mtime,
common_series_csv_mtime=common_series_csv_mtime
)
@app.route('/task_status/<task_id>')
def task_status(task_id):
with tasks_lock:
status = tasks.get(task_id, {'status': 'unknown', 'message': 'Tâche inconnue.'})
return jsonify(status)
@app.route('/view_csv/<path:csv_file>', methods=['GET', 'POST'])
def view_csv(csv_file):
if os.path.exists(csv_file):
df = pd.read_csv(csv_file)
df = df.fillna('N/A')
else:
flash('Le fichier CSV spécifié n\'existe pas.', 'danger')
return redirect(url_for('index'))
if 'ratingKey' not in df.columns:
df['ratingKey'] = 'N/A'
date_columns = ['added_at', 'release_date']
for col in date_columns:
if col in df.columns:
df[col] = pd.to_datetime(df[col], format='%Y-%m-%d', errors='coerce')
unique_libraries = sorted(df['Bibliothèque'].unique().tolist()) if 'Bibliothèque' in df.columns else []
unique_actions = sorted(df['Action'].dropna().unique().tolist()) if 'Action' in df.columns else []
if request.method == 'POST':
for index, row in df.iterrows():
action = request.form.get(f'action_{index}')
if action in ['A', 'D']:
df.at[index, 'Action'] = action
df.to_csv(csv_file, index=False)
flash('CSV mis à jour avec succès.', 'success')
return redirect(url_for('view_csv', csv_file=csv_file))
return render_template(
'view_csv.html',
df=df,
titles=df.columns.values,
csv_file=csv_file,
unique_libraries=unique_libraries,
unique_actions=unique_actions
)
@app.route('/view_existing_csv/<library>')
def view_existing_csv(library):
if library == 'films':
csv_file = CSV_FILE_FILMS
elif library == 'series':
csv_file = CSV_FILE_SERIES
elif library == 'common_movies':
csv_file = CSV_FILE_COMMON_MOVIES
elif library == 'common_series':
csv_file = CSV_FILE_COMMON_SERIES
else:
flash("Bibliothèque CSV inconnue.", 'danger')
return redirect(url_for('index'))
return redirect(url_for('view_csv', csv_file=csv_file))
@app.route('/process_csv/<path:csv_file>', methods=['POST'])
def process_csv(csv_file):
task_id = str(uuid4())
with tasks_lock:
tasks[task_id] = {
'status': 'running',
'message': 'La suppression a démarré.',
'progress': '0%',
'errors': []
}
threading.Thread(target=delete_items_from_csv_thread, args=(csv_file, task_id)).start()
flash('La suppression a démarré en arrière-plan.', 'info')
return redirect(url_for('index', tasks=task_id))
@app.route('/download/<path:csv_file>')
def download_csv(csv_file):
return send_from_directory(directory=os.getcwd(), path=csv_file, as_attachment=True)
@app.route('/settings', methods=['GET', 'POST'])
def settings():
global PLEX_URL, PLEX_TOKEN, PLEX_USERNAME, PLEX_PASSWORD
global FRIEND_SERVER_NAME
global plex, account
if request.method == 'POST':
config['PLEX_URL'] = request.form.get('PLEX_URL')
config['PLEX_TOKEN'] = request.form.get('PLEX_TOKEN')
config['PLEX_USERNAME'] = request.form.get('PLEX_USERNAME')
config['PLEX_PASSWORD'] = request.form.get('PLEX_PASSWORD')
config['FRIEND_SERVER_NAME'] = request.form.get('friend_server_name', '')
with open('config.json', 'w') as config_file:
json.dump(config, config_file, indent=4)
PLEX_URL = config['PLEX_URL']
PLEX_TOKEN = config['PLEX_TOKEN']
PLEX_USERNAME = config['PLEX_USERNAME']
PLEX_PASSWORD = config['PLEX_PASSWORD']
FRIEND_SERVER_NAME = config['FRIEND_SERVER_NAME']
plex = PlexServer(PLEX_URL, PLEX_TOKEN)
account = MyPlexAccount(PLEX_USERNAME, PLEX_PASSWORD)
flash('Paramètres mis à jour avec succès.', 'success')
return redirect(url_for('index'))
return render_template(
'settings.html',
config=config,
local_movie_libraries=get_library_sections(plex, 'movie'),
local_show_libraries=get_library_sections(plex, 'show'),
friend_server_names=[resource.name for resource in account.resources()]
)
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True)