Skip to main content

Add a script for saving tweets in Obsidian

ID
0b94491
date
2024-01-04 23:45:41+00:00
author
Alex Chan <alex@alexwlchan.net>
parent
ef591c9
message
Add a script for saving tweets in Obsidian
changed files
5 files, 96 additions, 4 deletions

Changed files

requirements.in (179) → requirements.in (189)

diff --git a/requirements.in b/requirements.in
index 2403d3d..f5f0d14 100644
--- a/requirements.in
+++ b/requirements.in
@@ -1,6 +1,7 @@
 attrs
 black
 boto3
+bs4
 cogapp
 flake8
 flickr-photos-api
@@ -15,5 +16,6 @@ pillow_heif
 pip-tools
 pypdf
 pytest
+regex
 termcolor
 yt-dlp

requirements.txt (2716) → requirements.txt (2880)

diff --git a/requirements.txt b/requirements.txt
index 8c66b8b..859640f 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -8,6 +8,8 @@ anyio==4.1.0
     # via httpx
 attrs==23.1.0
     # via -r requirements.in
+beautifulsoup4==4.12.2
+    # via bs4
 black==23.11.0
     # via -r requirements.in
 boto3==1.33.11
@@ -18,6 +20,8 @@ botocore==1.33.11
     #   s3transfer
 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
@@ -116,6 +120,8 @@ pytest==7.4.3
     # via -r requirements.in
 python-dateutil==2.8.2
     # via botocore
+regex==2023.12.25
+    # via -r requirements.in
 requests==2.31.0
     # via yt-dlp
 s3transfer==0.8.2
@@ -126,6 +132,8 @@ sniffio==1.3.0
     # via
     #   anyio
     #   httpx
+soupsieve==2.5
+    # via beautifulsoup4
 tenacity==8.2.3
     # via flickr-photos-api
 termcolor==2.4.0

textexpander/README.md (1695) → textexpander/README.md (2214)

diff --git a/textexpander/README.md b/textexpander/README.md
index f9c3141..7026817 100644
--- a/textexpander/README.md
+++ b/textexpander/README.md
@@ -20,7 +20,7 @@ scripts = [
     {
         "name": "get_mastodon_text.py",
         "description": """
-        print a Markdown-formatted blockquote of a Mastodon I've got open in Safari, suitable for pasting into Obsidian
+        print a Markdown-formatted blockquote of a Mastodon I've got open in Safari, suitable for saving in Obsidian
         """,
     },
     {
@@ -30,9 +30,19 @@ scripts = [
         This makes a couple of tweaks to tidy up the URL, e.g. remove tracking parameters and tidy up some Jekyll stuff for my personal site.
         """,
     },
+    {
+        "name": "get_tweet_text.py",
+        "description": """
+        print a Markdown-formatted blockquote of a tweet I've got open in Safari, suitable for saving in Obsidian
+        """,
+    },
 ]
 
-cog_helpers.create_description_table(folder_name=folder_name, scripts=scripts)
+cog_helpers.create_description_table(
+    folder_name=folder_name,
+    scripts=scripts,
+    ignore_files={"urls.py"}
+)
 
 ]]]-->
 <dl>
@@ -42,7 +52,7 @@ cog_helpers.create_description_table(folder_name=folder_name, scripts=scripts)
     </a>
   </dt>
   <dd>
-    print a Markdown-formatted blockquote of a Mastodon I've got open in Safari, suitable for pasting into Obsidian
+    print a Markdown-formatted blockquote of a Mastodon I've got open in Safari, suitable for saving in Obsidian
   </dd>
 
   <dt>
@@ -54,5 +64,14 @@ cog_helpers.create_description_table(folder_name=folder_name, scripts=scripts)
     print the URL in my frontmost Safari window.
     This makes a couple of tweaks to tidy up the URL, e.g. remove tracking parameters and tidy up some Jekyll stuff for my personal site.
   </dd>
+
+  <dt>
+    <a href="https://github.com/alexwlchan/scripts/blob/main/textexpander/get_tweet_text.py">
+      <code>get_tweet_text.py</code>
+    </a>
+  </dt>
+  <dd>
+    print a Markdown-formatted blockquote of a tweet I've got open in Safari, suitable for saving in Obsidian
+  </dd>
 </dl>
-<!-- [[[end]]] (checksum: 92cb9f2e13db3a692bf3b95b32c5c773) -->
+<!-- [[[end]]] (checksum: ef5c5561422a830e36c0bb6498b552a9) -->

textexpander/get_tweet_text.py (0) → textexpander/get_tweet_text.py (1434)

diff --git a/textexpander/get_tweet_text.py b/textexpander/get_tweet_text.py
new file mode 100755
index 0000000..c14f764
--- /dev/null
+++ b/textexpander/get_tweet_text.py
@@ -0,0 +1,51 @@
+#!/usr/bin/env python3
+"""
+Look at the tweet in the frontmost Safari window, and print it
+as a blockquote suitable for pasting into Obsidian.
+"""
+
+import datetime
+import regex
+import subprocess
+import sys
+import textwrap
+
+import bs4
+import hyperlink
+
+from urls import get_safari_url
+
+
+if __name__ == "__main__":
+    url = get_safari_url()
+
+    if not hyperlink.DecodedURL.from_text(url).host == "twitter.com":
+        sys.exit(f"Not a Twitter URL: {url}")
+
+    handle = hyperlink.DecodedURL.from_text(url).path[0]
+
+    cmd = """
+    tell application "Safari"
+        tell document 1
+            get (do JavaScript "document.querySelector('article').innerHTML")
+        end tell
+    end tell
+    """
+
+    html = subprocess.check_output(["osascript", "-e", cmd]).decode("utf8")
+
+    # Twemoji will be something like <img alt="🌮" …, so go ahead and
+    # replace them with native emoji ASAP.
+    html = regex.sub(r'<img alt="(\p{Extended_Pictographic})"[^>]+>', r"\g<1>", html)
+
+    soup = bs4.BeautifulSoup(html, "html.parser")
+
+    # username = soup.find("div", attrs={"data-testid": "User-Name"}).text.replace(f'@{handle}', '').strip()
+
+    text = soup.find("div", attrs={"data-testid": "tweetText"}).text
+
+    time = datetime.datetime.fromisoformat(soup.find("time").attrs["datetime"])
+
+    print(f'{url} ({time.strftime("%-d %b %Y")}):')
+    print("")
+    print(textwrap.indent(text, prefix="> ", predicate=lambda line: True))

textexpander/urls.py (0) → textexpander/urls.py (277)

diff --git a/textexpander/urls.py b/textexpander/urls.py
new file mode 100644
index 0000000..e9f1fc5
--- /dev/null
+++ b/textexpander/urls.py
@@ -0,0 +1,12 @@
+import subprocess
+
+
+def get_safari_url(window: int = 1) -> str:
+    """
+    Get the URL of the given Safari window.
+    """
+    cmd = ["/Users/alexwlchan/.cargo/bin/safari", "url", "--window", str(window)]
+
+    url = subprocess.check_output(cmd).decode("utf8")
+
+    return url