|
7 | 7 | # Please see: https://github.com/TgCatUB/catuserbot/blob/master/LICENSE |
8 | 8 | # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~# |
9 | 9 |
|
| 10 | +import io |
10 | 11 | import json |
11 | 12 | import math |
12 | 13 | import os |
13 | 14 | import random |
14 | 15 | import re |
| 16 | +import requests |
15 | 17 | import time |
| 18 | +import urllib.parse |
| 19 | +import textwrap |
16 | 20 | from pathlib import Path |
17 | 21 |
|
| 22 | +from PIL import Image, ImageDraw, ImageFont |
18 | 23 | from telethon import Button, types |
19 | 24 | from telethon.events import CallbackQuery, InlineQuery |
20 | 25 | from youtubesearchpython import VideosSearch |
@@ -475,6 +480,9 @@ async def inline_handler(event): |
475 | 480 | elif str_y[0].lower() == "ytdl" and len(str_y) == 2: |
476 | 481 | result = await youtube_data_article(event, str_y) |
477 | 482 | await event.answer([result] if result else None) |
| 483 | + elif str_y[0].lower() == "google" and len(str_y) >= 2: |
| 484 | + result = await google_search_article(event, str_y[1].strip()) |
| 485 | + await event.answer([result] if result else None) |
478 | 486 | elif string == "age_verification_alert": |
479 | 487 | result = await age_verification_article(event) |
480 | 488 | await event.answer([result] if result else None) |
@@ -541,6 +549,65 @@ async def youtube_data_article(event, str_y): |
541 | 549 | return result |
542 | 550 |
|
543 | 551 |
|
| 552 | +async def google_search_article(event, input_str): |
| 553 | + font_url = "https://github.com/TgCatUB/CatUserbot-Resources/raw/master/Resources/fonts/roboto_regular.ttf" |
| 554 | + image_url = "https://raw.githubusercontent.com/ZAR0X/CatUserbot-Resources/refs/heads/master/Resources/Inline/google.webp" |
| 555 | + font_res = requests.get(font_url) |
| 556 | + font_res.raise_for_status() |
| 557 | + font_data = io.BytesIO(font_res.content) |
| 558 | + |
| 559 | + img_res = requests.get(image_url) |
| 560 | + img_res.raise_for_status() |
| 561 | + img = Image.open(io.BytesIO(img_res.content)).convert("RGBA") |
| 562 | + |
| 563 | + draw = ImageDraw.Draw(img) |
| 564 | + |
| 565 | + box_x1, box_y1, box_x2, box_y2 = 40, 240, 472, 370 |
| 566 | + box_width = box_x2 - box_x1 |
| 567 | + box_height = box_y2 - box_y1 |
| 568 | + |
| 569 | + font_size = 40 |
| 570 | + if len(input_str) > 35: |
| 571 | + font_size = 32 |
| 572 | + if len(input_str) > 80: |
| 573 | + font_size = 24 |
| 574 | + |
| 575 | + font = ImageFont.truetype(font_data, font_size) |
| 576 | + |
| 577 | + avg_char_width = font.getlength("a") if hasattr(font, "getlength") else font.getsize("a")[0] |
| 578 | + chars_per_line = max(1, int(box_width / (avg_char_width * 1.05))) |
| 579 | + |
| 580 | + lines = textwrap.wrap(input_str, width=chars_per_line) |
| 581 | + wrapped_text = "\n".join(lines) |
| 582 | + |
| 583 | + if hasattr(draw, "multiline_textbbox"): |
| 584 | + left, top, right, bottom = draw.multiline_textbbox((0, 0), wrapped_text, font=font) |
| 585 | + else: |
| 586 | + w, h = draw.multiline_textsize(wrapped_text, font=font) |
| 587 | + left, top, right, bottom = 0, 0, w, h |
| 588 | + |
| 589 | + text_w = right - left |
| 590 | + text_h = bottom - top |
| 591 | + |
| 592 | + x = box_x1 + (box_width - text_w) / 2 |
| 593 | + y = box_y1 + (box_height - text_h) / 2 |
| 594 | + |
| 595 | + draw.multiline_text((x, y), wrapped_text, font=font, fill="#3c4043", align="center") |
| 596 | + out_buffer = io.BytesIO() |
| 597 | + out_buffer.name = "google_search.png" |
| 598 | + img.save(out_buffer, "PNG") |
| 599 | + out_buffer.seek(0) |
| 600 | + |
| 601 | + search_query_url = f"https://www.google.com/search?q={urllib.parse.quote_plus(input_str)}" |
| 602 | + buttons = [Button.url("Google Search", search_query_url)] |
| 603 | + |
| 604 | + uploaded_file = await catub.tgbot.upload_file(out_buffer) |
| 605 | + return event.builder.photo( |
| 606 | + file=uploaded_file, |
| 607 | + text="", # Ensuring only the image and button are sent |
| 608 | + buttons=buttons |
| 609 | + ) |
| 610 | + |
544 | 611 | async def hide_toll_secret(event, query, match, match3): |
545 | 612 | user_list = [] |
546 | 613 | if match3: |
|
0 commit comments