Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
import json
|
3 |
+
|
4 |
+
def search_news(query, num_results=10):
|
5 |
+
# SearXNG instance URL (you may need to replace this with a working instance)
|
6 |
+
searxng_url = "https://searx.be/search"
|
7 |
+
|
8 |
+
# Parameters for the search request
|
9 |
+
params = {
|
10 |
+
"q": query,
|
11 |
+
"categories": "news",
|
12 |
+
"format": "json",
|
13 |
+
"lang": "en",
|
14 |
+
"pageno": 1,
|
15 |
+
"time_range": "None",
|
16 |
+
"engines": "google_news,bing_news,yahoo_news",
|
17 |
+
"results": num_results
|
18 |
+
}
|
19 |
+
|
20 |
+
try:
|
21 |
+
# Send the request to SearXNG
|
22 |
+
response = requests.get(searxng_url, params=params)
|
23 |
+
response.raise_for_status() # Raise an exception for bad status codes
|
24 |
+
|
25 |
+
# Parse the JSON response
|
26 |
+
results = response.json()
|
27 |
+
|
28 |
+
# Extract and return the news items
|
29 |
+
news_items = results.get("results", [])
|
30 |
+
return news_items
|
31 |
+
|
32 |
+
except requests.RequestException as e:
|
33 |
+
print(f"An error occurred: {e}")
|
34 |
+
return []
|
35 |
+
|
36 |
+
def display_news(news_items):
|
37 |
+
for i, item in enumerate(news_items, 1):
|
38 |
+
print(f"\n{i}. {item['title']}")
|
39 |
+
print(f" URL: {item['url']}")
|
40 |
+
print(f" Published: {item.get('publishedDate', 'N/A')}")
|
41 |
+
print(f" Content: {item.get('content', 'N/A')[:150]}...")
|
42 |
+
|
43 |
+
if __name__ == "__main__":
|
44 |
+
search_query = input("Enter a news topic to search for: ")
|
45 |
+
news_results = search_news(search_query)
|
46 |
+
|
47 |
+
if news_results:
|
48 |
+
print(f"\nFound {len(news_results)} news items:")
|
49 |
+
display_news(news_results)
|
50 |
+
else:
|
51 |
+
print("No news items found or an error occurred.")
|