maelchiotti / LocalMaterialNotes

Simple, local, material design notes
https://play.google.com/store/apps/details?id=com.maelchiotti.localmaterialnotes
GNU Affero General Public License v3.0
115 stars 7 forks source link

Importing Samsung Notes (.txt) (v4.4.10.71) #218

Open 1ycx opened 4 days ago

1ycx commented 4 days ago

Samsung notes when exported as text are named in this way - <note name>_<YYMMDD>_<HHMMSS>.txt. This is a simple script in python to combine those text files to a json file and importing it worked fine.

import json
import os
import datetime
import re

def txt_to_json(input_folder, output_file):
    """Converts TXT files with date/time in filenames to a JSON file."""

    notes = []
    for filename in os.listdir(input_folder):
        if filename.endswith(".txt"):
            filepath = os.path.join(input_folder, filename)

            # Extract date and time from filename using regular expression
            match = re.match(r".*_(\d{6})_(\d{6})\.txt", filename)
            if match:
                try:
                    date_str = match.group(1)
                    time_str = match.group(2)
                    dt = datetime.datetime.strptime(date_str + time_str, "%y%m%d%H%M%S")
                    timestamp = dt.isoformat()
                except ValueError:
                    print(f"Invalid date/time format in filename: {filename}")
                    timestamp = datetime.datetime.now().isoformat()  # Fallback
            else:
                print(f"Could not extract date/time from filename: {filename}")
                timestamp = datetime.datetime.now().isoformat()  # Fallback

            try:
                with open(filepath, 'r', encoding='utf-8') as f:
                    content = f.read()

                title = filename.split("_")[0]  # Extract title

                note = {
                    "deleted": False,
                    "pinned": False,
                    "created_time": timestamp,
                    "edited_time": timestamp,
                    "title": title,
                    "content": json.dumps([{"insert": content}])
                }
                notes.append(note)

            except Exception as e:
                print(f"Error processing {filename}: {e}")

    # Create the JSON structure
    json_data = {
        "version": "1.5.0",
        "encrypted": False,
        "notes": notes
    }

    # Write the JSON data to the output file
    try:
        with open(output_file, 'w', encoding='utf-8') as outfile:
            json.dump(json_data, outfile, indent=4, ensure_ascii=False)
        print(f"JSON data written to {output_file}")

    except Exception as e:
        print(f"Error writing to {output_file}: {e}")

# Example usage:
input_folder = r"/path/to/your/txt/files/folder"  # Replace with your folder path
output_file = r"/path/to/output/output.json"  # Replace with desired output filename
txt_to_json(input_folder, output_file)

I can submit a PR if you want, don't know where it would should be placed.

maelchiotti commented 4 days ago

Thank you very much @1ycx for this script! I'll add it under the docs section, and mention it in the README. I'll also credit you here.