Repository navigation
Expand file tree
/
Copy pathRofls.py
More file actions
585 lines (519 loc) · 19.4 KB
/
Copy pathRofls.py
File metadata and controls
585 lines (519 loc) · 19.4 KB
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
# CopyLeft 2026 github.com/i-execute
# Author: I_execute.t.me
# Licensed under AGPLv3.
__version__ = (1, 1, 0)
# meta developer: Execute_forge.t.me
# meta banner: https://raw.githubusercontent.com/i-execute/Modules/main/Storage/Rofls/MetaBanner.jpeg
import io
import os
import sys
import asyncio
import aiohttp
import tempfile
import subprocess
import logging
from PIL import Image, ImageDraw, ImageFont
from telethon.tl.functions.messages import UploadMediaRequest, GetStickerSetRequest
from telethon.tl.functions.stickers import (
CreateStickerSetRequest,
AddStickerToSetRequest,
RemoveStickerFromSetRequest,
)
from telethon.tl.types import (
InputPeerSelf,
InputStickerSetItem,
InputStickerSetShortName,
)
from telethon.errors import StickersetInvalidError
from telethon.utils import get_input_document
from .. import loader, utils
logger = logging.getLogger(__name__)
BASE_IMAGE_URL = "https://raw.githubusercontent.com/i-execute/Modules/main/Storage/Rofls/Down.jpeg"
AVATAR_BOX = (607, 148, 1080, 581)
TG_COLORS = [
(255, 80, 80),
(255, 150, 0),
(230, 185, 0),
(50, 190, 100),
(0, 150, 240),
(120, 80, 230),
(235, 90, 165),
]
FONT_PATHS = [
"/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf",
"/usr/share/fonts/TTF/DejaVuSans-Bold.ttf",
"/usr/share/fonts/dejavu/DejaVuSans-Bold.ttf",
]
def _ensure_all_deps():
for mod, pip_name in {"PIL": "Pillow"}.items():
try:
__import__(mod)
except ImportError:
subprocess.check_call(
[sys.executable, "-m", "pip", "install", pip_name, "-q"],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
try:
__import__("petpetgif")
except ImportError:
subprocess.check_call(
[sys.executable, "-m", "pip", "install", "setuptools<81", "-q"],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
subprocess.check_call(
[
sys.executable, "-m", "pip", "install",
"git+https://github.com/camprevail/pet-pet-gif.git", "-q",
],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
_ensure_all_deps()
from petpetgif import petpet as ppg
def _jpeg_shakalize(img, quality):
tmp = io.BytesIO()
img.save(tmp, format="JPEG", quality=quality)
tmp.seek(0)
return Image.open(tmp).convert("RGB")
def _make_name_avatar(name, uid):
left, top, right, bottom = AVATAR_BOX
w = right - left
h = bottom - top
color = TG_COLORS[uid % 7]
img = Image.new("RGB", (w, h), color)
draw = ImageDraw.Draw(img)
font = None
for path in FONT_PATHS:
try:
font = ImageFont.truetype(path, 10)
break
except Exception:
continue
for font_size in range(120, 8, -2):
f = font.font_variant(size=font_size) if font else ImageFont.load_default()
bbox = draw.textbbox((0, 0), name, font=f)
if (bbox[2] - bbox[0]) <= w - 24 and (bbox[3] - bbox[1]) <= h - 24:
break
bbox = draw.textbbox((0, 0), name, font=f)
tw = bbox[2] - bbox[0]
th = bbox[3] - bbox[1]
x = (w - tw) // 2 - bbox[0]
y = (h - th) // 2 - bbox[1]
draw.text((x, y), name, fill="white", font=f)
buf = io.BytesIO()
img.save(buf, format="JPEG", quality=95)
buf.seek(0)
return buf.read()
def _resolve_avatar(data):
try:
Image.open(io.BytesIO(data)).verify()
return io.BytesIO(data)
except Exception:
fi = tempfile.NamedTemporaryFile(suffix=".mp4", delete=False)
fi.write(data)
fi.close()
fo = tempfile.NamedTemporaryFile(suffix=".png", delete=False)
fo.close()
q = subprocess.run(
[
"ffmpeg", "-y", "-i", fi.name,
"-frames:v", "1", "-vf", "scale=512:512", fo.name,
],
capture_output=True,
)
os.unlink(fi.name)
if q.returncode:
return None
src = io.BytesIO(open(fo.name, "rb").read())
os.unlink(fo.name)
return src
def _apply_circle_mask(src_buf):
img = Image.open(src_buf).convert("RGBA").resize((512, 512))
mask = Image.new("L", (512, 512), 0)
draw = ImageDraw.Draw(mask)
draw.ellipse((0, 0, 512, 512), fill=255)
img.putalpha(mask)
result = io.BytesIO()
img.save(result, format="PNG")
result.seek(0)
return result
def _make_petpet_webm(src_buf, circle=False):
if circle:
src_buf = _apply_circle_mask(src_buf)
g = io.BytesIO()
ppg.make(src_buf, g)
g.seek(0)
f = tempfile.NamedTemporaryFile(suffix=".gif", delete=False)
f.write(g.read())
f.close()
w = f.name[:-4] + ".webm"
q = subprocess.run(
[
"ffmpeg", "-y", "-i", f.name,
"-vf", "scale=512:512:flags=lanczos",
"-r", "30", "-t", "2.99", "-an",
"-c:v", "libvpx-vp9", "-pix_fmt", "yuva420p",
"-b:v", "350K", w,
],
capture_output=True,
)
os.unlink(f.name)
if q.returncode:
return None
return w
def _make_down_mp4(base_bytes, avatar_bytes):
base = Image.open(io.BytesIO(base_bytes)).convert("RGB")
avatar_img = Image.open(io.BytesIO(avatar_bytes)).convert("RGB")
left, top, right, bottom = AVATAR_BOX
box_w = right - left
box_h = bottom - top
avatar_img = avatar_img.resize((box_w, box_h), Image.LANCZOS)
avatar_img = _jpeg_shakalize(avatar_img, quality=8)
base.paste(avatar_img, (left, top))
base = _jpeg_shakalize(base, quality=8)
frame = base.quantize(colors=128, method=Image.Quantize.FASTOCTREE)
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as ff:
frame.save(ff, format="PNG")
frame_path = ff.name
with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as vf:
video_path = vf.name
try:
w, h = frame.size
w_even = w if w % 2 == 0 else w - 1
h_even = h if h % 2 == 0 else h - 1
subprocess.run(
[
"ffmpeg", "-y",
"-loop", "1", "-i", frame_path,
"-c:v", "libx264", "-t", "3",
"-pix_fmt", "yuv420p",
"-vf", f"fps=30,scale={w_even}:{h_even}",
"-an", video_path,
],
check=True, capture_output=True,
)
with open(video_path, "rb") as f:
mp4_data = f.read()
finally:
os.unlink(frame_path)
os.unlink(video_path)
buf = io.BytesIO(mp4_data)
buf.name = "downed.mp4"
buf.seek(0)
return buf
@loader.tds
class RoflsMod(loader.Module):
"""PetPet stickers and Down meme"""
strings = {
"name": "Rofls",
"no_reply": "<b>Error:</b> Reply to a user",
"no_uid": "<b>Error:</b> Cannot get user ID",
"no_avatar": "<b>Error:</b> No avatar",
"avatar_err": "<b>Error:</b> Cannot process avatar",
"ffmpeg_err": "<b>Error:</b> FFmpeg error",
"error": "<b>Error:</b>\n<blockquote>{}</blockquote>",
"pack_info": (
"<b>PetPet Pack</b>\n"
"<blockquote>"
"Stickers: <code>{count}</code>\n"
"Tracked: <code>{tracked}</code>\n"
"<a href=\"{link}\">Open pack</a>"
"</blockquote>"
),
"pack_none": (
"<b>PetPet Pack</b>\n"
"<blockquote>Pack does not exist yet.</blockquote>"
),
"pack_err": (
"<b>PetPet Pack</b>\n"
"<blockquote>No pack info available.</blockquote>"
),
"pack_cleared": "<b>Pack cleared:</b>\n<blockquote><code>{}</code></blockquote>",
"no_stickers": "<b>Error:</b> No stickers to remove",
"last_removed": "<b>Last sticker removed</b>",
"btn_delete_pack": "Delete Pack",
"btn_delete_last": "Delete Last Sticker",
"btn_close": "Close",
}
strings_ru = {
"no_reply": "<b>Ошибка:</b> Ответьте на сообщение пользователя",
"no_uid": "<b>Ошибка:</b> Не удалось получить ID пользователя",
"no_avatar": "<b>Ошибка:</b> Нет аватара",
"avatar_err": "<b>Ошибка:</b> Не удалось обработать аватар",
"ffmpeg_err": "<b>Ошибка:</b> Ошибка FFmpeg",
"error": "<b>Ошибка:</b>\n<blockquote>{}</blockquote>",
"pack_info": (
"<b>PetPet Pack</b>\n"
"<blockquote>"
"Стикеров: <code>{count}</code>\n"
"Отслеживается: <code>{tracked}</code>\n"
"<a href=\"{link}\">Открыть пак</a>"
"</blockquote>"
),
"pack_none": (
"<b>PetPet Pack</b>\n"
"<blockquote>Пак ещё не создан.</blockquote>"
),
"pack_err": (
"<b>PetPet Pack</b>\n"
"<blockquote>Нет данных о паке.</blockquote>"
),
"pack_cleared": "<b>Пак очищен:</b>\n<blockquote><code>{}</code></blockquote>",
"no_stickers": "<b>Ошибка:</b> Нет стикеров для удаления",
"last_removed": "<b>Последний стикер удалён</b>",
"btn_delete_pack": "Удалить пак",
"btn_delete_last": "Удалить последний",
"btn_close": "Закрыть",
}
def __init__(self):
self.config = loader.ModuleConfig(
loader.ConfigValue(
"MAKE_CIRCLE_IN_PETPET",
False,
"If True, avatar will be cropped into a circle in petpet sticker",
validator=loader.validators.Boolean(),
),
)
async def _get_sn(self, client):
me = await client.get_me()
return f"petpetpackby_{me.id}"
@loader.command(
ru_doc="Ответьте на пользователя для petpet стикера, или без реплая для управления паком",
en_doc="Reply to user for petpet sticker, or send without reply for management",
)
async def petcmd(self, message):
"""Reply to user for petpet sticker, or send without reply for management"""
reply = await message.get_reply_message()
if not reply:
sn = await self._get_sn(message.client)
sticker_ids = self.get("sticker_ids", [])
try:
ss = InputStickerSetShortName(sn)
sticker_set = await message.client(GetStickerSetRequest(ss, 0))
count = len(sticker_set.documents)
link = f"https://t.me/addstickers/{sn}"
text = self.strings["pack_info"].format(
count=count,
tracked=len(sticker_ids),
link=link,
)
except StickersetInvalidError:
text = self.strings["pack_none"]
except Exception:
text = self.strings["pack_err"]
await self.inline.form(
text=text,
message=message,
reply_markup=[
[{"text": self.strings["btn_delete_pack"], "callback": self._cb_delete_pack, "style": "danger"}],
[{"text": self.strings["btn_delete_last"], "callback": self._cb_delete_last, "style": "primary"}],
[{"text": self.strings["btn_close"], "callback": self._cb_close, "style": "primary"}],
],
)
return
sender = await reply.get_sender()
uid = getattr(sender, "id", None)
if not uid:
await utils.answer(message, self.strings["no_uid"])
return
try:
await message.delete()
a = io.BytesIO()
r = await message.client.download_profile_photo(uid, file=a)
if not r:
await message.client.send_message(
message.chat_id,
self.strings["no_avatar"],
parse_mode="html",
)
return
a.seek(0)
data = a.read()
src = _resolve_avatar(data)
if not src:
await message.client.send_message(
message.chat_id,
self.strings["avatar_err"],
parse_mode="html",
)
return
circle = self.config["MAKE_CIRCLE_IN_PETPET"]
loop = asyncio.get_event_loop()
w = await loop.run_in_executor(None, _make_petpet_webm, src, circle)
if not w:
await message.client.send_message(
message.chat_id,
self.strings["ffmpeg_err"],
parse_mode="html",
)
return
me = await message.client.get_me()
sn = f"petpetpackby_{me.id}"
u = await message.client.upload_file(w, file_name="sticker.webm")
d = get_input_document(
await message.client(UploadMediaRequest(InputPeerSelf(), u))
)
sticker = InputStickerSetItem(document=d, emoji="\U0001F43E")
ss = InputStickerSetShortName(sn)
new_doc_id = None
try:
old = await message.client(GetStickerSetRequest(ss, 0))
old_ids = [x.id for x in old.documents]
res = await message.client(AddStickerToSetRequest(ss, sticker))
new_docs = [x for x in res.documents if x.id not in old_ids]
if new_docs:
new_doc_id = new_docs[0]
except StickersetInvalidError:
res = await message.client(
CreateStickerSetRequest(
user_id=me,
title="PetPet by @Execute_forge",
short_name=sn,
stickers=[sticker],
)
)
if res.documents:
new_doc_id = res.documents[0]
os.unlink(w)
sticker_ids = self.get("sticker_ids", [])
if new_doc_id:
sticker_ids.append(new_doc_id.id)
self.set("sticker_ids", sticker_ids)
if new_doc_id:
await message.client.send_file(
message.chat_id,
new_doc_id,
reply_to=reply.id,
)
else:
await message.client.send_message(
message.chat_id,
f"Done: https://t.me/addstickers/{sn}",
reply_to=reply.id,
)
except Exception as e:
await message.client.send_message(
message.chat_id,
self.strings["error"].format(e),
parse_mode="html",
)
@loader.command(
ru_doc="Ответьте на пользователя для down мема",
en_doc="Reply to user for down meme",
)
async def dncmd(self, message):
"""Reply to user for down meme"""
reply = await message.get_reply_message()
if not reply:
await utils.answer(message, self.strings["no_reply"])
return
sender = await reply.get_sender()
try:
await message.delete()
base_bytes = await self._fetch_url(BASE_IMAGE_URL)
avatar_bytes = await self._get_avatar(message.client, sender)
if sender:
first = getattr(sender, "first_name", "") or ""
last = getattr(sender, "last_name", "") or ""
name = (first + " " + last).strip() or getattr(sender, "username", None) or "??"
uid = getattr(sender, "id", 0) or 0
else:
name, uid = "??", 0
if not avatar_bytes:
avatar_bytes = _make_name_avatar(name, uid)
loop = asyncio.get_event_loop()
mp4_buf = await loop.run_in_executor(
None, _make_down_mp4, base_bytes, avatar_bytes
)
await message.client.send_file(
message.chat_id,
mp4_buf,
caption=None,
reply_to=reply.id,
force_document=False,
)
except Exception as e:
await message.client.send_message(
message.chat_id,
self.strings["error"].format(e),
parse_mode="html",
)
async def _cb_delete_pack(self, call):
client = self._client
sn = await self._get_sn(client)
ss = InputStickerSetShortName(sn)
try:
sticker_set = await client(GetStickerSetRequest(ss, 0))
for doc in sticker_set.documents:
try:
inp = get_input_document(doc)
await client(RemoveStickerFromSetRequest(inp))
except Exception:
pass
except StickersetInvalidError:
pass
except Exception as e:
await call.edit(self.strings["error"].format(e))
return
self.set("sticker_ids", [])
await call.edit(
self.strings["pack_cleared"].format(sn),
reply_markup=[
[{"text": self.strings["btn_close"], "callback": self._cb_close, "style": "primary"}],
],
)
async def _cb_delete_last(self, call):
client = self._client
sticker_ids = self.get("sticker_ids", [])
if not sticker_ids:
await call.edit(
self.strings["no_stickers"],
reply_markup=[
[{"text": self.strings["btn_close"], "callback": self._cb_close, "style": "primary"}],
],
)
return
sn = await self._get_sn(client)
ss = InputStickerSetShortName(sn)
try:
sticker_set = await client(GetStickerSetRequest(ss, 0))
last_id = sticker_ids[-1]
target_doc = None
for doc in sticker_set.documents:
if doc.id == last_id:
target_doc = doc
break
if not target_doc:
target_doc = sticker_set.documents[-1]
inp = get_input_document(target_doc)
await client(RemoveStickerFromSetRequest(inp))
sticker_ids.pop()
self.set("sticker_ids", sticker_ids)
await call.edit(
self.strings["last_removed"],
reply_markup=[
[{"text": self.strings["btn_close"], "callback": self._cb_close, "style": "primary"}],
],
)
except Exception as e:
await call.edit(self.strings["error"].format(e))
async def _cb_close(self, call):
await call.delete()
async def _fetch_url(self, url):
async with aiohttp.ClientSession() as session:
async with session.get(url, timeout=aiohttp.ClientTimeout(total=15)) as resp:
resp.raise_for_status()
return await resp.read()
async def _get_avatar(self, client, entity):
try:
buf = io.BytesIO()
result = await client.download_profile_photo(entity, file=buf)
if result is None:
return None
buf.seek(0)
data = buf.read()
return data if data else None
except Exception:
return None