-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.py
475 lines (404 loc) · 14.8 KB
/
functions.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
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
import asyncio
import os
import zipfile
from random import choice
from textwrap import wrap
from uuid import uuid4
import requests
from googletrans import Translator
from PIL import Image, ImageOps
from telethon import functions, types
from ..utils.extdl import install_pip
try:
from imdb import IMDb
except ModuleNotFoundError:
install_pip("IMDbPY")
from imdb import IMDb
from html_telegraph_poster import TelegraphPoster
from PIL import Image, ImageColor, ImageDraw, ImageFont, ImageOps
from telethon.errors.rpcerrorlist import YouBlockedUserError
from telethon.tl.functions.contacts import UnblockRequest as unblock
from ...Config import Config
from ...core.logger import logging
from ...sql_helper.globals import gvarstatus
from ..resources.states import states
LOGS = logging.getLogger(__name__)
imdb = IMDb()
mov_titles = [
"long imdb title",
"long imdb canonical title",
"smart long imdb canonical title",
"smart canonical title",
"canonical title",
"localized title",
]
# ----------------------------------------------## Scrap ##------------------------------------------------------------#
async def get_cast(casttype, movie):
mov_casttype = ""
if casttype in list(movie.keys()):
i = 0
for j in movie[casttype]:
if i < 1:
mov_casttype += str(j)
elif i < 5:
mov_casttype += f", {str(j)}"
else:
break
i += 1
else:
mov_casttype += "Not Data"
return mov_casttype
async def get_moviecollections(movie):
result = ""
if "box office" in movie.keys():
for i in movie["box office"].keys():
result += f"\n• <b>{i}:</b> <code>{movie['box office'][i]}</code>"
else:
result = "<code>No Data</code>"
return result
def rand_key():
return str(uuid4())[:8]
async def sanga_seperator(sanga_list):
for i in sanga_list:
if i.startswith("🔗"):
sanga_list.remove(i)
s = 0
for i in sanga_list:
if i.startswith("Username History"):
break
s += 1
usernames = sanga_list[s:]
names = sanga_list[:s]
return names, usernames
# covid india data
async def covidindia(state):
url = "https://www.mohfw.gov.in/data/datanew.json"
req = requests.get(url).json()
return next((req[states.index(i)] for i in states if i == state), None)
async def post_to_telegraph(
page_title,
html_format_content,
auth_name="Zed-Thon",
auth_url="https://t.me/ZedThon",
):
post_client = TelegraphPoster(use_api=True)
post_client.create_api_token(auth_name)
post_page = post_client.post(
title=page_title,
author=auth_name,
author_url=auth_url,
text=html_format_content,
)
return post_page["url"]
# --------------------------------------------------------------------------------------------------------------------#
# ----------------------------------------------## Media ##-----------------------------------------------------------#
async def age_verification(event, reply_to_id):
ALLOW_NSFW = gvarstatus("ALLOW_NSFW") or "False"
if ALLOW_NSFW.lower() == "true":
return False
results = await event.client.inline_query(
Config.TG_BOT_USERNAME, "age_verification_alert"
)
await results[0].click(event.chat_id, reply_to=reply_to_id, hide_via=True)
await event.delete()
return True
async def unsavegif(event, sandy):
try:
await event.client(
functions.messages.SaveGifRequest(
id=types.InputDocument(
id=sandy.media.document.id,
access_hash=sandy.media.document.access_hash,
file_reference=sandy.media.document.file_reference,
),
unsave=True,
)
)
except Exception as e:
LOGS.info(str(e))
async def animator(media, mainevent, textevent=None):
# //Hope u dunt kang :/ @Jisan7509
if not os.path.isdir(Config.TEMP_DIR):
os.makedirs(Config.TEMP_DIR)
OldZed = await mainevent.client.download_media(media, Config.TEMP_DIR)
file = await fileinfo(OldZed)
h = file["height"]
w = file["width"]
w, h = (-1, 512) if h > w else (512, -1)
if textevent:
await textevent.edit("__🎞Converting into Animated sticker..__")
await runcmd(
f"ffmpeg -to 00:00:02.900 -i '{OldZed}' -vf scale={w}:{h} -c:v libvpx-vp9 -crf 30 -b:v 560k -maxrate 560k -bufsize 256k -an animate.webm"
) # pain
os.remove(OldZed)
return "animate.webm"
# --------------------------------------------------------------------------------------------------------------------#
# ----------------------------------------------## Bots ##------------------------------------------------------------#
async def clippy(borg, msg, chat_id, reply_to_id):
chat = "@clippy"
async with borg.conversation(chat) as conv:
try:
msg = await conv.send_file(msg)
except YouBlockedUserError:
await borg(unblock("clippy"))
msg = await conv.send_file(msg)
pic = await conv.get_response()
await borg.send_read_acknowledge(conv.chat_id)
await borg.send_file(
chat_id,
pic,
reply_to=reply_to_id,
)
await borg.delete_messages(conv.chat_id, [msg.id, pic.id])
async def hide_inlinebot(borg, bot_name, text, chat_id, reply_to_id, c_lick=0):
sticcers = await borg.inline_query(bot_name, f"{text}.")
cat = await sticcers[c_lick].click("me", hide_via=True)
if cat:
await borg.send_file(int(chat_id), cat, reply_to=reply_to_id)
await cat.delete()
async def make_inline(text, borg, chat_id, reply_to_id):
catinput = f"Inline buttons {text}"
results = await borg.inline_query(Config.TG_BOT_USERNAME, catinput)
await results[0].click(chat_id, reply_to=reply_to_id)
async def delete_conv(event, chat, from_message):
itermsg = event.client.iter_messages(chat, min_id=from_message.id)
msgs = [from_message.id]
async for i in itermsg:
msgs.append(i.id)
await event.client.delete_messages(chat, msgs)
await event.client.send_read_acknowledge(chat)
# --------------------------------------------------------------------------------------------------------------------#
# ----------------------------------------------## Tools ##------------------------------------------------------------#
# https://www.tutorialspoint.com/How-do-you-split-a-list-into-evenly-sized-chunks-in-Python
def sublists(input_list: list, width: int = 3):
return [input_list[x : x + width] for x in range(0, len(input_list), width)]
# split string into fixed length substrings
def chunkstring(string, length):
return (string[0 + i : length + i] for i in range(0, len(string), length))
# unziping file
async def unzip(downloaded_file_name):
with zipfile.ZipFile(downloaded_file_name, "r") as zip_ref:
zip_ref.extractall("./temp")
downloaded_file_name = os.path.splitext(downloaded_file_name)[0]
return f"{downloaded_file_name}.gif"
# https://github.com/ssut/py-googletrans/issues/234#issuecomment-722379788
async def getTranslate(text, **kwargs):
translator = Translator()
result = None
for _ in range(10):
try:
result = translator.translate(text, **kwargs)
except Exception:
translator = Translator()
await asyncio.sleep(0.1)
return result
def reddit_thumb_link(preview, thumb=None):
for i in preview:
if "width=216" in i:
thumb = i
break
if not thumb:
thumb = preview.pop()
return thumb.replace("\u0026", "&")
# --------------------------------------------------------------------------------------------------------------------#
# ----------------------------------------------## Image ##------------------------------------------------------------#
def ellipse_create(filename, size, border):
img = Image.open(filename)
img = img.resize((int(1024 / size), int(1024 / size)))
drawsize = (img.size[0] * 3, img.size[1] * 3)
mask = Image.new("L", drawsize, 0)
draw = ImageDraw.Draw(mask)
draw.ellipse((0, 0) + drawsize, fill=255, outline="green", width=int(border))
mask = mask.resize(img.size, Image.ANTIALIAS)
img.putalpha(mask)
return img, mask
def ellipse_layout_create(filename, size, border):
x, mask = ellipse_create(filename, size, border)
return ImageOps.expand(mask)
def text_draw(font_name, font_size, img, text, hight, stroke_width=0, stroke_fill=None):
font = ImageFont.truetype(font_name, font_size)
draw = ImageDraw.Draw(img)
w, h = draw.textsize(text, font=font)
h += int(h * 0.21)
draw.text(
((1024 - w) / 2, int(hight)),
text=text,
fill="white",
font=font,
stroke_width=stroke_width,
stroke_fill=stroke_fill,
)
def higlighted_text(
input_img,
text,
align="center",
background="black",
foreground="white",
stroke_fill="white",
linespace="+2",
rad=20,
text_wrap=2,
font_size=60,
stroke_width=0,
transparency=255,
position=(0, 0),
album=False,
lines=None,
direction=None,
font_name=None,
album_limit=None,
): # sourcery skip: low-code-quality
templait = Image.open(input_img)
# resize image
raw_width, raw_height = templait.size
resized_width, resized_height = (
(1024, int(1024 * raw_height / raw_width))
if raw_width > raw_height
else (int(1024 * raw_width / raw_height), 1024)
)
if font_name is None:
font_name = "zthon/helpers/styles/impact.ttf"
font = ImageFont.truetype(font_name, font_size)
extra_width, extra_height = position
# get text size
text_width, text_height = font.getsize(text)
width = 50 + extra_width
hight = 30 + extra_height
# wrap the text & save in a list
mask_size = int((resized_width / text_wrap) + 50)
list_text = []
output = []
output_text = []
raw_text = text.splitlines()
for item in raw_text:
input_text = "\n".join(wrap(item, int((40.0 / resized_width) * mask_size)))
split_text = input_text.splitlines()
list_text.extend(iter(split_text))
texts = [list_text]
if album and len(list_text) > lines:
texts = [list_text[i : i + lines] for i in range(0, len(list_text), lines)]
for pic_no, list_text in enumerate(texts):
# create image with correct size and black background
source_img = templait.convert("RGBA").resize((resized_width, resized_height))
if direction == "upwards":
list_text.reverse()
operator = "-"
hight = (
resized_height - (text_height + int(text_height / 1.2)) + extra_height
)
else:
operator = "+"
for i, items in enumerate(list_text):
x, y = (
font.getsize(list_text[i])[0] + 50,
int(text_height * 2 - (text_height / 2)),
)
# align masks on the image....left,right & center
if align == "center":
width_align = "((mask_size-x)/2)"
elif align == "left":
width_align = "0"
elif align == "right":
width_align = "(mask_size-x)"
color = ImageColor.getcolor(background, "RGBA")
if transparency == 0:
mask_img = Image.new(
"RGBA", (x, y), (color[0], color[1], color[2], 0)
) # background
mask_draw = ImageDraw.Draw(mask_img)
mask_draw.text(
(25, 8),
list_text[i],
foreground,
font=font,
stroke_width=stroke_width,
stroke_fill=stroke_fill,
)
else:
mask_img = Image.new(
"RGBA", (x, y), (color[0], color[1], color[2], transparency)
) # background
# put text on mask
mask_draw = ImageDraw.Draw(mask_img)
mask_draw.text(
(25, 8),
list_text[i],
foreground,
font=font,
stroke_width=stroke_width,
stroke_fill=stroke_fill,
)
# https://stackoverflow.com/questions/11287402/how-to-round-corner-a-logo-without-white-backgroundtransparent-on-it-using-pi
circle = Image.new("L", (rad * 2, rad * 2), 0)
draw = ImageDraw.Draw(circle)
draw.ellipse((0, 0, rad * 2, rad * 2), transparency)
alpha = Image.new("L", mask_img.size, transparency)
mask_width, mask_height = mask_img.size
alpha.paste(circle.crop((0, 0, rad, rad)), (0, 0))
alpha.paste(circle.crop((0, rad, rad, rad * 2)), (0, mask_height - rad))
alpha.paste(circle.crop((rad, 0, rad * 2, rad)), (mask_width - rad, 0))
alpha.paste(
circle.crop((rad, rad, rad * 2, rad * 2)),
(mask_width - rad, mask_height - rad),
)
mask_img.putalpha(alpha)
# put mask_img on source image & trans remove the corner white
trans = Image.new("RGBA", source_img.size)
trans.paste(
mask_img,
(
(int(width) + int(eval(f"{width_align}"))),
(eval(f"{hight} {operator}({y*i}+({int(linespace)*i}))")),
),
)
source_img = Image.alpha_composite(source_img, trans)
output_text.append(list_text[i])
output_img = f"./temp/cat{pic_no}.jpg"
output.append(output_img)
source_img.save(output_img, "png")
if album_limit and (album_limit - 1) == pic_no:
break
return output, output_text
# ----------------------------------------------------------------------------------------------------------------------#
# ----------------------------------------------## Sticker ##-----------------------------------------------------------#
# for stickertxt
async def waifutxt(text, chat_id, reply_to_id, bot):
animus = [
0,
1,
2,
3,
4,
9,
15,
20,
22,
27,
29,
32,
33,
34,
37,
38,
41,
42,
44,
45,
47,
48,
51,
52,
53,
55,
56,
57,
58,
61,
62,
63,
]
sticcers = await bot.inline_query("stickerizerbot", f"#{choice(animus)}{text}")
cat = await sticcers[0].click("me", hide_via=True)
if cat:
await bot.send_file(int(chat_id), cat, reply_to=reply_to_id)
await cat.delete()