fjxmlzn / FindMyHistory

Track your Apple devices and look up their past location, battery levels, and more
MIT License
308 stars 34 forks source link

Fix the _save_log function (sanitize filename) #19

Open tyhallcsu opened 1 year ago

tyhallcsu commented 1 year ago

My device name was:

MyName's iPhone iOS 7.1.1 (06/24/14)

This caused an issue in generating file paths with special characters and spaces in the filename.

File "/Users/tylerhall/Downloads/AIRTAGS/FindMyHistory-main/lib/log_manager.py", line 74, in _save_log
    with open(path, 'w') as f:
FileNotFoundError: [Errno 2] No such file or directory: "log/2023-08-23/MyName's iPhone iOS 7.1.1 (06/24/14)__NULL.csv"

Here is my proposed fix in log_manager.py's _save_log function.

Here's the modified log_manager.py file that includes the _sanitize_filename function to handle filenames with spaces and special characters:

` def _sanitize_filename(self, name):

Replace spaces and special characters with underscores

    return name.replace(' ', '_').replace('/', '_').replace('\\', '_')

def _save_log(self, name, data):
    log_folder = self._log_folder
    if not self._no_date_folder:
        log_folder = os.path.join(
            log_folder, datetime.now().strftime(self._date_format))
    if not os.path.exists(log_folder):
        os.makedirs(log_folder)

    sanitized_name = self._sanitize_filename(name)
    path = os.path.join(log_folder, sanitized_name + '.csv')

    if not os.path.exists(path):
        with open(path, 'w') as f:
            writer = csv.writer(f)
            writer.writerow(self._keys)

    with open(path, 'a') as f:
        writer = csv.writer(f)
        writer.writerow([data[k] for k in self._keys])
        `

You can add the _sanitize_filename function to your existing LogManager class to handle filename sanitization. The function replaces spaces, slashes, and backslashes in the name with underscores to create a valid filename.

This modification should help you avoid issues with special characters and spaces in the filenames when creating the CSV files. Just ensure that this updated log_manager.py file is used in conjunction with the rest of your code.

tyhallcsu commented 1 year ago

Here is the fix:

image