-
Notifications
You must be signed in to change notification settings - Fork 4
/
pinterest.py
81 lines (68 loc) · 2.99 KB
/
pinterest.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
from pyrogram import Client, filters, enums
from pyrogram.types import Message, InputMediaPhoto
from io import BytesIO
from PIL import Image
import requests
import asyncio
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)
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)
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",
}