Skip to content

Instantly share code, notes, and snippets.

@rubiojr
Created August 26, 2024 14:07
Show Gist options
  • Save rubiojr/74594904f099b9c67f1d4ae9adaba438 to your computer and use it in GitHub Desktop.
Save rubiojr/74594904f099b9c67f1d4ae9adaba438 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import http.client
import sys, time
import json
import tempfile
import os
def client():
return http.client.HTTPConnection("localhost", 12002, timeout=10)
def get_repo():
c = client()
c.request("GET", "/api/open-repositories")
j = c.getresponse().read()
return json.loads(j)[0]
print("Type note, press Ctrl-D to finish reading")
content = ""
for line in sys.stdin:
content += line
temp_file = tempfile.NamedTemporaryFile(mode="w", delete=False)
temp_file.write(content)
temp_file.close()
# Clean up function to remove the temporary file
def cleanup():
time.sleep(5)
os.unlink(temp_file.name)
## Register the cleanup function to be called at exit
import atexit
atexit.register(cleanup)
repo = get_repo()["instance_id"]
req = {
"repo": repo,
"filenames": [temp_file.name],
"data_source_name": "generic",
"data_source_options": {},
"processing_options": {
"item_unique_constraints": {
"filename": True,
"timestamp": True,
"data": True,
}
},
}
c = client()
c.request("POST", "/api/import", json.dumps(req))
if c.getresponse().status == 200:
print("Note added successfully.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment