From 216fa78eeb704a77c69fcd5cffcfa66887c561f2 Mon Sep 17 00:00:00 2001 From: Grizouille Date: Fri, 7 Nov 2025 23:21:46 +0100 Subject: [PATCH] Ajout update nextcloud file index --- .../cli/deezer-downloader.ini.template | 21 ++++++++ deezer_downloader/deezer.py | 2 - deezer_downloader/nextcloud.py | 18 ++----- deezer_downloader/update_nextcloud_files.py | 4 +- deezer_downloader/web/app.py | 23 +++++---- deezer_downloader/web/music_backend.py | 51 ++++++++++++------- 6 files changed, 74 insertions(+), 45 deletions(-) diff --git a/deezer_downloader/cli/deezer-downloader.ini.template b/deezer_downloader/cli/deezer-downloader.ini.template index 08ae05d..6ae3b41 100644 --- a/deezer_downloader/cli/deezer-downloader.ini.template +++ b/deezer_downloader/cli/deezer-downloader.ini.template @@ -56,4 +56,25 @@ quality = mp3 ; command = /home/kmille/projects/deezer-downloader/app/venv/bin/yt-dlp command = /usr/bin/yt-dlp + +[nextcloud] +; Your nextcloud public key for decrypt jwt token +public_key = -----BEGIN PUBLIC KEY----- + ... + -----END PUBLIC KEY----- +; Your key to added from nextcloud ExternalApp exp: /?jwt={jwt} +jwt_name_parameter = jwt + +[nextcloud_ssh_scan_files] +; ip from nextcloud server +host = 192.168.10.9 +; port ssh from nextcloud server +port = 22 +; username ssh nextcloud server +username = nextclouddeezer +; password ssh nextcloud server +password = Q8QbqQru3hRc16vK +; occ path nextcloud server +occ_path = /var/www/nextcloud/occ + ; vim: syntax=dosini diff --git a/deezer_downloader/deezer.py b/deezer_downloader/deezer.py index f07d187..07a4066 100644 --- a/deezer_downloader/deezer.py +++ b/deezer_downloader/deezer.py @@ -15,7 +15,6 @@ from mutagen.flac import FLAC, Picture from mutagen.mp3 import MP3 from mutagen.id3 import PictureType, TIT2, TALB, TPE1, TRCK, TDRC, TPOS, APIC, TPE2 from mutagen import MutagenError -from deezer_downloader.update_nextcloud_files import call_scan # BEGIN TYPES @@ -246,7 +245,6 @@ def download_song(song: dict, output_file: str) -> None: except Exception as e: raise DeezerApiException(f"Could not write song to disk: {e}") from e - call_scan(output_file) print("Download finished: {}".format(output_file)) diff --git a/deezer_downloader/nextcloud.py b/deezer_downloader/nextcloud.py index caa3733..87a960e 100644 --- a/deezer_downloader/nextcloud.py +++ b/deezer_downloader/nextcloud.py @@ -45,25 +45,13 @@ def addJwtInUserSession(request): print("UID:", uid) print("Display Name:", display_name) - #base_dir_user = base_dir.format(user=uid) - #config["download_dirs"]["base"] = base_dir_user - session['user_uid'] = uid session['user_email'] = email session['user_displayName'] = display_name - session['user_base_dir'] = config["download_dirs"]["base"] + "\\" + uid - #createDownloadDirectoriesIfNotExist() + session['user_base_dir'] = config["download_dirs"]["base"] print(f"✅ Download base dir set to: {session['user_base_dir']}") except ExpiredSignatureError: - return jsonify({"error": "Token expired"}), 401 + abort(403) except InvalidTokenError as e: - print("❌ Erreur de validation du token :", e) - -def createDownloadDirectoriesIfNotExist(): - os.makedirs(config["download_dirs"]["base"], exist_ok=True) - os.makedirs(config["download_dirs"]["songs"], exist_ok=True) - os.makedirs(config["download_dirs"]["albums"], exist_ok=True) - os.makedirs(config["download_dirs"]["zips"], exist_ok=True) - os.makedirs(config["download_dirs"]["playlists"], exist_ok=True) - os.makedirs(config["download_dirs"]["youtubedl"], exist_ok=True) \ No newline at end of file + print("❌ Erreur de validation du token :", e) \ No newline at end of file diff --git a/deezer_downloader/update_nextcloud_files.py b/deezer_downloader/update_nextcloud_files.py index 799ff1a..e092e83 100644 --- a/deezer_downloader/update_nextcloud_files.py +++ b/deezer_downloader/update_nextcloud_files.py @@ -11,8 +11,10 @@ def call_scan(directory_path): password = config['nextcloud_ssh_scan_files']['password'] occCmd= config['nextcloud_ssh_scan_files']['occ_path'] + if isinstance(directory_path, (list, tuple)) and all(isinstance(x, str) for x in directory_path): + directory_path = directory_path[0] directory_path = os.path.dirname(directory_path) - directory_path = directory_path.replace(config['download_dirs']['base'] + "\\", "") + directory_path = directory_path.replace(config['download_dirs']['base'], "").lstrip("/\\") print("Path Nexcloud Scan " + directory_path) # Commande à exécuter diff --git a/deezer_downloader/web/app.py b/deezer_downloader/web/app.py index 279daea..62967a0 100644 --- a/deezer_downloader/web/app.py +++ b/deezer_downloader/web/app.py @@ -4,14 +4,14 @@ from subprocess import Popen, PIPE from functools import wraps import requests import atexit -from flask import Flask, render_template, request, jsonify, session, make_response +from flask import Flask, render_template, request, jsonify, session from markupsafe import escape from flask_autoindex import AutoIndex import warnings import giphypop from deezer_downloader.configuration import config -from deezer_downloader.web.music_backend import sched, check_and_set_download_dirs_exist +from deezer_downloader.web.music_backend import sched from deezer_downloader.deezer import deezer_search, init_deezer_session from deezer_downloader.nextcloud import addJwtInUserSession @@ -173,18 +173,19 @@ def deezer_download_song_or_album(): add_to_playlist: True|False (add to mpd playlist) create_zip: True|False (create a zip for the album) """ - check_and_set_download_dirs_exist() user_input = request.get_json(force=True) desc = "Downloading {}".format(user_input['type']) if user_input['type'] == "track": task = sched.enqueue_task(desc, "download_deezer_song_and_queue", track_id=user_input['music_id'], - add_to_playlist=user_input['add_to_playlist']) + add_to_playlist=user_input['add_to_playlist'], + uid_user=session['user_uid']) else: task = sched.enqueue_task(desc, "download_deezer_album_and_queue_and_zip", album_id=user_input['music_id'], add_to_playlist=user_input['add_to_playlist'], - create_zip=user_input['create_zip']) + create_zip=user_input['create_zip'], + uid_user=session['user_uid']) return jsonify({"task_id": id(task), }) @@ -201,7 +202,8 @@ def youtubedl_download(): desc = "Downloading via youtube-dl" task = sched.enqueue_task(desc, "download_youtubedl_and_queue", video_url=user_input['url'], - add_to_playlist=user_input['add_to_playlist']) + add_to_playlist=user_input['add_to_playlist'], + uid_user=session['user_uid']) return jsonify({"task_id": id(task), }) @@ -221,7 +223,8 @@ def deezer_playlist_download(): task = sched.enqueue_task(desc, "download_deezer_playlist_and_queue_and_zip", playlist_id=user_input['playlist_url'], add_to_playlist=user_input['add_to_playlist'], - create_zip=user_input['create_zip']) + create_zip=user_input['create_zip'], + uid_user=session['user_uid']) return jsonify({"task_id": id(task), }) @@ -244,7 +247,8 @@ def spotify_playlist_download(): playlist_name=user_input['playlist_name'], playlist_id=user_input['playlist_url'], add_to_playlist=user_input['add_to_playlist'], - create_zip=user_input['create_zip']) + create_zip=user_input['create_zip'], + uid_user=session['user_uid']) return jsonify({"task_id": id(task), }) @@ -265,5 +269,6 @@ def deezer_favorites_download(): task = sched.enqueue_task(desc, "download_deezer_favorites", user_id=user_input['user_id'], add_to_playlist=user_input['add_to_playlist'], - create_zip=user_input['create_zip']) + create_zip=user_input['create_zip'], + uid_user=session['user_uid']) return jsonify({"task_id": id(task), }) diff --git a/deezer_downloader/web/music_backend.py b/deezer_downloader/web/music_backend.py index 2ad8982..419e2aa 100644 --- a/deezer_downloader/web/music_backend.py +++ b/deezer_downloader/web/music_backend.py @@ -4,8 +4,8 @@ from os.path import basename import mpd import platform from zipfile import ZipFile, ZIP_DEFLATED -from flask import session +from deezer_downloader.update_nextcloud_files import call_scan from deezer_downloader.configuration import config from deezer_downloader.youtubedl import youtubedl_download from deezer_downloader.spotify import get_songs_from_spotify_website @@ -17,16 +17,17 @@ from deezer_downloader.threadpool_queue import ThreadpoolScheduler, report_progr sched = ThreadpoolScheduler() -def check_and_set_download_dirs_exist(): - config["download_dirs"]["songs"] = os.path.join(session['user_base_dir'], "DeezerDownload", "songs") - config["download_dirs"]["zips"] = os.path.join(session['user_base_dir'], "DeezerDownload","zips") - config["download_dirs"]["albums"] = os.path.join(session['user_base_dir'], "DeezerDownload","albums") - config["download_dirs"]["playlists"] = os.path.join(session['user_base_dir'], "DeezerDownload","playlists") - config["download_dirs"]["youtubedl"] = os.path.join(session['user_base_dir'], "DeezerDownload","youtubedl") +def check_and_set_download_dirs_exist(uid_user): + config["download_dirs"]["songs"] = config["download_dirs"]["songs"].format(uid=uid_user) + config["download_dirs"]["zips"] = config["download_dirs"]["zips"].format(uid=uid_user) + config["download_dirs"]["albums"] = config["download_dirs"]["albums"].format(uid=uid_user) + config["download_dirs"]["playlists"] = config["download_dirs"]["playlists"].format(uid=uid_user) + config["download_dirs"]["youtubedl"] = config["download_dirs"]["youtubedl"].format(uid=uid_user) for directory in [config["download_dirs"]["songs"], config["download_dirs"]["zips"], config["download_dirs"]["albums"], config["download_dirs"]["playlists"], config["download_dirs"]["youtubedl"]]: os.makedirs(directory, exist_ok=True) + def make_song_paths_relative_to_mpd_root(songs, prefix=""): # ensure last slash config["mpd"]["music_dir_root"] = os.path.join(config["mpd"]["music_dir_root"], '') @@ -118,7 +119,8 @@ def download_song_and_get_absolute_filename(search_type, song, playlist_name=Non return absolute_filename -def create_zip_file(songs_absolute_location): +def create_zip_file(songs_absolute_location, uid_user): + check_and_set_download_dirs_exist(uid_user) # take first song in list and take the parent dir (name of album/playlist") parent_dir = basename(os.path.dirname(songs_absolute_location[0])) location_zip_file = os.path.join(config["download_dirs"]["zips"], "{}.zip".format(parent_dir)) @@ -130,6 +132,7 @@ def create_zip_file(songs_absolute_location): zip.write(song_location, arcname=os.path.join(parent_dir, basename(song_location))) except FileNotFoundError: print("Could not find file '{}'".format(song_location)) + call_scan(location_zip_file) print("Done with the zip") return location_zip_file @@ -150,10 +153,12 @@ def create_m3u8_file(songs_absolute_location): @sched.register_command() -def download_deezer_song_and_queue(track_id, add_to_playlist): +def download_deezer_song_and_queue(track_id, add_to_playlist, uid_user): + check_and_set_download_dirs_exist(uid_user) song = get_song_infos_from_deezer_website(TYPE_TRACK, track_id) try: absolute_filename = download_song_and_get_absolute_filename(TYPE_TRACK, song) + call_scan(absolute_filename) update_mpd_db(absolute_filename, add_to_playlist) return make_song_paths_relative_to_mpd_root([absolute_filename]) except DeezerApiException: @@ -162,7 +167,8 @@ def download_deezer_song_and_queue(track_id, add_to_playlist): @sched.register_command() -def download_deezer_album_and_queue_and_zip(album_id, add_to_playlist, create_zip): +def download_deezer_album_and_queue_and_zip(album_id, add_to_playlist, create_zip, uid_user): + check_and_set_download_dirs_exist(uid_user) songs = get_song_infos_from_deezer_website(TYPE_ALBUM, album_id) songs_absolute_location = [] for i, song in enumerate(songs): @@ -173,14 +179,16 @@ def download_deezer_album_and_queue_and_zip(album_id, add_to_playlist, create_zi songs_absolute_location.append(absolute_filename) except Exception as e: print(f"Warning: {e}. Continuing with album...") + call_scan(songs_absolute_location) update_mpd_db(songs_absolute_location, add_to_playlist) if create_zip: - return [create_zip_file(songs_absolute_location)] + return [create_zip_file(songs_absolute_location, uid_user)] return make_song_paths_relative_to_mpd_root(songs_absolute_location) @sched.register_command() -def download_deezer_playlist_and_queue_and_zip(playlist_id, add_to_playlist, create_zip): +def download_deezer_playlist_and_queue_and_zip(playlist_id, add_to_playlist, create_zip, uid_user): + check_and_set_download_dirs_exist(uid_user) playlist_name, songs = parse_deezer_playlist(playlist_id) songs_absolute_location = [] for i, song in enumerate(songs): @@ -192,13 +200,15 @@ def download_deezer_playlist_and_queue_and_zip(playlist_id, add_to_playlist, cre print(f"Warning: {e}. Continuing with playlist...") update_mpd_db(songs_absolute_location, add_to_playlist) songs_with_m3u8_file = create_m3u8_file(songs_absolute_location) + call_scan(songs_absolute_location) if create_zip: - return [create_zip_file(songs_with_m3u8_file)] + return [create_zip_file(songs_with_m3u8_file, uid_user)] return make_song_paths_relative_to_mpd_root(songs_absolute_location) @sched.register_command() -def download_spotify_playlist_and_queue_and_zip(playlist_name, playlist_id, add_to_playlist, create_zip): +def download_spotify_playlist_and_queue_and_zip(playlist_name, playlist_id, add_to_playlist, create_zip, uid_user): + check_and_set_download_dirs_exist(uid_user) songs = get_songs_from_spotify_website(playlist_id, config["proxy"]["server"]) songs_absolute_location = [] @@ -215,22 +225,26 @@ def download_spotify_playlist_and_queue_and_zip(playlist_name, playlist_id, add_ print(f"Warning: Could not download Spotify song ({song_of_playlist}) on Deezer: {e}") update_mpd_db(songs_absolute_location, add_to_playlist) songs_with_m3u8_file = create_m3u8_file(songs_absolute_location) + call_scan(songs_absolute_location) if create_zip: - return [create_zip_file(songs_with_m3u8_file)] + return [create_zip_file(songs_with_m3u8_file, uid_user)] return make_song_paths_relative_to_mpd_root(songs_absolute_location) @sched.register_command() -def download_youtubedl_and_queue(video_url, add_to_playlist): +def download_youtubedl_and_queue(video_url, add_to_playlist, uid_user): + check_and_set_download_dirs_exist(uid_user) filename_absolute = youtubedl_download(video_url, config["download_dirs"]["youtubedl"], config["proxy"]["server"]) + call_scan(filename_absolute) update_mpd_db(filename_absolute, add_to_playlist) return make_song_paths_relative_to_mpd_root([filename_absolute]) @sched.register_command() -def download_deezer_favorites(user_id: str, add_to_playlist: bool, create_zip: bool): +def download_deezer_favorites(user_id: str, add_to_playlist: bool, create_zip: bool, uid_user): + check_and_set_download_dirs_exist(uid_user) songs_absolute_location = [] output_directory = f"favorites_{user_id}" favorite_songs = get_deezer_favorites(user_id) @@ -248,8 +262,9 @@ def download_deezer_favorites(user_id: str, add_to_playlist: bool, create_zip: b print(f"Could not find song ({fav_song}) on Deezer?") update_mpd_db(songs_absolute_location, add_to_playlist) songs_with_m3u8_file = create_m3u8_file(songs_absolute_location) + call_scan(songs_absolute_location) if create_zip: - return [create_zip_file(songs_with_m3u8_file)] + return [create_zip_file(songs_with_m3u8_file, uid_user)] return make_song_paths_relative_to_mpd_root(songs_absolute_location)