fedipost.py
· 1.3 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
if len(sys.argv) > 3:
if sys.argv[3] not in item["content_html"]:
continue
content = markdownify(item["content_html"]).strip()
data = {
"username": "Fediverse",
"avatar_url": feed["icon"],
"embeds": [
{
"title": feed["title"],
"url": feed["home_page_url"],
"color": 16711829,
"fields": [
{
"name": "\u200B",
"value": content
}
],
"url": item["url"],
"timestamp": item["date_modified"][:-5] + "Z"
}
]
}
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 | 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)) |