Files
hooky/main.py
2025-09-26 10:46:47 -04:00

84 lines
2.3 KiB
Python

from flask import Flask, request
import httpx
import logging
app = Flask(__name__)
# Configure logging
logging.basicConfig(level=logging.INFO)
def populate_webhook_lookup():
lookup = {}
with open("lookup.txt", "r") as file:
for line in file:
split_line = line.rstrip().split(",")
lookup[split_line[0]]= split_line[1]
return lookup
UUID_TO_WEBHOOK_LOOKUP = populate_webhook_lookup()
def convert_ghost_to_discord(ghost_payload):
post = ghost_payload.get("post").get("current")
return {
"embeds": [{
"title": post.get("title"),
"url": f"https://torrtle.co/{post.get('slug')}",
"color": "0x00ff00",
"thumbnail": {
"url": post.get("feature_image")
},
"fields": [
{
"name": "Author",
"value": post.get("primary_author", {}).get("name","simbob"),
"inline": True
},
{
"name": "Published",
"value": post.get("published_at"),
"inline": True,
}
],
}]
}
@app.route("/")
def home():
return "This is a silly web util that converts the Ghost webhook payloads to Discord payloads"
@app.route("/hook/<uuid>", methods=["POST"])
def convert(uuid):
lookup = populate_webhook_lookup()
webhook = lookup.get(uuid)
ghost_payload = request.get_json()
discord_payload = convert_ghost_to_discord(ghost_payload)
try:
r = httpx.post(webhook, json=discord_payload)
if r.status_code == 200:
logging.info(f"Successfully posted to Discord webhook for UUID {uuid}")
else:
logging.warning(f"Discord webhook POST failed for UUID {uuid} with status {r.status_code}: {r.text}")
print(r)
except Exception as e:
logging.error(f"Failed to post to Discord webhook for UUID {uuid}: {str(e)}")
return "error", 500
return "yay"
def main():
print("Hello from webhookproxy!")
with open("lookup.txt", "r") as file:
for line in file:
print(line.split(','))
if __name__ == "__main__":
main()