Skip to main content

Start checking in my Pinboard scripts

ID
c595ce3
date
2024-02-14 22:22:37+00:00
author
Alex Chan <alex@alexwlchan.net>
parent
e95f0ad
message
Start checking in my Pinboard scripts
changed files
1 file, 45 additions

Changed files

web/save_pinboard_bookmarks.py (0) → web/save_pinboard_bookmarks.py (1058)

diff --git a/web/save_pinboard_bookmarks.py b/web/save_pinboard_bookmarks.py
new file mode 100755
index 0000000..03739c8
--- /dev/null
+++ b/web/save_pinboard_bookmarks.py
@@ -0,0 +1,45 @@
+#!/usr/bin/env python3
+
+import datetime
+import json
+import pathlib
+
+import httpx
+import keyring
+
+
+BACKUP_ROOT = pathlib.Path("/Volumes/Media (Sapphire)/backups/pinboard")
+
+
+def get_bookmarks_json(username: str, password: str) -> str:
+    """
+    Call the Pinboard API to get a complete list of my bookmarks.
+
+    Return the result as a pretty-printed JSON string.
+    """
+    resp = httpx.get(
+        "https://api.pinboard.in/v1/posts/all",
+        params={"format": "json"},
+        auth=(username, password),
+    )
+
+    resp.raise_for_status()
+
+    json_string = json.dumps(resp.json(), indent=2, sort_keys=True)
+
+    return json_string
+
+
+if __name__ == "__main__":
+    username = "alexwlchan"
+
+    password = keyring.get_password("pinboard", "password")
+    assert password is not None
+
+    json_string = get_bookmarks_json(username, password)
+
+    now = datetime.date.today().strftime("%Y-%m-%d")
+
+    for name in (f"bookmarks.{now}.json", "bookmarks.json"):
+        with open(BACKUP_ROOT / name, "w") as outfile:
+            outfile.write(json_string)