File size: 11,038 Bytes
89a7a7a
4e9cef8
89a7a7a
 
 
 
818420a
89a7a7a
 
4e9cef8
89a7a7a
aef1161
89a7a7a
c62ca93
 
ea508b7
818420a
89a7a7a
b48df3a
aef1161
c62ca93
 
 
4e9cef8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89ad488
 
 
 
 
 
 
 
4e9cef8
 
6cef7ec
f192ee1
c62ca93
6dcea66
c62ca93
89ad488
6cef7ec
c62ca93
 
 
 
 
 
 
 
 
6cef7ec
 
c62ca93
 
 
 
6dcea66
 
c62ca93
 
89ad488
 
c62ca93
 
6dcea66
c62ca93
 
89ad488
c62ca93
 
 
6dcea66
c62ca93
 
89ad488
c62ca93
6dcea66
 
 
 
 
 
 
 
c62ca93
 
89ad488
c62ca93
aef1161
89a7a7a
ea508b7
 
 
 
 
 
 
 
 
 
 
 
 
51e1c40
89a7a7a
6cef7ec
89a7a7a
 
 
 
 
 
 
 
 
 
 
 
 
6cef7ec
 
 
 
 
 
 
89a7a7a
 
 
 
 
 
6dcea66
89a7a7a
 
 
 
 
6cef7ec
 
 
89a7a7a
 
 
 
 
 
 
 
 
 
 
 
6cef7ec
89a7a7a
 
 
 
51e1c40
89a7a7a
c26bbaf
89a7a7a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aef1161
6cef7ec
aef1161
 
 
ea508b7
51e1c40
ea508b7
 
 
 
 
 
 
 
 
51e1c40
ea508b7
 
 
 
 
51e1c40
 
ea508b7
 
 
 
 
 
 
89ad488
ea508b7
 
 
 
 
 
89a7a7a
 
 
6cef7ec
89a7a7a
 
 
 
 
51e1c40
 
89a7a7a
 
 
 
 
 
aef1161
89a7a7a
 
 
 
 
 
 
 
 
 
 
6cef7ec
89a7a7a
 
 
 
 
 
 
 
51e1c40
818420a
 
 
 
863c802
1036b06
863c802
 
 
 
89a7a7a
 
 
aef1161
863c802
 
 
818420a
863c802
 
 
 
818420a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
863c802
 
aef1161
 
863c802
 
 
51e1c40
863c802
 
 
 
 
 
 
 
 
ba92e70
89ad488
ba92e70
51e1c40
ba92e70
aef1161
 
 
982e098
aef1161
 
 
 
ba92e70
89ad488
ba92e70
51e1c40
ba92e70
aef1161
 
 
 
 
51e1c40
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
import asyncio
import base64
import math
import os
import shlex
import textwrap
from time import time
from typing import List, Tuple

import requests
from PIL import Image, ImageDraw, ImageFont
from pyrogram import errors, raw
from pyrogram.file_id import FileId
from pyrogram.types import InlineKeyboardButton as ikb
from pyrogram.types import InlineKeyboardMarkup as ikm
from pyrogram.types import Message
from unidecode import unidecode

from Powers import scrap_dir
from Powers.bot_class import Gojo
from Powers.utils.string import encode_decode


def quotify(msg_info: List[dict]) -> Tuple[bool, str]:
    json = {
        "type": "quote",
        "format": "webp",
        "backgroundColor": "#000000",
        "width": 512,
        "height": 768,
        "scale": 2,
        "messages": msg_info,
    }

    try:
        response = requests.post("https://bot.lyo.su/quote/generate", json=json).json()
        image = base64.b64decode(str(response["result"]["image"]).encode("utf-8"))

        file_name = f"Quote_{int(time())}.webp"
        with open(file_name, "wb") as f:
            f.write(image)

        return True, file_name
    except Exception as e:
        return False, str(e)


def get_msg_entities(m: Message) -> List[dict]:
    entities = []

    if m.entities:
        entities.extend(
            {
                "type": entity.type.name.lower(),
                "offset": entity.offset,
                "length": entity.length,
            }
            for entity in m.entities
        )
    return entities


async def get_all_sticker_packs(c: Gojo, user_id: int, offset: int = 1, limit: int = 25):
    packnum = 25 * (offset - 1)
    txt = f"Here is your stickers pack that I have created:\nPage: {offset}\n\nNOTE: I may have kanged more sticker sets for you, but since last update I will no longer add stickers in those packs due to recent telegram update in bot api sorry."
    while True:
        packname = f"CE{user_id}{packnum}_by_{c.me.username}"
        sticker_set = await get_sticker_set_by_name(c, packname)
        if not sticker_set and packnum == 0:
            txt, kb = None, None
            break
        elif sticker_set and packnum <= (packnum + limit - 1):
            base_ = f"t.me/addstickers/{packname}"
            txt += f"`{packnum}`. [{sticker_set.set.name}]({base_})\n"
            packnum += 1
        else:
            page = await encode_decode(f"1_{user_id}")
            b64_next = await encode_decode(f"{offset + 1}_{user_id}")
            b64_prev = await encode_decode(f"{offset - 1}_{user_id}")

            if (packnum > (packnum + limit - 1)) and offset >= 2:
                kb = [
                    [
                        ikb("Previous", f"stickers_{b64_prev}"),
                        ikb("Next", f"stickers_{b64_next}")
                    ],
                ]

            elif offset >= 2:
                kb = [
                    [
                        ikb("Previous", f"stickers_{b64_prev}")
                    ],
                ]

            elif packnum > (packnum + limit - 1) and offset == 1:
                kb = [
                    [
                        ikb("Next", f"stickers_{b64_next}")
                    ],
                ]

            else:
                kb = [
                    [
                        ikb(
                            "Close ❌",
                            callback_data="f_close"
                        )
                    ]
                ]
            kb = ikm(kb)
            break

    return txt, kb


async def runcmd(cmd: str) -> Tuple[str, str, int, int]:
    args = shlex.split(cmd)
    process = await asyncio.create_subprocess_exec(
        *args, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE
    )
    stdout, stderr = await process.communicate()
    return (
        stdout.decode("utf-8", "replace").strip(),
        stderr.decode("utf-8", "replace").strip(),
        process.returncode,
        process.pid,
    )


async def get_sticker_set_by_name(
        client: Gojo, name: str
) -> raw.base.messages.StickerSet:
    try:
        return await client.invoke(
            raw.functions.messages.GetStickerSet(
                stickerset=raw.types.InputStickerSetShortName(short_name=name),
                hash=0,
            )
        )
    except errors.exceptions.not_acceptable_406.StickersetInvalid:
        return None


async def create_sticker_set(
        client: Gojo,
        owner: int,
        title: str,
        short_name: str,
        stickers: List[raw.base.InputStickerSetItem],
        animated: bool = False,
        video: bool = False
) -> raw.base.messages.StickerSet:
    return await client.invoke(
        raw.functions.stickers.CreateStickerSet(
            user_id=await client.resolve_peer(owner),
            title=title,
            short_name=short_name,
            stickers=stickers
        )
    )


async def add_sticker_to_set(
        client: Gojo,
        stickerset: raw.base.messages.StickerSet,
        sticker: raw.base.InputStickerSetItem,
) -> raw.base.messages.StickerSet:
    return await client.invoke(
        raw.functions.stickers.AddStickerToSet(
            stickerset=raw.types.InputStickerSetShortName(
                short_name=stickerset.set.short_name
            ),
            sticker=sticker,
        )
    )


async def create_sticker(
        sticker: raw.base.InputDocument, emoji: str
) -> raw.base.InputStickerSetItem:
    return raw.types.InputStickerSetItem(document=sticker, emoji=emoji)


async def resize_file_to_sticker_size(file_path: str, length: int = 512, width: int = 512) -> str:
    im = Image.open(file_path)
    STICKER_DIMENSIONS = (length, width)
    if (im.width, im.height) < STICKER_DIMENSIONS:
        size1 = im.width
        size2 = im.height
        if im.width > im.height:
            scale = length / size1
            size1new = length
            size2new = size2 * scale
        else:
            scale = width / size2
            size1new = size1 * scale
            size2new = width
        size1new = math.floor(size1new)
        size2new = math.floor(size2new)
        sizenew = (size1new, size2new)
        im = im.resize(sizenew)
    else:
        im.thumbnail(STICKER_DIMENSIONS)

    file_pathh = f"{scrap_dir}r{str(time()).replace('.', '_')}.png"
    im.save(file_pathh)
    os.remove(file_path)
    return file_pathh


async def tgs_to_gif(file, tgs=False, video=False):
    if tgs:
        cmd = f"lottie_convert.py '{file}' 'gojo_satoru.gif'"
    elif video:
        cmd = f"ffmpeg -i '{file}' -c copy 'gojo_satoru.gif'"
    await runcmd(cmd)
    os.remove(file)
    return 'gojo_satoru.gif'


async def webm_to_gif(file):
    cmd = f"ffmpeg -i '{file}' 'goJo.gif'"
    await runcmd(cmd)
    os.remove(file)
    return "goJo.gif"


async def Vsticker(c: Gojo, file: Message):
    if file.animation:
        file = file.animation
    elif file.video:
        file = file.video
    _width_ = file.width
    _height_ = file.height
    _height_, _width_ = (512, -1) if _height_ > _width_ else (-1, 512)
    file = await c.download_media(file)
    await runcmd(
        f"ffmpeg -to 00:00:02.900 -i '{file}' -vf scale={_width_}:{_height_} -c:v libvpx-vp9 -crf 30 -b:v 560k -maxrate 560k -bufsize 256k -an 'VideoSticker.webm'"
    )
    os.remove(file)
    return "VideoSticker.webm"


async def upload_document(
        client: Gojo, file_path: str, chat_id: int
) -> raw.base.InputDocument:
    media = await client.invoke(
        raw.functions.messages.UploadMedia(
            peer=await client.resolve_peer(chat_id),
            media=raw.types.InputMediaUploadedDocument(
                mime_type=client.guess_mime_type(
                    file_path) or "application/zip",
                file=await client.save_file(file_path),
                attributes=[
                    raw.types.DocumentAttributeFilename(
                        file_name=os.path.basename(file_path)
                    )
                ],
                force_file=True,
            ),
        )
    )
    return raw.types.InputDocument(
        id=media.document.id,
        access_hash=media.document.access_hash,
        file_reference=media.document.file_reference,
    )


async def get_document_from_file_id(
        file_id: str,
) -> raw.base.InputDocument:
    decoded = FileId.decode(file_id)
    return raw.types.InputDocument(
        id=decoded.media_id,
        access_hash=decoded.access_hash,
        file_reference=decoded.file_reference,
    )


async def remove_sticker(c: Gojo, stickerid: str) -> raw.base.messages.StickerSet:
    sticker = await get_document_from_file_id(stickerid)
    return await c.invoke(raw.functions.stickers.RemoveStickerFromSet(sticker=sticker))


async def draw_meme(image_path: str, text: str, sticker: bool, fiill: str) -> list:
    _split = text.split(";", 1)
    if len(_split) == 2:
        lower_text = _split[1]
        upper_text = _split[0]
    else:
        upper_text = text
        lower_text = ""

    image = Image.open(image_path)
    width, height = image.size

    font_size = int((30 / 500) * width)
    font = ImageFont.truetype("./extras/comic.ttf", font_size)

    draw = ImageDraw.Draw(image)

    curr_height, padding = 20, 5
    for utext in textwrap.wrap(upper_text, 25):
        upper_width = draw.textlength(utext, font=font)
        draw.text(
            ((width - upper_width) / 2, curr_height),
            unidecode(utext),
            (255, 255, 255),
            font,
            stroke_width=3,
            stroke_fill=fiill,
        )
        curr_height += font_size + padding

    curr_height = height - font_size
    for ltext in reversed(textwrap.wrap(lower_text, 25)):
        lower_width = draw.textlength(ltext, font=font)
        draw.text(
            ((width - lower_width) / 2, curr_height - font_size),
            ltext,
            (255, 255, 255),
            font,
            stroke_width=3,
            stroke_fill=fiill,
        )
        curr_height -= font_size + padding

    if sticker:
        stick_path = image_path
    else:
        stick_path = await resize_file_to_sticker_size(image_path)

    image1 = image_path
    image2 = tosticker(stick_path, f"@GojoSuperbot_{int(time())}.webp")

    image.save(image1)
    image.save(image2)

    image.close()

    return [image1, image2]


def toimage(image, filename=None, is_direc=False):
    filename = filename or "gojo.png"
    if is_direc:
        os.rename(image, filename)
        return filename
    img = Image.open(image)
    if img.mode != "RGB":
        img = img.convert("RGB")
    img.save(filename, "png")
    os.remove(image)
    return filename


def tosticker(response, filename=None, is_direc=False):
    filename = filename or "gojo.webp"
    if is_direc:
        os.rename(response, filename)
        return filename
    image = Image.open(response)
    if image.mode != "RGB":
        image.convert("RGB")
    image.save(filename, "webp")
    os.remove(response)
    return filename