[Python] I created fdm addon that put track number in the download of playlist with elephant

Discussão em 'Python' iniciado por Stack, Junho 30, 2025.

  1. Stack

    Stack Membro Participativo

    I created fdm addon that add trackk number to the download playlist when try in to donwload playlist it prompt you to add track numbers in the playlist but when i finished evrything and craeted the fdm addon which is addtrack.fda and loaded in fdm it tell me unknown error and when i checked the log of fdm it say performance issue so my code is like this manifest.json

    {
    "id": "addtrack",
    "name": "Add Track Numbers",
    "description": "Adds track numbers to playlist filenames in order.",
    "version": "1.0.0",
    "minApiVersion": 4,
    "minFeaturesLevel": 2,
    "author": "Mohamed Subarashi",
    "authorUrl": "https://www.facebook.com/mohamedsubarashi",
    "icon": "icon.png",
    "dependencies": {
    "Python": { "minVersion": "3.9" }
    },
    "entry": "main.py",
    "permissions": ["launchPython"]
    }


    my fdm main.py

    import os
    import json
    import tkinter as tk
    from tkinter import filedialog
    from pathlib import Path
    import getpass

    CONFIG_FILE = os.path.join(os.path.dirname(__file__), "config.json")
    SUPPORTED_EXTS = [".m3u", ".txt", ".json"]

    def load_config():
    if os.path.exists(CONFIG_FILE):
    with open(CONFIG_FILE, "r", encoding="utf-8") as f:
    return json.load(f)
    return {"remember": False, "enabled": None}

    def save_config(remember, enabled):
    with open(CONFIG_FILE, "w", encoding="utf-8") as f:
    json.dump({"remember": remember, "enabled": enabled}, f)

    def prompt_user_gui(config):
    result = {"enabled": None, "remember": False}
    def on_yes():
    result["enabled"] = True
    result["remember"] = remember_var.get()
    if result["remember"]:
    save_config(True, True)
    root.destroy()

    def on_no():
    result["enabled"] = False
    result["remember"] = remember_var.get()
    if result["remember"]:
    save_config(True, False)
    root.destroy()

    root = tk.Tk()
    root.title("Add Track Numbers?")
    root.geometry("340x140")
    root.resizable(False, False)

    tk.Label(root, text="Add track numbers to playlist filenames?", pady=10).pack()

    remember_var = tk.BooleanVar()
    tk.Checkbutton(root, text="Remember my choice", variable=remember_var).pack()

    button_frame = tk.Frame(root)
    button_frame.pack(pady=10)
    tk.Button(button_frame, text="Yes", width=10, command=on_yes).pack(side=tk.LEFT, padx=10)
    tk.Button(button_frame, text="No", width=10, command=on_no).pack(side=tk.RIGHT, padx=10)

    root.mainloop()
    config["enabled"] = result["enabled"]
    config["remember"] = result["remember"]
    return config

    def apply_track_numbers(file_path):
    try:
    with open(file_path, 'r', encoding='utf-8') as f:
    lines = [line.strip() for line in f.readlines() if line.strip()]
    except Exception:
    print(f"⚠️ Couldn't read file: {file_path}")
    return

    updated = []
    for i, line in enumerate(lines):
    if not line.lower().endswith(('.mp4', '.mkv', '.mp3', '.avi')):
    updated.append(line)
    continue
    filename = os.path.basename(line)
    directory = os.path.dirname(line)
    new_filename = f"{str(i + 1).zfill(3)}. {filename}"
    new_path = os.path.join(directory, new_filename)
    updated.append(new_path)

    with open(file_path, 'w', encoding='utf-8') as f:
    f.write('\n'.join(updated))
    print(f"✔️ Modified: {file_path}")

    def scan_fdm_folder():
    user = getpass.getuser()
    base_paths = [
    f"C:/Users/{user}/Downloads",
    f"C:/Users/{user}/AppData/Roaming/FDM",
    f"C:/Users/{user}/AppData/Local/Free Download Manager"
    ]
    found = []
    for path in base_paths:
    if os.path.exists(path):
    try:
    for file in os.listdir(path):
    if Path(file).suffix.lower() in SUPPORTED_EXTS:
    found.append(os.path.join(path, file))
    except Exception:
    continue
    return found

    def drop_file_interface():
    root = tk.Tk()
    root.withdraw()
    file_path = filedialog.askopenfilename(
    title="Drop a playlist file or browse manually",
    filetypes=[("Playlist Files", "*.txt *.m3u *.json")]
    )
    return file_path

    def main():
    config = load_config()

    files = scan_fdm_folder()
    if not files:
    file = drop_file_interface()
    if not file:
    print("No file selected.")
    return
    files = [file]

    if not config.get("remember"):
    config = prompt_user_gui(config)

    if config.get("enabled"):
    for f in files:
    apply_track_numbers(f)
    else:
    print("Track numbering not applied.")

    if __name__ == "__main__":
    main()


    Please help me to fix this issue.

    I want it to work which is not working correctly.

    Continue reading...

Compartilhe esta Página