Drag commited on
Commit
946b2f2
·
1 Parent(s): fc17ccc

Update search.py

Browse files
Files changed (1) hide show
  1. Mikobot/plugins/search.py +37 -20
Mikobot/plugins/search.py CHANGED
@@ -10,7 +10,9 @@ from pyrogram.types import InputMediaPhoto, Message
10
 
11
  from Mikobot import app
12
  from Mikobot.state import state
 
13
 
 
14
  # <======================================== CONFIGURATION =========================================================>
15
  # Centralized configuration for API settings
16
  API_CONFIG = {
@@ -132,32 +134,47 @@ async def bingimg_search(client: Client, message: Message):
132
 
133
 
134
  @app.on_message(filters.command("googleimg"))
135
- async def googleimg_search(client: Client, message: Message):
 
136
  try:
137
- text = message.text.split(None, 1)[1]
138
- except IndexError:
139
- return await message.reply_text("Provide me a query to search!")
 
140
 
141
- search_message = await message.reply_text("💭")
 
142
 
143
- api_url = API_CONFIG["google_image_api"]["url"].format(
144
- query=text, api_key=API_CONFIG["google_image_api"]["api_key"]
145
- )
146
- response = await state.get(api_url)
 
147
 
148
- if response.status_code == 200:
149
- images = response.json().get("items", [])
150
- if not images:
151
- await message.reply_text("No images found.")
152
- return
153
 
154
- media = [InputMediaPhoto(media=img["link"]) for img in images[:7]]
155
- await message.reply_media_group(media=media)
156
- else:
157
- await message.reply_text("An error occurred while searching for images.")
158
 
159
- await search_message.delete()
160
- await message.delete()
 
 
 
 
 
 
 
 
 
 
 
 
 
161
 
162
  # <=======================================================================================================>
163
 
 
10
 
11
  from Mikobot import app
12
  from Mikobot.state import state
13
+ import logging
14
 
15
+ logging.basicConfig(level=logging.DEBUG)
16
  # <======================================== CONFIGURATION =========================================================>
17
  # Centralized configuration for API settings
18
  API_CONFIG = {
 
134
 
135
 
136
  @app.on_message(filters.command("googleimg"))
137
+ async def googleimg_search(_, message: Message):
138
+ logging.debug("Google Image Search command invoked")
139
  try:
140
+ query = message.text.split(None, 1)[1].strip() if len(message.text.split()) > 1 else None
141
+ if not query:
142
+ logging.debug("No query provided")
143
+ return await message.reply_text("Please provide a search query!")
144
 
145
+ logging.debug(f"Query: {query}")
146
+ search_message = await message.reply_text("💭 Searching for images...")
147
 
148
+ api_url = (
149
+ f"https://www.googleapis.com/customsearch/v1"
150
+ f"?q={query}&searchType=image&cx=YOUR_CSE_ID&key=YOUR_API_KEY&num=7"
151
+ )
152
+ logging.debug(f"API URL: {api_url}")
153
 
154
+ async with aiohttp.ClientSession() as session:
155
+ async with session.get(api_url) as response:
156
+ logging.debug(f"API Response Status: {response.status}")
157
+ if response.status != 200:
158
+ return await message.reply_text(f"Error: {response.status}")
159
 
160
+ data = await response.json()
161
+ logging.debug(f"API Response: {data}")
 
 
162
 
163
+ items = data.get("items", [])
164
+ if not items:
165
+ return await message.reply_text("No images found.")
166
+
167
+ media = [InputMediaPhoto(item["link"]) for item in items[:7]]
168
+ await message.reply_media_group(media=media)
169
+
170
+ await search_message.delete()
171
+
172
+ except IndexError:
173
+ logging.exception("IndexError: No query provided")
174
+ await message.reply_text("Provide me a query to search!")
175
+ except Exception as e:
176
+ logging.exception(f"Exception occurred: {str(e)}")
177
+ await message.reply_text(f"An error occurred: {str(e)}")
178
 
179
  # <=======================================================================================================>
180