Created
June 29, 2025 15:30
-
-
Save Romern/af9c26a3fd3ca9d2372d7c0f1c85276b to your computer and use it in GitHub Desktop.
Import Folder Structure containign markdown notes into usememos (chatgpt generated)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import sys | |
import requests | |
def create_memos(baseurl, auth_cookie, folder_path): | |
headers = { | |
'Cookie': f'memos.access-token={auth_cookie}', | |
'Content-Type': 'application/json' | |
} | |
for root, _, files in os.walk(folder_path): | |
for file in files: | |
if file.endswith('.md'): | |
file_path = os.path.join(root, file) | |
with open(file_path, 'r', encoding='utf-8') as f: | |
content = f.read() | |
# Extract relative path components as hashtags | |
rel_path = os.path.relpath(root, folder_path) | |
hashtags = "" | |
if rel_path != ".": | |
parts = rel_path.split(os.sep) | |
hashtags = " " + " ".join(f"#{part.replace(' ','')}" for part in parts) | |
memo_text = str(file).replace(".md","") + ':\n' + content.strip() + '\n' + hashtags | |
payload = { | |
"content": memo_text | |
} | |
response = requests.post( | |
f"{baseurl}/api/v1/memos", | |
headers=headers, | |
json=payload | |
) | |
# print(payload) | |
if response.status_code == 200: | |
print(f"Created memo for: {file_path}") | |
else: | |
print(f"Failed to create memo for: {file_path}. Status: {response.status_code}. Response: {response.text}") | |
if __name__ == "__main__": | |
if len(sys.argv) != 4: | |
print(f"Usage: python {sys.argv[0]} <baseurl> <auth_cookie> <folder_path>") | |
sys.exit(1) | |
baseurl = sys.argv[1] | |
auth_cookie = sys.argv[2] | |
folder_path = sys.argv[3] | |
create_memos(baseurl, auth_cookie, folder_path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment