-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrip.py
53 lines (42 loc) · 1.42 KB
/
rip.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
import os
import urllib.parse
from io import BytesIO
import textwrap
from flask import Flask, request, send_file, jsonify
from PIL import Image, ImageDraw, ImageFont
app = Flask(__name__)
base_url = 'https://ultra-rip.herokuapp.com/'
font = ImageFont.truetype("Slackey/Slackey-Regular.ttf", 25)
def gravestone_png(msg):
msg = '\n'.join(textwrap.wrap(msg, 20))
rip_img = Image.open('rip.png')
canvas = ImageDraw.Draw(rip_img)
center = rip_img.width//2 - 20
text_width, _ = canvas.multiline_textsize(msg, font)
canvas.multiline_text((center - text_width//2, 350), msg, 'black', font, align='center', )
del canvas
img_io = BytesIO()
rip_img.save(img_io, 'PNG')
img_io.seek(0)
return img_io
@app.route('/rip', methods=['POST'])
def rip_cmd():
img_query = urllib.parse.urlencode({'text': request.form['text']})
return jsonify({
'response_type': 'in_channel',
'text': 'rip...',
'attachments': [
{
'image_url': '{}/gen_img?{}'.format(base_url, img_query),
}
]
})
@app.route('/gen_img', methods=['GET'])
@app.route('/gen_img.png', methods=['GET'])
def gen_img():
return send_file(gravestone_png(request.args.get('text', 'dead...')), mimetype='image/png')
def start_slack():
port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port, debug=True)
if __name__ == '__main__':
start_slack()