-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcrabrave.py
246 lines (201 loc) · 8.22 KB
/
crabrave.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import logging
import threading
import time
import json
import hashlib
import ffmpeg
from uuid import uuid4
from urllib import parse
from flask import Flask, request, send_file, abort
from waitress import serve
from colorlist import FFMPEG_COLORS
from filter import *
from telegram import InlineQueryResultVideo, InlineQueryResultArticle, ParseMode, InputTextMessageContent
from telegram.ext import Updater, InlineQueryHandler, CommandHandler
ENABLED_FILTERS = {
"classic": classic,
"simple": simple,
"snapchat": snapchat,
"textbox": textbox,
"topbottom": topbottom
}
# enable logging
logging.basicConfig(format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO)
logger = logging.getLogger(__name__)
BOT_TOKEN = os.environ.get("BOT_TOKEN", None)
PORT = int(os.environ.get("PORT", "3000"))
BASE_URL = os.environ.get("BASE_URL", "http://localhost:3000")
app = Flask(__name__, static_url_path="")
updater = Updater(BOT_TOKEN, use_context=True)
if not os.path.isdir("output"):
os.path.mkdir("output")
@app.route("/")
def index():
return app.send_static_file("index.html")
@app.route("/thumb/<name>.jpg", methods=['GET'])
def thumbnail(name):
return send_file(os.path.join("thumb", f"{name}.jpg"))
@app.route("/video/<overlay_text>.mp4", methods=['GET'])
def crabrave(overlay_text):
style = request.args.get("style", default="papaj")
input_file = os.path.join("input", f"{style}.mp4")
# check if input file exists
if not os.path.exists(input_file):
return abort(400)
font = request.args.get("font", default="comicsans")
font_file = os.path.join("font", f"{font}.ttf")
# check if font file exists
if not os.path.exists(font_file):
return abort(400)
font_size = request.args.get("size", default=48, type=int)
# check if text size is in range
if font_size > 96:
return abort(400)
font_color = request.args.get("color", default="white")
# check if text color is available
if font_color not in FFMPEG_COLORS:
return abort(400)
filter_name = request.args.get("filter", default="classic")
# check if filter is enabled
if filter_name not in ENABLED_FILTERS:
return abort(400)
overlay_text = parse.unquote_plus(overlay_text).strip()
# check if overlay text is 2-120 chars long
if len(overlay_text) < 2 or len(overlay_text) > 120:
return abort(400)
# generate filename based on selected options
filename = f"{generate_filename(style, overlay_text, font, font_color, font_size, filter_name)}.mp4"
# check if video has already been rendered
output_file = os.path.join("output", filename)
if os.path.exists(output_file):
return send_file(output_file)
# render video
selected_filter = ENABLED_FILTERS[filter_name]
input_stream = ffmpeg.input(input_file)
output = ffmpeg.output(
selected_filter.apply_filter(input_stream, overlay_text, font_file, font_color, font_size),
input_stream.audio,
output_file,
**{"codec:a": "copy"}
)
output.run()
return send_file(output_file)
def inlinequery(update, context):
# get entered text message
query = update.inline_query.query.strip()
if len(query) < 2 or len(query) > 40:
update.inline_query.answer([
InlineQueryResultArticle(
id=uuid4(),
title="Not enough arguments",
description="Text must be between 2 and 40 characters long.",
thumb_url="https://cdn.pixabay.com/photo/2013/07/12/18/09/help-153094__340.png",
input_message_content=InputTextMessageContent(
"Usage: @isgonebot <overlay text>\n\nText must be between 2 and 40 characters long.",
parse_mode=ParseMode.MARKDOWN
)
)
])
return
# generate query results
results = [
InlineQueryResultVideo(
id=uuid4(),
title="🦀🦀 PAPAJ IS GONE 🦀🦀",
description=query,
thumb_url=f"{BASE_URL}/thumb/papaj.jpg?t={time.time()}",
video_url=f"{BASE_URL}/video/{parse.quote_plus(query)}.mp4?style=papaj&font=comicsans&color=white&size=52&filter=classic&t={time.time()}",
mime_type="video/mp4"
),
InlineQueryResultVideo(
id=uuid4(),
title="🦀🦀 DESPACITO IS GONE 🦀🦀",
description=query,
thumb_url=f"{BASE_URL}/thumb/classic.jpg?t={time.time()}",
video_url=f"{BASE_URL}/video/{parse.quote_plus(query)}.mp4?style=classic&font=raleway&color=white&size=52&filter=classic&t={time.time()}",
mime_type="video/mp4"
),
InlineQueryResultVideo(
id=uuid4(),
title="🦀🦀 KEBAB IS GONE 🦀🦀",
description=query,
thumb_url=f"{BASE_URL}/thumb/kebab.jpg?t={time.time()}",
video_url=f"{BASE_URL}/video/{parse.quote_plus(query)}.mp4?style=kebab&font=impact&color=white&size=48&filter=snapchat&t={time.time()}",
mime_type="video/mp4"
),
InlineQueryResultVideo(
id=uuid4(),
title="🦀🦀 STONOGA IS GONE 🦀🦀",
description=query,
thumb_url=f"{BASE_URL}/thumb/stonoga.jpg?t={time.time()}",
video_url=f"{BASE_URL}/video/{parse.quote_plus(query)}.mp4?style=stonoga&font=timesnewroman&color=white&size=48&filter=topbottom&t={time.time()}",
mime_type="video/mp4"
),
InlineQueryResultVideo(
id=uuid4(),
title="🦀🦀 FICHTL IS GONE 🦀🦀",
description=query,
thumb_url=f"{BASE_URL}/thumb/woodys.jpg?t={time.time()}",
video_url=f"{BASE_URL}/video/{parse.quote_plus(query)}.mp4?style=woodys&font=potsdam&color=white&size=52&filter=classic&t={time.time()}",
mime_type="video/mp4"
),
InlineQueryResultVideo(
id=uuid4(),
title="🦀🦀 RONNIE FERRARI IS GONE 🦀🦀",
description=query,
thumb_url=f"{BASE_URL}/thumb/onabytakchciala.jpg?t={time.time()}",
video_url=f"{BASE_URL}/video/{parse.quote_plus(query)}.mp4?style=onabytakchciala&font=comicsans&color=white&size=52&filter=snapchat&t={time.time()}",
mime_type="video/mp4"
),
InlineQueryResultVideo(
id=uuid4(),
title="🦀🦀 BAG RAIDERS IS GONE 🦀🦀",
description=query,
thumb_url=f"{BASE_URL}/thumb/shootingstars.jpg?t={time.time()}",
video_url=f"{BASE_URL}/video/{parse.quote_plus(query)}.mp4?style=shootingstars&font=spacemono&color=white&size=52&filter=simple&t={time.time()}",
mime_type="video/mp4"
),
InlineQueryResultArticle(
id=uuid4(),
title="Bot made by @divadsn",
description="Check out my source code on GitHub!",
thumb_url="https://avatars2.githubusercontent.com/u/28547847?s=460&v=4",
input_message_content=InputTextMessageContent(
"https://github.com/divadsn/crabrave-telegram-bot\n\nDonate me via PayPal: https://paypal.me/divadsn",
parse_mode=ParseMode.MARKDOWN
)
)
]
update.inline_query.answer(results)
def generate_filename(style, overlay_text, font, font_color, font_size, filter_name):
# generate md5 hash based on selected parameters
selection = {
overlay_text: {
"style": style,
"font": font,
"size": font_size,
"color": font_color,
"filter": filter_name
}
}
return hashlib.md5(json.dumps(selection).encode("utf-8")).hexdigest()
def start_bot():
# add inlinequery handler
dp = updater.dispatcher
dp.add_handler(InlineQueryHandler(inlinequery))
# start the bot
updater.start_polling()
def main():
print("Starting Telegram bot...")
bot = threading.Thread(target=start_bot)
bot.start()
# start web server and idle
print(f"Listening on {BASE_URL} for requests, press CTRL+C to stop")
serve(app, port=PORT, _quiet=True)
# finish thread by stopping the bot
updater.stop()
if __name__ == '__main__':
main()