File size: 2,871 Bytes
f02770e
409c813
 
 
23d2217
409c813
 
 
 
 
 
 
 
 
617c3a2
c969677
 
 
 
 
409c813
 
 
 
 
 
 
 
c969677
 
409c813
c969677
409c813
 
 
 
 
c969677
409c813
c969677
409c813
 
c969677
 
409c813
 
00536e4
c969677
409c813
 
 
617c3a2
409c813
 
 
 
 
c969677
 
 
 
 
 
 
 
 
409c813
 
c969677
409c813
 
c969677
 
 
 
 
f02770e
 
409c813
f02770e
c969677
409c813
f02770e
 
409c813
c969677
f02770e
 
409c813
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
82
83
84
85
86
87
88
89
import gradio as gr
import requests
import json
import random

# List of SearXNG instances to try
SEARXNG_INSTANCES = [
    "https://search.inetol.net",
    "https://northboot.xyz",
    "https://search.hbubli.cc",
    "https://searx.tiekoetter.com",
    "https://search.bus-hit.me",
    # Add more instances here
]

import logging

# Configure logging
logging.basicConfig(level=logging.INFO)

def search_news(query, num_results=10):
    random.shuffle(SEARXNG_INSTANCES)
    
    for searxng_url in SEARXNG_INSTANCES:
        params = {
            "q": query,
            "categories": "news",
            "format": "json",
            "language": "en",
            "page": 1,
            "engines": "google_news,bing_news,yahoo_news",
            "count": num_results
        }
        try:
            response = requests.get(f"{searxng_url}/search", params=params, timeout=10)
            response.raise_for_status()
            results = response.json()
            news_items = results.get("news", [])
            if news_items:
                logging.info(f"Success from instance: {searxng_url}")
                return news_items, None
        except requests.RequestException as e:
            logging.warning(f"Instance {searxng_url} failed: {e}")
            continue
    
    return [], "Unable to fetch results from any SearXNG instance. Please try again later."


def format_news(news_items, error=None):
    if error:
        return f"Error: {error}"
    
    if not news_items:
        return "No news items found."
    
    formatted_results = ""
    for i, item in enumerate(news_items, 1):
        title = item.get('title', 'No Title')
        url = item.get('url', 'No URL')
        published_date = item.get('published', 'N/A')  # Adjusted field name
        content = item.get('content', 'N/A')
        
        formatted_results += f"{i}. {title}\n"
        formatted_results += f"   URL: {url}\n"
        formatted_results += f"   Published: {published_date}\n"
        formatted_results += f"   Content: {content[:150]}...\n\n"
    return formatted_results


def gradio_search_news(query, num_results):
    news_items, error = search_news(query, int(num_results))
    if news_items:
        # Optionally include instance information in the response
        return format_news(news_items, error)
    else:
        return format_news(news_items, error)

iface = gr.Interface(
    fn=gradio_search_news,
    inputs=[
        gr.Textbox(label="Enter a news topic to search for", placeholder="e.g., Artificial Intelligence"),
        gr.Slider(minimum=1, maximum=20, value=10, step=1, label="Number of results")
    ],
    outputs=gr.Textbox(label="Search Results", lines=20),
    title="SearXNG News Search",
    description="Search for news articles using the SearXNG metasearch engine. If one instance fails, it will try others."
)

iface.launch()