Skip to main content

Add the initial version of this script

ID
86efa87
date
2023-12-10 10:10:24+00:00
author
Alex Chan <alex@alexwlchan.net>
parent
7193319
message
Add the initial version of this script
changed files
4 files, 68 additions, 1 deletion

Changed files

requirements.in (86) → requirements.in (96)

diff --git a/requirements.in b/requirements.in
index b8e8f1d..d5f7af5 100644
--- a/requirements.in
+++ b/requirements.in
@@ -1,5 +1,7 @@
 black
+bs4
 flake8
+httpx
 humanize
 hyperlink
 keyring

requirements.txt (1596) → requirements.txt (1976)

diff --git a/requirements.txt b/requirements.txt
index f2cdf8c..278fd1a 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -2,16 +2,24 @@
 # This file is autogenerated by pip-compile with Python 3.12
 # by the following command:
 #
-#    pip-compile
+#    pip-compile requirements.in
 #
+anyio==4.1.0
+    # via httpx
+beautifulsoup4==4.12.2
+    # via bs4
 black==23.11.0
     # via -r requirements.in
 brotli==1.1.0
     # via yt-dlp
+bs4==0.0.1
+    # via -r requirements.in
 build==1.0.3
     # via pip-tools
 certifi==2023.11.17
     # via
+    #   httpcore
+    #   httpx
     #   requests
     #   yt-dlp
 charset-normalizer==3.3.2
@@ -22,12 +30,20 @@ click==8.1.7
     #   pip-tools
 flake8==6.1.0
     # via -r requirements.in
+h11==0.14.0
+    # via httpcore
+httpcore==1.0.2
+    # via httpx
+httpx==0.25.2
+    # via -r requirements.in
 humanize==4.9.0
     # via -r requirements.in
 hyperlink==21.0.0
     # via -r requirements.in
 idna==3.4
     # via
+    #   anyio
+    #   httpx
     #   hyperlink
     #   requests
 jaraco-classes==3.3.0
@@ -68,6 +84,12 @@ pyproject-hooks==1.0.0
     # via build
 requests==2.31.0
     # via yt-dlp
+sniffio==1.3.0
+    # via
+    #   anyio
+    #   httpx
+soupsieve==2.5
+    # via beautifulsoup4
 termcolor==2.3.0
     # via -r requirements.in
 urllib3==2.1.0

textexpander/README.md (0) → textexpander/README.md (437)

diff --git a/textexpander/README.md b/textexpander/README.md
new file mode 100644
index 0000000..c215b88
--- /dev/null
+++ b/textexpander/README.md
@@ -0,0 +1,16 @@
+# textexpander
+
+These scripts I invoke as text expansion macros in [TextExpander](https://textexpander.com/).
+
+## The individual scripts
+
+<dl>
+  <dt>
+    <a href="https://github.com/alexwlchan/scripts/blob/main/obsidian/get_mastodon_text.py">
+      <code>get_mastodon_text.py</code>
+    </a>
+  </dt>
+  <dd>
+    print a Markdown-formatted blockquote of a Mastodon I've got open in Safari, suitable for pasting into Obsidian
+  </dd>
+</dl>

textexpander/get_mastodon_text.py (0) → textexpander/get_mastodon_text.py (715)

diff --git a/textexpander/get_mastodon_text.py b/textexpander/get_mastodon_text.py
new file mode 100755
index 0000000..2229e06
--- /dev/null
+++ b/textexpander/get_mastodon_text.py
@@ -0,0 +1,27 @@
+#!/usr/bin/env python3
+"""
+Look at the Mastodon URL in the frontmost Safari window, and print it
+as a blockquote.
+"""
+
+import subprocess
+
+import bs4
+import httpx
+
+
+if __name__ == "__main__":
+    url = (
+        subprocess.check_output(["/usr/local/bin/safari", "url"]).decode("utf8").strip()
+    )
+    resp = httpx.get(url)
+    soup = bs4.BeautifulSoup(resp, "html.parser")
+
+    text = soup.find("meta", attrs={"name": "description"}).attrs["content"]
+    author = soup.find("meta", attrs={"property": "og:title"}).attrs["content"]
+    url = soup.find("meta", attrs={"property": "og:url"}).attrs["content"]
+
+    print(f"[{author}]({url}):\n")
+
+    for line in text.splitlines():
+        print(f"> {line.rstrip()}")