File size: 2,592 Bytes
e69f359
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f722cc4
e69f359
 
f722cc4
31b03a2
ce68558
025533e
e69f359
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f722cc4
e69f359
f722cc4
 
e69f359
 
 
 
f722cc4
 
e69f359
 
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
import gradio as gr
import feedparser
from datetime import datetime, timedelta
import time

# Fetch and sort agri news (last 1 year)
def fetch_all_hindi_agri_news():
    query = "कृषि"
    rss_url = f"https://news.google.com/rss/search?q={query}&hl=hi-IN&gl=IN&ceid=IN:hi"
    feed = feedparser.parse(rss_url)
    now = datetime.now()
    one_year_ago = now - timedelta(days=365)

    news_items = []
    for entry in feed.entries:
        try:
            published_time = datetime.fromtimestamp(time.mktime(entry.published_parsed))
        except:
            continue

        if published_time >= one_year_ago:
            news_items.append({
                "title": entry.title,
                "link": entry.link,
                "published_dt": published_time,
                "published": published_time.strftime("%Y-%m-%d %H:%M")
            })

    news_items.sort(key=lambda x: x["published_dt"], reverse=True)
    return news_items

# Render 20 news items
def render_news(news_items, start, count=20):
    sliced = news_items[start:start+count]
    if not sliced:
        return "<p>✅ आपने सभी समाचार देख लिए हैं।</p>"
    result = ""
    for i, item in enumerate(sliced, start + 1):
        result += f"<strong>{i}. {item['title']}</strong><br>\n"
        result += f"<a href='{item['link']}' target='_self'>🔗 समाचार लिंक</a><br>\n"
        result += f"🗓️ प्रकाशित: {item['published']}<br>\n"
        result += f"<hr style='margin: 10px 0;' />\n"
    return result

# Load initial news
def load_initial_news():
    news = fetch_all_hindi_agri_news()
    display = render_news(news, 0)
    return display, news, 20

# Load more news
def load_more_news(news_state, current_index):
    display = render_news(news_state, current_index)
    js_scroll_top = "<script>window.scrollTo(0, 0);</script>"
    return display + js_scroll_top, current_index + 20

# Gradio App
with gr.Blocks(title="निंजा किसान कृषि समाचार") as demo:
    gr.HTML("<h1 style='text-align: center;'>निंजा किसान कृषि समाचार</h1>", elem_id="title")

    output_html = gr.HTML()
    load_more_bottom = gr.Button("🔄 और समाचार देखें")

    news_state = gr.State([])
    news_index = gr.State(0)

    demo.load(fn=load_initial_news, outputs=[output_html, news_state, news_index])
    load_more_bottom.click(fn=load_more_news, inputs=[news_state, news_index], outputs=[output_html, news_index])

demo.launch()