Skip to content

Commit

Permalink
add note support
Browse files Browse the repository at this point in the history
  • Loading branch information
yinan-c committed Nov 19, 2024
1 parent 56c00c2 commit 0463e3b
Showing 1 changed file with 44 additions and 16 deletions.
60 changes: 44 additions & 16 deletions hoard.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,63 @@

def add_bookmark(url):
"""Add a new bookmark to Hoarder"""
payload = {
"type": "link",
"url": url,
"title": None,
"archived": False,
"favourited": False,
"note": "",
"summary": "",
"tags": []
}
def create_payload(content, type="link"):
if type == "link":
return {
"type": "link",
"url": content,
"title": None,
"archived": False,
"favourited": False,
"note": "",
"summary": "",
"tags": []
}
else:
return {
"type": "text",
"text": content,
"title": None,
"archived": False,
"favourited": False,
"note": "",
"summary": "",
"tags": []
}

try:
payload = create_payload(url, "link")
response = requests.post(HOARDER_API_URL, headers=HEADERS, json=payload)
response.raise_for_status()
data = response.json()
bookmark_id = data.get("id", "Unknown ID")
return f"Successfully added bookmark with ID: {bookmark_id}"
return f"Hoarded bookmark with ID: {bookmark_id}"
except requests.exceptions.RequestException as e:
error_detail = ""
if hasattr(e.response, 'text'):
try:
error_detail = f" - Details: {json.loads(e.response.text)}"
error_json = json.loads(e.response.text)
error_detail = f" - Details: {error_json}"

if "Invalid" in str(error_json):
try:
text_payload = create_payload(url, "text")
#print(f"Trying text payload: {json.dumps(text_payload, indent=2)}")
response = requests.post(HOARDER_API_URL, headers=HEADERS, json=text_payload)
response.raise_for_status()
data = response.json()
bookmark_id = data.get("id", "Unknown ID")
return f"Hoarded as text with ID: {bookmark_id}"
except requests.exceptions.RequestException as text_e:
#print(f"Text error response: {text_e.response.text if hasattr(text_e.response, 'text') else text_e}")
return f"Error adding as text: {text_e}"
except json.JSONDecodeError:
error_detail = f" - Response: {e.response.text}"

print(f"Debug Info:")
print(f"URL: {HOARDER_API_URL}")
print(f"Headers: {HEADERS}")
print(f"Payload: {json.dumps(payload, indent=2)}")
#print(f"Debug Info:")
#print(f"URL: {HOARDER_API_URL}")
#print(f"Headers: {HEADERS}")
#print(f"Payload: {json.dumps(payload, indent=2)}")
return f"Error adding bookmark: {e}{error_detail}"

if __name__ == "__main__":
Expand Down

0 comments on commit 0463e3b

Please sign in to comment.