-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from alexwlchan/add-obsidian-mastodon-script
Add obsidian mastodon script
- Loading branch information
Showing
5 changed files
with
146 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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("") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |