diff --git a/.secret b/.secret new file mode 100644 index 0000000..402ee80 --- /dev/null +++ b/.secret @@ -0,0 +1,2 @@ + +https:// \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..32b6ab4 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,10 @@ +# syntax=docker/dockerfile:1 +FROM python:3.8-slim-bullseye +RUN apt update && apt install python3-dev gcc -y +COPY requirements.txt requirements.txt +RUN pip3 install -r requirements.txt +COPY webhookfromghost.ini webhookfromghost.ini +COPY webhookfromghost.py webhookfromghost.py +COPY wsgi.py wsgi.py +EXPOSE 5000/tcp +CMD [ "uwsgi", "--ini", "webhookfromghost.ini" ] diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..b89adb7 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,12 @@ +version: '3.1' + +services: + + ghostcms2mastodon: + image: okxo/ghostcms2mastodon:latest + restart: always + volumes: + - .secret:/.secret + restart: always + ports: + - 5000:5000 \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..c5c0925 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,21 @@ +blurhash==1.1.4 +certifi==2022.9.24 +charset-normalizer==2.1.1 +click==8.1.3 +decorator==5.1.1 +Flask==2.2.2 +idna==3.4 +importlib-metadata==5.0.0 +itsdangerous==2.1.2 +Jinja2==3.1.2 +MarkupSafe==2.1.1 +Mastodon.py==1.6.3 +python-dateutil==2.8.2 +python-magic==0.4.27 +pytz==2022.6 +requests==2.28.1 +six==1.16.0 +urllib3==1.26.12 +uWSGI==2.0.21 +Werkzeug==2.2.2 +zipp==3.10.0 diff --git a/webhookfromghost.ini b/webhookfromghost.ini new file mode 100644 index 0000000..243ae69 --- /dev/null +++ b/webhookfromghost.ini @@ -0,0 +1,9 @@ +[uwsgi] +module = wsgi:app +master = true +processes = 1 +http-socket = :5000 +socket = webhookfromghost.sock +chmod-socket = 600 +vacuum = true +die-on-term = true diff --git a/webhookfromghost.py b/webhookfromghost.py new file mode 100644 index 0000000..ac69acc --- /dev/null +++ b/webhookfromghost.py @@ -0,0 +1,39 @@ +from flask import Flask, request, abort +from mastodon import Mastodon + +app = Flask(__name__) +@app.route('/webhook', methods=['POST']) +def get_webhook(): + #print(request) + if request.method == 'POST': + try: + # extract post title, URL, excerpt and tags + ghostTitle = str(request.json["post"]["current"]["title"]) + ghostURL = str(request.json["post"]["current"]["url"]) + ghostExcerpt = str(request.json["post"]["current"]["custom_excerpt"]) + ghostTags = request.json["post"]["current"]["tags"] + ghostToot = ghostTitle + "\n" + ghostExcerpt + "\n" + ghostURL + + hashtags = tags_to_mastodon_has(ghostTags) + print("Adding Hashtags: ", hashtags) + ghostToot = ghostTitle + "\n" + ghostExcerpt + "\n" + ghostURL + "\n" + hashtags + print("Creating toot: ", ghostToot) + mastodon = Mastodon(access_token = '.secret', debug_requests=True) + mastodon.toot(ghostToot) + return 'success', 200 + except: + raise + else: + abort(400) + +def tags_to_mastodon_has(ghostTags): + # convert ghost cms tags to mastodon hashtags + tagsList = '' + #print(ghostTags) + for tags in ghostTags: + print(tags["name"]) + tagsList += " #"+tags["name"] + return tagsList.lstrip() + +if __name__ == '__main__': + app.run(host="0.0.0.0") diff --git a/wsgi.py b/wsgi.py new file mode 100644 index 0000000..732e56d --- /dev/null +++ b/wsgi.py @@ -0,0 +1,4 @@ +from webhookfromghost import app + +if __name__ == "__main__": + app.run()