Drag commited on
Commit
76660cb
Β·
1 Parent(s): f8ea3ca

Update search.py

Browse files
Files changed (1) hide show
  1. 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
- # Log the constructed URL (for debugging)
44
- print(f"Requesting URL: {api_url}")
45
-
46
- # Send the request to the API
47
- response = await state.get(api_url)
48
- print(f"Response Status Code: {response.status_code}") # Log status code
49
-
50
- if response.status_code != 200:
51
- await message.reply_text(f"API Error: {response.status_code}")
52
- return
53
-
54
- news_data = response.json()
55
- print(f"Response JSON: {news_data}") # Log the JSON response
56
-
57
- if news_data.get("status") == "ok":
58
- articles = news_data.get("articles", [])
59
- if articles:
60
- news_item = random.choice(articles)
61
- title = news_item.get("title", "No title")
62
- excerpt = news_item.get("description", "No description")
63
- source = news_item.get("source", {}).get("name", "Unknown source")
64
- relative_time = news_item.get("publishedAt", "Unknown time")
65
- news_url = news_item.get("url", "#")
66
-
67
- message_text = f"π—§π—œπ—§π—Ÿπ—˜: {title}\n𝗦𝗒𝗨π—₯π—–π—˜: {source}\nπ—§π—œπ— π—˜: {relative_time}\nπ—˜π—«π—–π—˜π—₯𝗣𝗧: {excerpt}\n𝗨π—₯π—Ÿ: {news_url}"
68
- await message.reply_text(message_text)
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