Drag
commited on
Commit
Β·
76660cb
1
Parent(s):
f8ea3ca
Update search.py
Browse files- Mikobot/plugins/search.py +27 -31
Mikobot/plugins/search.py
CHANGED
@@ -3,6 +3,7 @@
|
|
3 |
# <============================================== IMPORTS =========================================================>
|
4 |
import json
|
5 |
import random
|
|
|
6 |
|
7 |
from pyrogram import Client, filters
|
8 |
from pyrogram.types import InputMediaPhoto, Message
|
@@ -40,38 +41,33 @@ async def news(_, message: Message):
|
|
40 |
api_url = API_CONFIG["news_api"]["url"].format(keyword, api_key=API_CONFIG["news_api"]["api_key"])
|
41 |
|
42 |
try:
|
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 |
-
else:
|
70 |
-
await message.reply_text("No news found.")
|
71 |
-
else:
|
72 |
-
await message.reply_text(f"API Response Error: {news_data.get('message', 'Unknown error')}")
|
73 |
except Exception as e:
|
74 |
-
# Log the error for debugging
|
75 |
print(f"Exception occurred: {e}")
|
76 |
await message.reply_text(f"An error occurred: {str(e)}")
|
77 |
|
|
|
3 |
# <============================================== IMPORTS =========================================================>
|
4 |
import json
|
5 |
import random
|
6 |
+
import aiohttp
|
7 |
|
8 |
from pyrogram import Client, filters
|
9 |
from pyrogram.types import InputMediaPhoto, Message
|
|
|
41 |
api_url = API_CONFIG["news_api"]["url"].format(keyword, api_key=API_CONFIG["news_api"]["api_key"])
|
42 |
|
43 |
try:
|
44 |
+
async with aiohttp.ClientSession() as session:
|
45 |
+
async with session.get(api_url) as response:
|
46 |
+
print(f"Response Status Code: {response.status}")
|
47 |
+
if response.status != 200:
|
48 |
+
await message.reply_text(f"API Error: {response.status}")
|
49 |
+
return
|
50 |
+
|
51 |
+
news_data = await response.json()
|
52 |
+
print(f"Response JSON: {news_data}")
|
53 |
+
|
54 |
+
if news_data.get("status") == "ok":
|
55 |
+
articles = news_data.get("articles", [])
|
56 |
+
if articles:
|
57 |
+
news_item = random.choice(articles)
|
58 |
+
title = news_item.get("title", "No title")
|
59 |
+
excerpt = news_item.get("description", "No description")
|
60 |
+
source = news_item.get("source", {}).get("name", "Unknown source")
|
61 |
+
relative_time = news_item.get("publishedAt", "Unknown time")
|
62 |
+
news_url = news_item.get("url", "#")
|
63 |
+
|
64 |
+
message_text = f"π§ππ§ππ: {title}\nπ¦π’π¨π₯ππ: {source}\nπ§ππ π: {relative_time}\nππ«πππ₯π£π§: {excerpt}\nπ¨π₯π: {news_url}"
|
65 |
+
await message.reply_text(message_text)
|
66 |
+
else:
|
67 |
+
await message.reply_text("No news found.")
|
68 |
+
else:
|
69 |
+
await message.reply_text(f"API Response Error: {news_data.get('message', 'Unknown error')}")
|
|
|
|
|
|
|
|
|
70 |
except Exception as e:
|
|
|
71 |
print(f"Exception occurred: {e}")
|
72 |
await message.reply_text(f"An error occurred: {str(e)}")
|
73 |
|