Skip to content

Commit

Permalink
Merge pull request #3 from alexwlchan/add-obsidian-mastodon-script
Browse files Browse the repository at this point in the history
Add obsidian mastodon script
  • Loading branch information
alexwlchan authored Dec 10, 2023
2 parents 7193319 + 06e36c8 commit bb70efc
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 1 deletion.
2 changes: 2 additions & 0 deletions requirements.in
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
black
flake8
httpx
humanize
hyperlink
keyring
Pillow
pillow_heif
pip-tools
pytest
termcolor
yt-dlp
25 changes: 24 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
# 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
black==23.11.0
# via -r requirements.in
brotli==1.1.0
Expand All @@ -12,6 +14,8 @@ build==1.0.3
# via pip-tools
certifi==2023.11.17
# via
# httpcore
# httpx
# requests
# yt-dlp
charset-normalizer==3.3.2
Expand All @@ -22,14 +26,24 @@ 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
iniconfig==2.0.0
# via pytest
jaraco-classes==3.3.0
# via keyring
keyring==24.3.0
Expand All @@ -46,6 +60,7 @@ packaging==23.2
# via
# black
# build
# pytest
pathspec==0.11.2
# via black
pillow==10.1.0
Expand All @@ -58,6 +73,8 @@ pip-tools==7.3.0
# via -r requirements.in
platformdirs==4.0.0
# via black
pluggy==1.3.0
# via pytest
pycodestyle==2.11.1
# via flake8
pycryptodomex==3.19.0
Expand All @@ -66,8 +83,14 @@ pyflakes==3.1.0
# via flake8
pyproject-hooks==1.0.0
# via build
pytest==7.4.3
# via -r requirements.in
requests==2.31.0
# via yt-dlp
sniffio==1.3.0
# via
# anyio
# httpx
termcolor==2.3.0
# via -r requirements.in
urllib3==2.1.0
Expand Down
16 changes: 16 additions & 0 deletions textexpander/README.md
Original file line number Diff line number Diff line change
@@ -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>
85 changes: 85 additions & 0 deletions textexpander/get_mastodon_text.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#!/usr/bin/env python3
"""
Look at the Mastodon URL in the frontmost Safari window, and print it
as a blockquote.
"""

import datetime
import os
import pathlib
import re
import subprocess

import httpx
import hyperlink


ATTACHMENTS_DIR = pathlib.Path.home() / "textfiles" / "Attachments" / "mastodon"


def download(url):
"""
Download a file to the attachments directory, or do nothing if it's
already downloaded.
"""
resp = httpx.get(url)
content = resp.content

ATTACHMENTS_DIR.mkdir(exist_ok=True)

out_path = ATTACHMENTS_DIR / os.path.basename(url)

try:
with open(out_path, "xb") as out_file:
out_file.write(content)
except FileExistsError:
if open(out_path, "rb").read() == content:
pass
else:
raise


def normalise_text(text: str) -> str:
text = text.replace("<p>", "").replace("</p>", "")
text = re.sub(
r'<a href="[^"]+" class="mention hashtag" rel="tag">#<span>(?P<hashtag>[^<]+)</span></a>',
r"\\#\g<hashtag>",
text,
)
return text


if __name__ == "__main__":
url = subprocess.check_output(["/usr/local/bin/safari", "url"]).decode("utf8")

u = hyperlink.URL.from_text(url)

# e.g. https://hachyderm.io/@djnavarro/111535929722933178
# ~> https://hachyderm.io/api/v1/statuses/111535929722933178
api_url = f"https://{u.host}/api/v1/statuses/{u.path[1]}"

resp = httpx.get(api_url)

post_data = resp.json()

for attachment in post_data["media_attachments"]:
download(attachment["url"])

author = post_data["account"]["display_name"]
post_url = post_data["url"]

# e.g. 2023-12-06T22:53:44.536Z
created_at = datetime.datetime.strptime(
post_data["created_at"], "%Y-%m-%dT%H:%M:%S.%fz"
)

print(f'[{author}]({post_url}) ({created_at.strftime("%-d %B %Y")}):')
print("")
print("> " + normalise_text(post_data["content"]))

if post_data["media_attachments"]:
print(">\n> ", end="")
for attachment in post_data["media_attachments"]:
print("![[%s|200]]" % os.path.basename(attachment["url"]), end="")

print("")
19 changes: 19 additions & 0 deletions textexpander/test_get_mastodon_text.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import pytest

from get_mastodon_text import normalise_text


@pytest.mark.parametrize(
["input", "output"],
[
(
"<p>A variation on the previous system for todays <a "
'href="https://hachyderm.io/tags/ArtAdventCalendar" class="mention '
'hashtag" rel="tag">#<span>ArtAdventCalendar</span></a> '
"contribution</p>",
"A variation on the previous system for todays \#ArtAdventCalendar contribution",
)
],
)
def test_normalise_text(input, output):
assert normalise_text(input) == output

0 comments on commit bb70efc

Please sign in to comment.