-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.py
More file actions
704 lines (560 loc) · 27.2 KB
/
index.py
File metadata and controls
704 lines (560 loc) · 27.2 KB
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
from flask import Flask, render_template, request, send_from_directory, redirect, send_file, url_for, abort, flash
import os
import random
from werkzeug.utils import secure_filename
import subprocess
import re
import json
import shutil
import threading
import concurrent.futures
from werkzeug.utils import safe_join
from urllib.parse import unquote
import random
import urllib.parse
import zipfile
import io
executor = concurrent.futures.ThreadPoolExecutor(max_workers=4)
app = Flask(__name__)
VIDEOS_FOLDER = './videos'
app.config['VIDEOS_FOLDER'] = VIDEOS_FOLDER
MUSIC_FOLDER = './musique'
app.config['MUSIC_FOLDER'] = MUSIC_FOLDER
app.secret_key = 'votre_clé_secrète_unique'
def sanitize_filename(filename):
return re.sub(r'[\\/*?:"<>|]', "", filename)
def download_audio_with_ytdlp(url, output_path='./musique'):
print("Téléchargement de la musique lancé")
try:
if not os.path.exists(output_path):
os.makedirs(output_path)
# Récupérer les informations sur la vidéo avec les commandes direct
info_command = [
"yt-dlp",
"--dump-json",
"--extract-audio",
"--audio-format", "mp3",
url
]
result = subprocess.run(info_command, capture_output=True, text=True, check=True)
audio_info = json.loads(result.stdout)
audio_title = sanitize_filename(audio_info['title'])
audio_folder = os.path.join(output_path, audio_title)
if not os.path.exists(audio_folder):
os.makedirs(audio_folder)
# Télécharger l'audio avec la commande prompt
audio_command = [
"yt-dlp",
"--verbose",
"-x",
"--audio-format", "mp3",
url,
"-o", os.path.join(audio_folder, '%(title)s.%(ext)s')
]
subprocess.run(audio_command, check=True)
# Télécharger la miniature
thumbnail_command = [
"yt-dlp",
"--write-thumbnail",
"--skip-download",
url,
"-o", os.path.join(audio_folder, "thumbnail")
]
subprocess.run(thumbnail_command, check=True)
# Renommer la miniature en thumbnail.jpg
for file in os.listdir(audio_folder):
if file.startswith("thumbnail"):
thumbnail_ext = os.path.splitext(file)[1]
os.rename(os.path.join(audio_folder, file), os.path.join(audio_folder, "thumbnail.jpg"))
break
# Créer un fichier de vues
with open(os.path.join(audio_folder, "views.txt"), "w") as f:
f.write("0")
print(f"Téléchargement de la musique depuis {url} terminé dans {audio_folder}.")
except subprocess.CalledProcessError as e:
print(f"Erreur lors de l'exécution de yt-dlp : {e}")
except Exception as e:
print(f"Erreur générale : {e}")
def download_music_playlist_with_ytdlp(playlist_url, base_output_path='./musique'):
print("Téléchargement de la playlist de musique lancé")
try:
# Récupérer les infos de la playlist toujours avec les commandes
info_command = [
"yt-dlp",
"--flat-playlist",
"--dump-json",
playlist_url
]
result = subprocess.run(info_command, capture_output=True, text=True, check=True)
playlist_info = result.stdout.splitlines()[0]
playlist_data = json.loads(playlist_info)
print(playlist_data)
playlist_title = sanitize_filename(playlist_data.get('playlist', 'Unknown_Playlist'))
playlist_path = os.path.join(base_output_path, playlist_title)
if not os.path.exists(playlist_path):
os.makedirs(playlist_path)
# Commande pour télécharger les musiques en mp3 avec leurs miniatures
download_command = [
"yt-dlp",
"--verbose",
"-f", "bestaudio",
"--extract-audio",
"--audio-format", "mp3",
"--write-thumbnail",
playlist_url,
"-o", os.path.join(playlist_path, '%(playlist_index)s-%(title)s/%(title)s.%(ext)s')
]
subprocess.run(download_command, check=True)
# Organiser les fichiers et créer views.txt pour chaque musique (meme si inutile il faudrait l'enlever)
for item in os.listdir(playlist_path):
item_path = os.path.join(playlist_path, item)
if os.path.isdir(item_path):
for file in os.listdir(item_path):
if file.endswith(".jpg") or file.endswith(".webp"):
os.rename(os.path.join(item_path, file), os.path.join(item_path, "thumbnail.jpg"))
elif file.endswith(".mp3"):
pass
else:
os.remove(os.path.join(item_path, file))
with open(os.path.join(item_path, "views.txt"), "w") as f:
f.write("0")
print(f"Téléchargement de la playlist '{playlist_title}' terminé dans {playlist_path}.")
except subprocess.CalledProcessError as e:
print(f"Erreur lors de l'exécution de yt-dlp : {e}")
except Exception as e:
print(f"Erreur générale : {e}")
def download_video_with_ytdlp(url, output_path='.'):
print("Téléchargement de la vidéo lancé")
try:
if not os.path.exists(output_path):
os.makedirs(output_path)
# Récupérer les informations sur la vidéo
info_command = [
"yt-dlp",
"--dump-json",
url
]
result = subprocess.run(info_command, capture_output=True, text=True, check=True)
video_info = json.loads(result.stdout)
video_title = sanitize_filename(video_info['title'])
video_folder = os.path.join(output_path, video_title)
video_file_path = os.path.join(video_folder, f"{video_title}.mp4")
# Vérifier si la vidéo existe déjà
if os.path.exists(video_file_path):
print(f"Vidéo {video_title} déjà présente, téléchargement évité.")
return
# Télécharger la vidéo
video_command = [
"yt-dlp",
"--verbose",
"-f", "bestvideo[height<=720]+bestaudio/best[height<=720]",
"--merge-output-format", "mp4",
url,
"-o", os.path.join(video_folder, '%(title)s.%(ext)s')
]
subprocess.run(video_command, check=True)
# Télécharger la miniature
thumbnail_command = [
"yt-dlp",
"--write-thumbnail",
"--skip-download",
url,
"-o", os.path.join(video_folder, "thumbnail")
]
subprocess.run(thumbnail_command, check=True)
# Renommer la miniature en thumbnail.jpg
for file in os.listdir(video_folder):
if file.startswith("thumbnail"):
thumbnail_ext = os.path.splitext(file)[1]
os.rename(os.path.join(video_folder, file), os.path.join(video_folder, "thumbnail.jpg"))
break
# Créer un fichier de vues
with open(os.path.join(video_folder, "views.txt"), "w") as f:
f.write("0")
print(f"Téléchargement de la vidéo depuis {url} terminé dans {video_folder}.")
except subprocess.CalledProcessError as e:
print(f"Erreur lors de l'exécution de yt-dlp : {e}")
except Exception as e:
print(f"Erreur générale : {e}")
def download_playlist_with_ytdlp(playlist_url, base_output_path='./videos'):
print("Téléchargement de la playlist lancé")
try:
# Obtenir les informations de la playlist
info_command = [
"yt-dlp",
"--flat-playlist",
"--dump-json",
playlist_url
]
result = subprocess.run(info_command, capture_output=True, text=True, check=True)
playlist_info = result.stdout.splitlines()
playlist_size = len(playlist_info)
playlist_data = json.loads(playlist_info[0])
print(playlist_data)
playlist_title = sanitize_filename(playlist_data.get('playlist', 'Unknown_Playlist'))
playlist_path = os.path.join(base_output_path, playlist_title)
if not os.path.exists(playlist_path):
os.makedirs(playlist_path)
# Parcourir les vidéos de la playlist
for i, video_info in enumerate(playlist_info):
video_data = json.loads(video_info)
video_title = sanitize_filename(video_data['title'])
video_folder = os.path.join(playlist_path, f"{i+1:03d}-{video_title}")
video_file_path = os.path.join(video_folder, f"{video_title}.mp4")
print(f"\n {video_file_path}\n")
# Si la vidéo existe déjà, on passe à la suivante
if os.path.exists(video_file_path):
print(f"Vidéo {video_title} déjà présente, passage à la suivante.")
continue
# Télécharger la vidéo
download_command = [
"yt-dlp",
"--verbose",
"-f", "bestvideo[height<=720]+bestaudio/best[height<=720]",
"--merge-output-format", "mp4",
"--write-thumbnail",
f"https://www.youtube.com/watch?v={video_data['id']}",
"-o", os.path.join(video_folder, '%(title)s.%(ext)s')
]
subprocess.run(download_command, check=True)
# Renommer les dossiers après le téléchargement
"""for item in os.listdir(playlist_path):
item_path = os.path.join(playlist_path, item)
if os.path.isdir(item_path):
old_index = int(item.split('-')[0])
new_index = playlist_size - old_index + 1
new_name = f"{new_index:03d}-{'-'.join(item.split('-')[1:])}"
os.rename(item_path, os.path.join(playlist_path, new_name))"""
# Organiser les fichiers et créer views.txt pour chaque vidéo
for item in os.listdir(playlist_path):
item_path = os.path.join(playlist_path, item)
if os.path.isdir(item_path):
for file in os.listdir(item_path):
if file.endswith(".jpg") or file.endswith(".webp") or file.endswith(".png"):
# Renommer la miniature en thumbnail.jpg
os.rename(os.path.join(item_path, file), os.path.join(item_path, "thumbnail.jpg"))
elif file.endswith(".mp4"):
pass
else:
os.remove(os.path.join(item_path, file))
with open(os.path.join(item_path, "views.txt"), "w") as f:
f.write("0")
print(f"Téléchargement de la playlist '{playlist_title}' terminé dans {playlist_path}.")
except subprocess.CalledProcessError as e:
print(f"Erreur lors de l'exécution de yt-dlp : {e}")
except Exception as e:
print(f"Erreur générale : {e}")
def sanitize_filename(filename):
# Implémentez cette fonction pour nettoyer les noms de fichiers si nécessaire
return filename # Ceci est une implémentation simplifiée
@app.route('/')
def index():
return render_template('index.html')
@app.route('/download', methods=['GET', 'POST'])
def download():
if request.method == 'POST':
url = request.form['url']
# Démarrer un thread pour le téléchargement
if 'playlist' in url:
executor.submit(download_playlist_with_ytdlp, url, app.config['VIDEOS_FOLDER'])
else:
executor.submit(download_video_with_ytdlp, url, app.config['VIDEOS_FOLDER'])
return redirect(url_for('download'))
return render_template('download.html')
@app.route('/watch')
def watch():
search_query = request.args.get('search', '').lower()
videos = []
playlists = []
for item in os.listdir(app.config['VIDEOS_FOLDER']):
item_path = os.path.join(app.config['VIDEOS_FOLDER'], item)
if os.path.isdir(item_path):
if any(file.endswith('.mp4') for file in os.listdir(item_path)):
# This is a single video
if not search_query or search_query in item.lower():
views_file_path = os.path.join(item_path, "views.txt")
# Créer views.txt s'il n'existe pas
if not os.path.exists(views_file_path):
with open(views_file_path, "w") as f:
f.write("0") # Initialise les vues à 0
with open(views_file_path, "r") as f:
views = int(f.read().strip())
videos.append({'name': item, 'views': views})
else:
# This is a playlist
if not search_query or search_query in item.lower():
# Utiliser un fallback pour éviter l'exception StopIteration
first_video = next(
(subdir for subdir in os.listdir(item_path) if os.path.isdir(os.path.join(item_path, subdir))),
None)
# Vérifier que `first_video` n'est pas `None`
if first_video:
playlists.append({'name': item, 'first_video': first_video})
else:
print(f"Aucun sous-dossier trouvé dans la playlist : {item}")
if 'random' in request.args:
if videos:
return redirect(url_for('play_video', video=random.choice(videos)['name']))
elif playlists:
return redirect(url_for('play_playlist', playlist=random.choice(playlists)['name']))
random.shuffle(videos)
random.shuffle(playlists)
return render_template('watch.html', videos=videos, playlists=playlists)
@app.route('/play_video/<video>')
def play_video(video):
video_path = os.path.join(app.config['VIDEOS_FOLDER'], video)
views_file = os.path.join(video_path, "views.txt")
# Increment views
with open(views_file, "r+") as f:
views = int(f.read().strip())
views += 1
f.seek(0)
f.write(str(views))
f.truncate()
video_file = next(f for f in os.listdir(video_path) if f.endswith('.mp4'))
return render_template('play_video.html', video=video, video_file=video_file, views=views)
@app.route('/thumbnail/<path:filename>')
def serve_thumbnail(filename):
# Vérifier d'abord dans le dossier des vidéos
video_path = safe_join(app.config['VIDEOS_FOLDER'], filename)
if os.path.exists(video_path):
return send_from_directory(app.config['VIDEOS_FOLDER'], filename)
# Si non trouvé, vérifier dans le dossier des musiques
music_path = safe_join(app.config['MUSIC_FOLDER'], filename)
if os.path.exists(music_path):
return send_from_directory(app.config['MUSIC_FOLDER'], filename)
# Si toujours pas trouvé, renvoyer une erreur 404
return "Thumbnail not found", 404
@app.route('/play_playlist/<playlist>')
def play_playlist(playlist):
playlist_path = os.path.join(app.config['VIDEOS_FOLDER'], playlist)
videos = []
for item in sorted(os.listdir(playlist_path), reverse=False):
item_path = os.path.join(playlist_path, item)
if os.path.isdir(item_path):
mp4_files = [f for f in os.listdir(item_path) if f.endswith('.mp4')]
if mp4_files:
video_file = mp4_files[0]
views_file = os.path.join(item_path, "views.txt")
if os.path.exists(views_file):
with open(views_file, "r") as f:
views = int(f.read().strip())
else:
views = 0
thumbnail_path = os.path.join(item_path, "thumbnail.jpg")
if os.path.exists(thumbnail_path):
thumbnail_url = url_for('serve_thumbnail', filename=f"{playlist}/{item}/thumbnail.jpg")
else:
thumbnail_url = url_for('static', filename='default_thumbnail.jpg')
videos.append({
'name': item,
'file': video_file,
'views': views,
'thumbnail': thumbnail_url
})
if not videos:
#flash("Aucune vidéo trouvée dans cette playlist.", "warning")
return redirect(url_for('watch'))
return render_template('play_playlist.html', playlist=playlist, videos=videos)
@app.route('/video/<path:filename>')
def serve_video(filename):
return send_from_directory(app.config['VIDEOS_FOLDER'], filename)
@app.route('/download_music', methods=['GET', 'POST'])
def download_music():
if request.method == 'POST':
url = request.form['url']
# Démarrer un thread pour le téléchargement
if 'playlist' in url:
executor.submit(download_music_playlist_with_ytdlp, url, app.config['MUSIC_FOLDER'])
else:
executor.submit(download_audio_with_ytdlp, url, app.config['MUSIC_FOLDER'])
return redirect(url_for('download_music'))
return render_template('download_music.html')
@app.route('/listen_music')
def listen_music():
search_query = request.args.get('search', '').lower()
music_files = []
playlists = []
for item in os.listdir(app.config['MUSIC_FOLDER']):
item_path = os.path.join(app.config['MUSIC_FOLDER'], item)
if os.path.isdir(item_path):
if "views.txt" in os.listdir(item_path):
# C'est une musique individuelle
if not search_query or search_query in item.lower():
views_file_path = os.path.join(item_path, "views.txt")
with open(views_file_path, "r") as f:
views = int(f.read().strip())
mp3_file = next((f for f in os.listdir(item_path) if f.endswith('.mp3')), None)
if mp3_file:
music_files.append({
'name': item,
'mp3_file': os.path.join(item, mp3_file),
'views': views,
'thumbnail': url_for('serve_thumbnail', filename=f"{item}/thumbnail.jpg")
})
else:
# C'est une playlist
if not search_query or search_query in item.lower():
playlist_views = sum(int(open(os.path.join(item_path, subitem, "views.txt"), "r").read().strip())
for subitem in os.listdir(item_path)
if os.path.isdir(os.path.join(item_path, subitem))
and "views.txt" in os.listdir(os.path.join(item_path, subitem)))
# Trouver la première chanson de la playlist pour utiliser son image
first_song = next((subitem for subitem in os.listdir(item_path)
if os.path.isdir(os.path.join(item_path, subitem))), None)
thumbnail_url = url_for('static', filename='default_thumbnail.jpg')
if first_song:
thumbnail_path = os.path.join(item_path, first_song, "thumbnail.jpg")
if os.path.exists(thumbnail_path):
thumbnail_url = url_for('serve_thumbnail', filename=f"{item}/{first_song}/thumbnail.jpg")
playlists.append({
'name': item,
'views': playlist_views,
'thumbnail': thumbnail_url
})
random.shuffle(playlists)
random.shuffle(music_files)
return render_template('listen_music.html', music_files=music_files, playlists=playlists)
@app.route('/play_music/<music>')
def play_music(music):
# Construire le chemin absolu du dossier de la musique
music_path = os.path.join(app.config['MUSIC_FOLDER'], music)
print(music)
print(music_path)
music_path = "./musique/"+music.split("\\")[0]
print(music_path)
music = music.split("\\")[0]
# Vérifier si le dossier de la musique existe
if os.path.exists(music_path) and os.path.isdir(music_path):
# Vérifier le fichier de vues dans le dossier de la musique
views_file = os.path.join(music_path, "views.txt")
# Initialiser ou mettre à jour le compteur de vues
if os.path.exists(views_file):
with open(views_file, "r+") as f:
views = int(f.read().strip())
views += 1
f.seek(0)
f.write(str(views))
f.truncate()
else:
views = 0 # Si le fichier de vues n'existe pas, on initialise à 0
# Trouver le fichier .mp3 dans le dossier de la musique (pas dans les sous-dossiers)
mp3_file = next((f for f in os.listdir(music_path) if f.endswith('.mp3')), None)
if not mp3_file:
return "Fichier MP3 introuvable", 404
# Construire le chemin relatif pour le rendu dans le template
mp3_file_path = os.path.join(music, mp3_file).replace('\\', '/')
# Retourner le template avec le fichier MP3 et les vues
return render_template('play_music.html', music=music, mp3_file=mp3_file_path, views=views)
return "Dossier de musique introuvable", 404
import random
@app.route('/play_music_playlist/<playlist>')
@app.route('/play_music_playlist/<playlist>/<shuffle>')
def play_music_playlist(playlist, shuffle=False):
playlist_path = os.path.join(app.config['MUSIC_FOLDER'], playlist)
if os.path.exists(playlist_path):
music_files = []
for item in os.listdir(playlist_path):
item_path = os.path.join(playlist_path, item)
if os.path.isdir(item_path):
views_file_path = os.path.join(item_path, "views.txt")
print(views_file_path)
mp3_file = next((f for f in os.listdir(item_path) if f.endswith('.mp3')), None)
if mp3_file:
music_files.append({
'name': item,
'mp3_file': os.path.join(playlist, item, mp3_file),
})
# Mélange la playlist si shuffle est activé
if shuffle:
random.shuffle(music_files)
return render_template('play_music_playlist.html', playlist=playlist, music_files=music_files, shuffle=shuffle)
return "Playlist non trouvée", 404
@app.route('/music/<path:filename>')
def serve_music(filename):
# Décoder l'URL
filename = unquote(filename)
# Remplacer les backslashes par des forward slashes
filename = filename.replace('\\', '/')
# Construire le chemin sécurisé
safe_path = safe_join(app.config['MUSIC_FOLDER'], filename)
print(f"Tentative d'accès au fichier : {safe_path}") # Log pour le débogage
# Vérifier si le fichier existe
if not os.path.isfile(safe_path):
print(f"Fichier non trouvé: {safe_path}") # Log pour le débogage
abort(404, description="Fichier audio non trouvé")
# Obtenir le répertoire et le nom de fichier
directory = os.path.dirname(safe_path)
file_name = os.path.basename(safe_path)
try:
return send_from_directory(directory, file_name)
except Exception as e:
print(f"Erreur lors de l'envoi du fichier: {str(e)}") # Log pour le débogage
abort(500, description="Erreur lors de l'envoi du fichier audio")
@app.route('/download_music_playlist_all/<string:playlist>')
def download_music_playlist_all(playlist):
base_music_path = './musique'
playlist_path = os.path.join(base_music_path, playlist)
# Vérification de l'existence de la playlist
if not os.path.exists(playlist_path):
flash(f"La playlist '{playlist}' n'existe pas.", "error")
return redirect(url_for('listen_music'))
# Créer une archive ZIP en mémoire pour inclure tous les fichiers MP3
zip_buffer = io.BytesIO()
with zipfile.ZipFile(zip_buffer, "w", zipfile.ZIP_DEFLATED) as zip_file:
# Parcourir les fichiers et sous-dossiers de la playlist
for root, dirs, files in os.walk(playlist_path):
for file in files:
# Inclure uniquement les fichiers MP3
if file.endswith('.mp3'):
full_path = os.path.join(root, file)
zip_file.write(full_path, file) # Ajout sans structure de dossier
print(f"Ajouté au ZIP : {file}")
# Repositionner le pointeur du buffer pour lecture
zip_buffer.seek(0)
# Envoyer le fichier ZIP pour téléchargement
return send_file(zip_buffer, as_attachment=True, download_name=f"{playlist}.zip", mimetype='application/zip')
@app.route('/download_file/<path:filename>')
def download_file(filename):
print(f"Download file {filename}")
# Décoder l'URL
filename = unquote(filename)
# Remplacer les backslashes par des forward slashes
filename = filename.replace('\\', '/')
# Définir le dossier des musiques
music_folder = './musique'
# Construire le chemin sécurisé
safe_path = os.path.join(music_folder, filename)
print(f"Tentative d'accès au fichier : {safe_path}")
# Vérifier si le fichier existe
if not os.path.isfile(safe_path):
print(f"Fichier non trouvé: {safe_path}")
abort(404, description="Fichier audio non trouvé")
# Envoyer le fichier au client
return send_file(safe_path, as_attachment=True)
def get_playlist_thumbnail(playlist_name):
# Chemin du dossier de la playlist
playlist_folder = os.path.join(app.config['MUSIC_FOLDER'], playlist_name)
# Lister tous les fichiers dans le dossier
if os.path.exists(playlist_folder):
for file in os.listdir(playlist_folder):
# Vérifier si le fichier commence par '0'
if file.startswith('0') and (file.endswith('.jpg') or file.endswith('.jpeg') or file.endswith('.png')):
return os.path.join(playlist_folder, file)
# Retourne None si aucune miniature n'est trouvée
return None
@app.route('/delete/<media_type>/<path:name>', methods=['POST'])
def delete_media(media_type, name):
# Détermine le dossier en fonction du type de média
folder = app.config['VIDEOS_FOLDER'] if media_type == 'video' else app.config['MUSIC_FOLDER']
media_path = os.path.join(folder, name)
# Supprime le dossier si il existe
if os.path.exists(media_path):
shutil.rmtree(media_path)
flash(f"{name} supprimé avec succès.", "success")
else:
flash(f"{name} introuvable.", "error")
# Redirige vers la page appropriée
return redirect(url_for('watch') if media_type == 'video' else url_for('listen_music'))
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=17000)