anonfaded / robospeaker101

Python tool for text-to-speech conversion with voice selection, usage history tracking and file saving capabilities, for Linux, Windows, Android and Mac.
GNU General Public License v3.0
1 stars 1 forks source link

Enhance: Make the 'directory making feature' dynamic #2

Open anonfaded opened 4 months ago

anonfaded commented 4 months ago

Right now the tool has a hardcoded feature where the saving of audio and history feature makes two folders in the location where the tool is present, but if we open the tool and its not in a directory then it doesn't make the directories.

Task to do:

muddi900 commented 4 months ago

Would something like this work?


- def save_to_history(text):
+ def save_to_history(text, dir_path=None):
    # Get current date
    current_date = datetime.now().strftime("%Y-%m-%d")

    # Create history folder if it doesn't exist
-    history_folder = "history"
+   history_folder = dir_path or "history"
    os.makedirs(history_folder, exist_ok=True)

    # Create subfolder for current date if it doesn't exist
    date_folder = os.path.join(history_folder, current_date)
    os.makedirs(date_folder, exist_ok=True)

    # Create markdown file for current date if it doesn't exist
    markdown_file = os.path.join(date_folder, "history.md")

    # Write text to markdown file with a timestamp
    with open(markdown_file, "a") as file:
        file.write(f"\n\n---\n\n{datetime.now().strftime('%H:%M:%S')}\n{text}\n")
anonfaded commented 4 months ago

Thanks for the suggestion! To ensure directories are created correctly regardless of where the script is run, we should use os.getcwd() to get the current working directory. Here's an example:

import os
cwd = os.getcwd()
history_folder = os.path.join(cwd, 'history')
os.makedirs(history_folder, exist_ok=True)

This ensures the directories are always created in the correct location. Test after that locally to make sure it works accordingly.