8a1da13383
Dockerfile fixes: - edu_master/phpsessid-bot: add --no-install-recommends, pin pip versions - edu_master/webinar-checker: pin pip versions with --no-cache-dir - userbot: add SHELL with pipefail for pipe operations Ruff fixes (173 → 0): - W293/W291/W292: whitespace clean via ruff format - N806: camelCase → snake_case (anilist, safone, hearts, flux, etc.) - ARG001/ARG002: prefix unused params with _ - SIM115: use context managers for file I/O - SIM117: combine nested with statements - S608: noqa on SQL f-strings (module name is validated) - E402/N812/N817: import fixes - B023: pass loop variable as argument - I001: auto-sorted imports - syntax: fixed = vs == in dtek_notif/main.py
111 lines
3.6 KiB
Python
111 lines
3.6 KiB
Python
import random
|
|
import re
|
|
from io import BytesIO
|
|
|
|
import requests
|
|
from bs4 import BeautifulSoup
|
|
from pyrogram import Client, filters
|
|
from pyrogram.errors import RPCError
|
|
from pyrogram.types import InputMediaPhoto, Message
|
|
from utils.misc import modules_help, prefix
|
|
|
|
|
|
@Client.on_message(filters.command('icon', prefix) & filters.me)
|
|
async def search_icon(_, message: Message):
|
|
if len(message.command) != 2:
|
|
return await message.edit_text('Please provide some text to search icons from Flaticon.com.')
|
|
query = message.text.split(maxsplit=1)[1]
|
|
|
|
await message.edit_text('Searching for icons...')
|
|
search_query = query.replace(' ', '%20')
|
|
url = f'https://www.flaticon.com/search?word={search_query}'
|
|
|
|
try:
|
|
html_content = requests.get(url, timeout=10).text
|
|
soup = BeautifulSoup(html_content, 'html.parser')
|
|
results = soup.find_all(
|
|
'img',
|
|
src=re.compile(r'https://cdn-icons-png.flaticon.com/128/[0-9]+/[0-9]+.png'),
|
|
)
|
|
|
|
if not results:
|
|
return await message.edit('No results found.')
|
|
|
|
random.shuffle(results)
|
|
icons = []
|
|
for i in range(5):
|
|
icons.append(results[i]['src'].replace('128', '512'))
|
|
|
|
for icon in icons:
|
|
await message.reply_document(icon)
|
|
|
|
return await message.delete()
|
|
except Exception as e:
|
|
await message.edit(f'An error occurred: {e}')
|
|
print(f'Error: {e}')
|
|
|
|
|
|
@Client.on_message(filters.command('freepik', prefix) & filters.me)
|
|
async def freepik_search(client: Client, message: Message):
|
|
parts = message.text.split(' ', 1)
|
|
if len(parts) < 2:
|
|
await message.edit_text('Please provide a search query!')
|
|
return
|
|
|
|
query = parts[1]
|
|
limit = 5
|
|
if ' ; ' in query:
|
|
match, limit_str = query.split(' ; ', 1)
|
|
try:
|
|
limit = int(limit_str)
|
|
except ValueError:
|
|
await message.edit_text('Invalid limit! Using the default value of 5.')
|
|
else:
|
|
match = query
|
|
|
|
match = match.replace(' ', '%20')
|
|
await message.edit_text('Searching Freepik...')
|
|
|
|
try:
|
|
url = f'https://www.freepik.com/api/regular/search?locale=en&term={match}'
|
|
json_content = requests.get(url, timeout=10).json()
|
|
results = []
|
|
for i in json_content['items']:
|
|
results.append(i['preview']['url'])
|
|
|
|
if results is None:
|
|
return await message.edit_text('No results found.')
|
|
|
|
random.shuffle(results)
|
|
img_urls = results[:limit]
|
|
|
|
media_group = []
|
|
for img_url in img_urls:
|
|
icon = requests.get(img_url, timeout=10)
|
|
if icon.status_code == 200:
|
|
media_group.append(InputMediaPhoto(media=BytesIO(icon.content)))
|
|
|
|
if not media_group:
|
|
await message.edit_text('No images could be downloaded.')
|
|
return
|
|
|
|
try:
|
|
await client.send_media_group(chat_id=message.chat.id, media=media_group)
|
|
except RPCError:
|
|
await message.edit_text('Failed to send some images. Retrying individually...')
|
|
for media in media_group:
|
|
try:
|
|
await message.reply_photo(photo=media.media)
|
|
except Exception as e:
|
|
await message.edit_text(f'Error sending image: {e}')
|
|
|
|
except Exception as e:
|
|
await message.edit_text(f'Failed to fetch data: {e}')
|
|
print(f'Error: {e}')
|
|
|
|
|
|
modules_help['icons'] = {
|
|
'icon [query]': 'Search for icons on Flaticon.',
|
|
'freepik [query] [limit]': 'Search for images on Freepik. Limit is optional and defaults to 5.',
|
|
}
|