Skip to main content

Add a script for getting quotes from Wikipedia

ID
ec4c856
date
2024-01-09 19:26:37+00:00
author
Alex Chan <alex@alexwlchan.net>
parent
5c1fecd
message
Add a script for getting quotes from Wikipedia
changed files
2 files, 46 additions, 1 deletion

Changed files

textexpander/README.md (2771) → textexpander/README.md (3269)

diff --git a/textexpander/README.md b/textexpander/README.md
index 8131db2..faec927 100644
--- a/textexpander/README.md
+++ b/textexpander/README.md
@@ -42,6 +42,12 @@ scripts = [
         print a Markdown-formatted blockquote of a tweet I've got open in Safari, suitable for saving in Obsidian
         """,
     },
+    {
+        "name": "get_wikipedia_quote.py",
+        "description": """
+        get URL and first paragraph of a Wikipedia entry I have open in Safari, suitable for saving in Obsidian.
+        """,
+    },
 ]
 
 cog_helpers.create_description_table(
@@ -88,5 +94,14 @@ cog_helpers.create_description_table(
   <dd>
     print a Markdown-formatted blockquote of a tweet I've got open in Safari, suitable for saving in Obsidian
   </dd>
+
+  <dt>
+    <a href="https://github.com/alexwlchan/scripts/blob/main/textexpander/get_wikipedia_quote.py">
+      <code>get_wikipedia_quote.py</code>
+    </a>
+  </dt>
+  <dd>
+    get URL and first paragraph of a Wikipedia entry I have open in Safari, suitable for saving in Obsidian.
+  </dd>
 </dl>
-<!-- [[[end]]] (checksum: 6e089377a506a73c7859d6e34777570a) -->
+<!-- [[[end]]] (checksum: 24db92a20bd6747bf2e7768f276dc414) -->

textexpander/get_wikipedia_quote.py (0) → textexpander/get_wikipedia_quote.py (640)

diff --git a/textexpander/get_wikipedia_quote.py b/textexpander/get_wikipedia_quote.py
new file mode 100755
index 0000000..af05a73
--- /dev/null
+++ b/textexpander/get_wikipedia_quote.py
@@ -0,0 +1,30 @@
+#!/usr/bin/python3
+"""
+Look at the Wikipedia URL in my frontmost browser window, and print
+a link with a quote of the first paragraph.
+"""
+
+import re
+
+import bs4
+import httpx
+
+from urls import get_safari_url
+
+
+if __name__ == "__main__":
+    url = get_safari_url()
+    assert "wikipedia.org" in url
+
+    resp = httpx.get(url)
+
+    soup = bs4.BeautifulSoup(resp.text, "html.parser")
+
+    title = soup.find("span", attrs={"class": "mw-page-title-main"}).text
+    text = re.sub(
+        r"\[\d+\]", "", soup.find("div", attrs={"id": "mw-content-text"}).find("p").text
+    )
+
+    print(f"[{title}]({url}):")
+    print("")
+    print(f"> {text}")