7fb0a0e179
- S113: Add timeout=10 to all requests calls (74 fixes) - E722: Replace bare except: with except Exception: - B904: Replace redundant re-raise with bare raise - E402: Add noqa for intentional late imports after import_library() - S102/S307/S310/S311/S603/S605/S606/S607/S108: Add noqa for intentional usage - F601: Fix duplicate dict key in unsplash.py - N802: Rename ReplyCheck -> reply_check with backward compat alias - N813: Rename bs -> BS in icons.py - B007/B020: Rename loop var _j in animations.py - SIM102: Collapse nested if in autofwd.py - SIM113: Use enumerate() in calculator.py - A002: Add noqa for builtin shadowing in admlist.py - F811: Add noqa for cohere redefinition - edu_master: Fix ARG001, S108, S110, SIM117, apply --unsafe-fixes - Add modules_list.txt with full module inventory
92 lines
3.1 KiB
Python
92 lines
3.1 KiB
Python
import asyncio
|
|
from io import BytesIO
|
|
|
|
import requests
|
|
from PIL import Image
|
|
from pyrogram import Client, enums, filters
|
|
from pyrogram.types import InputMediaPhoto, Message
|
|
from utils.misc import modules_help, prefix
|
|
|
|
# Pinterest API URL
|
|
API_URL = 'https://bk9.fun/pinterest/search?q='
|
|
|
|
|
|
def resize_image(image_bytes):
|
|
try:
|
|
with Image.open(image_bytes) as img:
|
|
max_size = (1280, 1280)
|
|
if img.size > max_size:
|
|
img.thumbnail(max_size)
|
|
output = BytesIO()
|
|
img.save(output, format='JPEG')
|
|
output.seek(0)
|
|
return output
|
|
image_bytes.seek(0) # Reset pointer if not resized
|
|
return image_bytes
|
|
except Exception as e:
|
|
print(f'Error resizing image: {e}')
|
|
return image_bytes
|
|
|
|
|
|
async def download_image(url):
|
|
try:
|
|
response = requests.get(url, timeout=10)
|
|
if response.status_code == 200:
|
|
img_bytes = BytesIO(response.content)
|
|
return resize_image(img_bytes)
|
|
except Exception as e:
|
|
print(f'Error downloading image: {e}')
|
|
return None
|
|
|
|
|
|
@Client.on_message(filters.command('pinterest', prefix) & filters.me)
|
|
async def pinterest_search(client: Client, message: Message):
|
|
if len(message.command) < 2:
|
|
await message.edit('Usage: `pinterest [number] <query>`', parse_mode=enums.ParseMode.MARKDOWN)
|
|
return
|
|
|
|
num_pics = int(message.command[1]) if message.command[1].isdigit() else 10
|
|
query = ' '.join(message.command[2:])
|
|
|
|
# Update status
|
|
status_message = await message.edit('Searching for images...', parse_mode=enums.ParseMode.MARKDOWN)
|
|
|
|
url = f'{API_URL}{query}'
|
|
response = requests.get(url, timeout=10)
|
|
|
|
if response.status_code == 200:
|
|
data = response.json()
|
|
if data.get('status'):
|
|
urls = [item['images_url'] for item in data.get('BK9', [])[:num_pics]]
|
|
images = [download_image(img_url) for img_url in urls]
|
|
|
|
# Download images
|
|
downloaded_images = await asyncio.gather(*images)
|
|
|
|
media = [InputMediaPhoto(media=img_bytes) for img_bytes in downloaded_images if img_bytes]
|
|
|
|
if media:
|
|
await status_message.edit('Uploading pictures...', parse_mode=enums.ParseMode.MARKDOWN)
|
|
while media:
|
|
batch = media[:10]
|
|
media = media[10:]
|
|
await message.reply_media_group(batch)
|
|
await status_message.delete() # Delete status message after uploading
|
|
else:
|
|
await status_message.edit('No valid images found.', parse_mode=enums.ParseMode.MARKDOWN)
|
|
else:
|
|
await status_message.edit(
|
|
'No images found for the given query.',
|
|
parse_mode=enums.ParseMode.MARKDOWN,
|
|
)
|
|
else:
|
|
await status_message.edit(
|
|
'An error occurred, please try again later.',
|
|
parse_mode=enums.ParseMode.MARKDOWN,
|
|
)
|
|
|
|
|
|
modules_help['pinterest'] = {
|
|
'pinterest [number]* [query]': 'Get images from Pinterest. Default number of images is 10',
|
|
}
|