Last active 1698081812

a script to forward fediverse posts from a jsonfeed url to a discord webhook, optionally filtering by keyword/hashtag

Alyssa Smith revised this gist 1698117812. Go to revision

1 file changed, 9 insertions, 8 deletions

fedipost.py

@@ -18,11 +18,14 @@ items = []
18 18 for item in feed["items"]:
19 19 if item["url"] in posts:
20 20 continue
21 + content = item["content_html"].split('<span class="new_note')[0].split(" <span class=\"reply_note")[0]
21 22 if len(sys.argv) > 3:
22 - if sys.argv[3] not in item["content_html"]:
23 + if sys.argv[3] not in content:
23 24 continue
24 - content, img_url = item["content_html"].split('<span class="new_note')[0].split("<img",1)
25 - img_url = img_url.split(' src="',1)[1].split('"')[0]
25 + img = {"image": {"url": ""}}
26 + if "<img" in content:
27 + content, img_url = content.split("<img",1)
28 + img["image"]["url"] = img_url.split(' src="',1)[1].split('"')[0]
26 29 content = markdownify(content).strip()
27 30 data = {
28 31 "username": "Fediverse",
@@ -32,17 +35,15 @@ for item in feed["items"]:
32 35 "title": feed["title"],
33 36 "url": feed["home_page_url"],
34 37 "color": 16711829,
35 - "image": {
36 - "url": img_url
37 - },
38 38 "fields": [
39 39 {
40 40 "name": "New post",
41 - "value": content
41 + "value": content[:1020] + ("..." if len(content) > 1020 else "")
42 42 }
43 43 ],
44 44 "url": item["url"],
45 - "timestamp": item["date_modified"][:-5] + "Z"
45 + "timestamp": item["date_modified"][:-5] + "Z",
46 + **img
46 47 }
47 48 ]
48 49 }

Alyssa Smith revised this gist 1697911319. Go to revision

1 file changed, 7 insertions, 2 deletions

fedipost.py

@@ -21,7 +21,9 @@ for item in feed["items"]:
21 21 if len(sys.argv) > 3:
22 22 if sys.argv[3] not in item["content_html"]:
23 23 continue
24 - content = markdownify(item["content_html"]).strip()
24 + content, img_url = item["content_html"].split('<span class="new_note')[0].split("<img",1)
25 + img_url = img_url.split(' src="',1)[1].split('"')[0]
26 + content = markdownify(content).strip()
25 27 data = {
26 28 "username": "Fediverse",
27 29 "avatar_url": feed["icon"],
@@ -30,9 +32,12 @@ for item in feed["items"]:
30 32 "title": feed["title"],
31 33 "url": feed["home_page_url"],
32 34 "color": 16711829,
35 + "image": {
36 + "url": img_url
37 + },
33 38 "fields": [
34 39 {
35 - "name": "\u200B",
40 + "name": "New post",
36 41 "value": content
37 42 }
38 43 ],

Alyssa Smith revised this gist 1697904652. Go to revision

1 file changed, 47 insertions

fedipost.py(file created)

@@ -0,0 +1,47 @@
1 + #!/usr/bin/env python3
2 + # usage: fedipost.py jsonfeed_url webhook_url [filter_text]
3 + # e.g: fedipost.py https://blahaj.zone/@alypet.json webhook #alyblog
4 + from markdownify import markdownify
5 + from requests import get,post
6 + import os, sys
7 +
8 + WH = sys.argv[2]
9 +
10 + try:
11 + with open("posts.txt") as f:
12 + posts = f.read().strip().split("\n")
13 + except:
14 + posts = []
15 +
16 + feed = get(sys.argv[1]).json()
17 + items = []
18 + for item in feed["items"]:
19 + if item["url"] in posts:
20 + continue
21 + if len(sys.argv) > 3:
22 + if sys.argv[3] not in item["content_html"]:
23 + continue
24 + content = markdownify(item["content_html"]).strip()
25 + data = {
26 + "username": "Fediverse",
27 + "avatar_url": feed["icon"],
28 + "embeds": [
29 + {
30 + "title": feed["title"],
31 + "url": feed["home_page_url"],
32 + "color": 16711829,
33 + "fields": [
34 + {
35 + "name": "\u200B",
36 + "value": content
37 + }
38 + ],
39 + "url": item["url"],
40 + "timestamp": item["date_modified"][:-5] + "Z"
41 + }
42 + ]
43 + }
44 + post(WH, json=data)
45 + posts.append(item["url"])
46 + with open("posts.txt", "w") as f:
47 + f.write("\n".join(posts))
Newer Older