Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfixes and cleanups #4

Merged
merged 3 commits into from
Jan 6, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions convert_notes.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ def main():
lines.append("")
lines.append(tag_text)


# Create the new filename/path based on the first line of the note:
# But trim it to 248 characters so we can keep the entire thing -
# with the possible extra digit(s) added below - under 255 characters.
Expand All @@ -109,43 +108,43 @@ def main():
else:
filename = filename_start + ".md"

# Need to remove any forward slashes or colons:
filename = filename.replace("/", "").replace(":", "")
filepath = os.path.join(OUTPUT_DIRECTORY, filename)

# Keep track of this filename and how many times it's been used:
if filename in filenames:
filenames[filename] += 1
else:
filenames[filename] = 1

# Need to remove any forward slashes or colons:
filename = filename.replace("/", "").replace(":", "")
filepath = os.path.join(OUTPUT_DIRECTORY, filename)

if os.path.exists(filepath) is True:
if os.path.exists(filepath):
# Don't want to overwrite it!
# So, remove .md, and add the count of how many times this filename
# has been used to the end, to make it unique.
filename = f"{filename[:-3]} {filenames[filename]}.md"
filepath = os.path.join(OUTPUT_DIRECTORY, filename)

with open(filepath, "x") as outfile:
print(f"Writing {note['id']} to '{filepath}'")
with open(filepath, "w") as outfile:
outfile.write("\n".join(lines))

if KEEP_ORIGINAL_CREATION_TIME is True:
if KEEP_ORIGINAL_CREATION_TIME:
creation_time = datetime.strptime(
note["creationDate"], "%Y-%m-%dT%H:%M:%S.%fZ"
).strftime("%m/%d/%Y %H:%M:%S %p")
call(["SetFile", "-d", creation_time, filepath])

if KEEP_ORIGINAL_MODIFIED_TIME is True:
if KEEP_ORIGINAL_MODIFIED_TIME:
# Set the file access and modified times:
modified_time = datetime.strptime(
note["lastModified"], "%Y-%m-%dT%H:%M:%S.%fZ"
)
modified_time = modified_time.timestamp()
os.utime(filepath, (modified_time, modified_time))


print(f"\n{sum(filenames.values())} .md file(s) were created in {OUTPUT_DIRECTORY}")


if __name__ == "__main__":
main()
main()