-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebhookfromghost.py
85 lines (75 loc) · 3.04 KB
/
webhookfromghost.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
from flask import Flask, request, abort
from mastodon import Mastodon
import os
access_token = os.environ.get('MASTODON_ACCESS_TOKEN')
base_url = os.environ.get('MASTODON_BASE_URL')
trusted_proxies = "127.0.0.1", "10.9.9.1", "10.9.9.99", os.environ.get('TRUSTED_PROXIES')
app = Flask(__name__)
@app.route('/webhook', methods=['POST'])
def get_webhook():
print("Posting to Mastodon")
if request.method == 'POST':
try:
# extract post title, URL, excerpt and tags
ghostPost = request.json["post"]["current"]
ghostTitle = str(ghostPost["title"])
ghostURL = str(ghostPost["url"])
ghostExcerpt = str(ghostPost.get("custom_excerpt", ""))
ghostTags = ghostPost.get("tags", [])
ghostToot = f"{ghostTitle}\n{ghostExcerpt}\n{ghostURL}"
hashtags = tags_to_mastodon_has(ghostTags)
print("Adding Hashtags: ", hashtags)
ghostToot = f"{ghostToot}\n{hashtags}"
print("Creating toot: ", ghostToot)
mastodon = Mastodon(access_token = access_token, api_base_url = base_url, debug_requests=True)
mastodon.toot(ghostToot)
return 'Send to Mastodon completed', 200
except:
raise
else:
abort(400)
def tags_to_mastodon_has(ghostTags):
# convert ghost cms tags to mastodon hashtags
return " ".join(f"#{tag['name']}" for tag in ghostTags)
tagsList = ''
for tags in ghostTags:
print(tags["name"])
tagsList += " #"+tags["name"]
return tagsList.lstrip()
@app.before_request
def limit_remote_addr():
# check if remote ip is in trusted proxies
print("Checking IP")
if request.environ.get('HTTP_X_FORWARDED_FOR') is None:
remote = request.environ['REMOTE_ADDR']
else:
remote = request.environ['HTTP_X_FORWARDED_FOR']
print("Got IP ", remote)
if remote not in str(trusted_proxies):
print("Your IP is not in trusted proxies list!")
# forbidden
abort(403)
@app.before_request
def check_access():
print("Checking token and URL...")
# check if token and url are set
if access_token is None:
print("Missing Mastodon access token")
raise RuntimeError('Missing Mastodon access token')
elif base_url is None:
print("Missing Mastodon base URL")
raise RuntimeError('Missing Mastodon base URL')
else:
print("Got Mastodon access token and base URL")
@app.before_request
def check_if_valid_ghost_post():
# check for valid ghost post
print("Checking valid Header")
if "Ghost" not in str(request.headers.get('User-Agent')):
return 'Bad Request', abort(400)
elif "application/json" != str(request.headers.get('Content-Type')):
return 'Bad Request', abort(400)
else:
print("Seems to be valid Ghost CMS post")
if __name__ == '__main__':
app.run(host="0.0.0.0", debug=False)