-
Notifications
You must be signed in to change notification settings - Fork 0
/
macros.py
executable file
·283 lines (263 loc) · 8.05 KB
/
macros.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
import subprocess
import tempfile
import urllib.request
from pathlib import Path
from telegram import Update
from telegram.ext import ContextTypes
from utils import no_can_do, printlog
# Inspirational
async def ispirami(update: Update, context: ContextTypes.DEFAULT_TYPE):
if await no_can_do(update, context):
return
def make_photo(text: str, nickname: str, destination_dir: Path) -> Path:
template_photo = "templates/motivational_poster.jpg"
font_file = "fonts/Vollkorn-Italic.ttf"
darken_file = "templates/darken.png"
destination_photo = Path("images/motivational_poster.png")
size = 42
if len(text) > 400:
size = 26
elif len(text) > 300:
size = 32
elif len(text) > 200:
size = 36
text = text.replace("&", "\&")
nickname = nickname.replace("&", "\&")
command = [
"convert",
"(",
str(template_photo),
str(darken_file),
"-flatten",
")",
"(",
"-gravity",
"center",
# "-size", "760x560",
"-font",
str(font_file),
"-pointsize",
str(size),
"-background",
"none",
# "-stroke", "#000",
"-fill",
"#fff",
"-size",
"760",
f'pango:<span font_family="Vollkorn" style="italic" size="{size * 600}">“{text}”</span>',
"+repage",
")",
"(",
"-clone",
"1",
"-background",
"black",
"-shadow",
"100x2+2+2",
")",
"(",
"-clone",
"1",
"-clone",
"2",
"+swap",
"-background",
"none",
"-layers",
"merge",
"+repage",
")",
"-delete",
"1,2",
"-gravity",
"center",
"-geometry",
"+0-50",
"-compose",
"over",
"-composite", # fine
"(",
"-gravity",
"SouthEast",
"-size",
"760",
"-font",
str(font_file),
"-pointsize",
str(size),
"-background",
"none",
"-fill",
"#fff",
f'pango:<span font_family="Vollkorn" style="italic" size="{size * 600}"> - {nickname}</span>',
"+repage",
")",
"(",
"-clone",
"1",
"-background",
"black",
"-shadow",
"100x2+2+2",
")",
"(",
"-clone",
"1",
"-clone",
"2",
"+swap",
"-background",
"none",
"-layers",
"merge",
"+repage",
")",
"-delete",
"1,2",
"-gravity",
"SouthEast",
"-geometry",
"+20+0",
"-compose",
"over",
# "-geometry", "+455+777",
# "-compose", "Multiply",
"-composite", # fine
str(destination_photo),
]
subprocess.check_call(command) # nosec
return destination_photo
if not update.message.reply_to_message:
return
else:
if not update.message.reply_to_message.text:
await update.message.reply_text("Non c'è nessun testo.")
return
text = update.message.reply_to_message.text
if len(text) > 700:
await update.message.reply_text("It's too long man!")
return
chars = (
"q",
"w",
"e",
"r",
"t",
"y",
"u",
"i",
"o",
"p",
"a",
"s",
"d",
"f",
"g",
"h",
"j",
"k",
"l",
"z",
"x",
"c",
"v",
"b",
"n",
"m",
"è",
"é",
"ò",
"à",
"ù",
"ì",
)
text = text[0].upper() + text[1:]
if text.lower().endswith(chars):
text += "."
url = "https://picsum.photos/800/600"
urllib.request.urlretrieve(url, "templates/motivational_poster.jpg") # nosec
first_name = ""
last_name = ""
username = ""
if update.message.reply_to_message.forward_origin: # è un forward
try:
first_name = update.message.reply_to_message.forward_origin.sender_user.first_name
last_name = update.message.reply_to_message.forward_origin.sender_user.last_name
username = update.message.reply_to_message.forward_origin.sender_user.username
if first_name and last_name:
nickname = f"{first_name} {last_name}"
elif first_name and not last_name:
nickname = first_name
else:
nickname = f"@{username}"
except AttributeError: # Privacy Forward
nickname = update.message.reply_to_message.forward_origin.sender_user_name
if nickname:
nickname += f", {update.message.reply_to_message.forward_origin.date.strftime('%-d %B %Y')}"
else:
nickname = f"Anonimo, {update.message.reply_to_message.forward_origin.date.strftime('%-d %B %Y')}"
else:
first_name = update.message.reply_to_message.from_user.first_name
last_name = update.message.reply_to_message.from_user.last_name
username = update.message.reply_to_message.from_user.username
if first_name and last_name:
nickname = f"{first_name} {last_name}"
elif first_name and not last_name:
nickname = first_name
else:
nickname = f"@{username}"
nickname += f", {update.message.reply_to_message.date.strftime('%-d %B %Y')}"
# print(f'{get_now()} {await get_display_name(update.effective_user)} in {await get_chat_name(update.message.chat.id)} fa un poster motivazionale')
await printlog(update, "fa un poster motivazionale")
if context.args:
if context.args[0] in ["-a", "-anonimo", "-anon"]:
pass
with tempfile.TemporaryDirectory() as tmp_dir:
photo = make_photo(text, nickname, Path(tmp_dir))
await context.bot.send_photo(chat_id=update.effective_chat.id, photo=photo.open("rb"))
async def change_my_mind(update: Update, context: ContextTypes.DEFAULT_TYPE):
if await no_can_do(update, context):
return
def make_photo(text: str, destination_dir: Path) -> Path:
template_photo = "templates/changemymind.jpg"
font_file = "fonts/calibri.ttf"
destination_photo = destination_dir / "changemymind.jpg"
command = [
"convert",
str(template_photo),
"(",
"-size",
"650x290!",
"-font",
str(font_file),
"-background",
"none",
"-fill",
"#222222",
f"caption:{text}",
"-rotate",
"337",
# "+distort", "perspective", "0,0 10,0 0,290 115,254 666,290 691,-94 666,0 625,-155",
")",
"-geometry",
"+910+700",
"-compose",
"Multiply",
"-composite",
str(destination_photo),
]
subprocess.check_call(command) # nosec
return destination_photo
if not context.args:
return
# print(f'{get_now()} {await get_display_name(update.effective_user)} in {await get_chat_name(update.message.chat.id)} fa un poster motivazionale')
await printlog(update, "fa un change my mind")
# text = ' '.join(context.args)
text = update.message.text[14:]
if len(text) > 200:
await update.message.reply_text("It's too long man!")
return
with tempfile.TemporaryDirectory() as tmp_dir:
photo = make_photo(text, Path(tmp_dir))
await update.message.reply_photo(photo=photo.open("rb"))