fedipost.py
· 1.6 KiB · Python
Raw
#!/usr/bin/env python3
# usage: fedipost.py jsonfeed_url webhook_url [filter_text]
# e.g: fedipost.py https://blahaj.zone/@alypet.json webhook #alyblog
from markdownify import markdownify
from requests import get,post
import os, sys
WH = sys.argv[2]
try:
with open("posts.txt") as f:
posts = f.read().strip().split("\n")
except:
posts = []
feed = get(sys.argv[1]).json()
items = []
for item in feed["items"]:
if item["url"] in posts:
continue
content = item["content_html"].split('<span class="new_note')[0].split(" <span class=\"reply_note")[0]
if len(sys.argv) > 3:
if sys.argv[3] not in content:
continue
img = {"image": {"url": ""}}
if "<img" in content:
content, img_url = content.split("<img",1)
img["image"]["url"] = img_url.split(' src="',1)[1].split('"')[0]
content = markdownify(content).strip()
data = {
"username": "Fediverse",
"avatar_url": feed["icon"],
"embeds": [
{
"title": feed["title"],
"url": feed["home_page_url"],
"color": 16711829,
"fields": [
{
"name": "New post",
"value": content[:1020] + ("..." if len(content) > 1020 else "")
}
],
"url": item["url"],
"timestamp": item["date_modified"][:-5] + "Z",
**img
}
]
}
post(WH, json=data)
posts.append(item["url"])
with open("posts.txt", "w") as f:
f.write("\n".join(posts))
| 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 | content = item["content_html"].split('<span class="new_note')[0].split(" <span class=\"reply_note")[0] |
| 22 | if len(sys.argv) > 3: |
| 23 | if sys.argv[3] not in content: |
| 24 | continue |
| 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] |
| 29 | content = markdownify(content).strip() |
| 30 | data = { |
| 31 | "username": "Fediverse", |
| 32 | "avatar_url": feed["icon"], |
| 33 | "embeds": [ |
| 34 | { |
| 35 | "title": feed["title"], |
| 36 | "url": feed["home_page_url"], |
| 37 | "color": 16711829, |
| 38 | "fields": [ |
| 39 | { |
| 40 | "name": "New post", |
| 41 | "value": content[:1020] + ("..." if len(content) > 1020 else "") |
| 42 | } |
| 43 | ], |
| 44 | "url": item["url"], |
| 45 | "timestamp": item["date_modified"][:-5] + "Z", |
| 46 | **img |
| 47 | } |
| 48 | ] |
| 49 | } |
| 50 | post(WH, json=data) |
| 51 | posts.append(item["url"]) |
| 52 | with open("posts.txt", "w") as f: |
| 53 | f.write("\n".join(posts)) |