trendingnews / app.py
dindizz's picture
Update app.py
b3fa525 verified
import gradio as gr
import requests
import xml.etree.ElementTree as ET
# RSS feed links for various states and India
RSS_FEEDS = {
"India": "https://trends.google.com/trending/rss?geo=IN",
"Tamil Nadu": "https://trends.google.com/trending/rss?geo=IN-TN",
"Karnataka": "https://trends.google.com/trending/rss?geo=IN-KA",
"Andhra Pradesh": "https://trends.google.com/trending/rss?geo=IN-AP",
"Telangana": "https://trends.google.com/trending/rss?geo=IN-TG",
"Delhi": "https://trends.google.com/trending/rss?geo=IN-DL",
"Maharashtra": "https://trends.google.com/trending/rss?geo=IN-MH",
"West Bengal": "https://trends.google.com/trending/rss?geo=IN-WB",
"Uttar Pradesh": "https://trends.google.com/trending/rss?geo=IN-UP",
"Kerala": "https://trends.google.com/trending/rss?geo=IN-KL",
"Rajasthan": "https://trends.google.com/trending/rss?geo=IN-RJ",
"Gujarat": "https://trends.google.com/trending/rss?geo=IN-GJ",
"Punjab": "https://trends.google.com/trending/rss?geo=IN-PB",
"Haryana": "https://trends.google.com/trending/rss?geo=IN-HR",
"Bihar": "https://trends.google.com/trending/rss?geo=IN-BR",
"Madhya Pradesh": "https://trends.google.com/trending/rss?geo=IN-MP",
"Chhattisgarh": "https://trends.google.com/trending/rss?geo=IN-CT",
"Odisha": "https://trends.google.com/trending/rss?geo=IN-OR",
"Assam": "https://trends.google.com/trending/rss?geo=IN-AS",
"Jharkhand": "https://trends.google.com/trending/rss?geo=IN-JH",
"Himachal Pradesh": "https://trends.google.com/trending/rss?geo=IN-HP",
"Uttarakhand": "https://trends.google.com/trending/rss?geo=IN-UT",
"Goa": "https://trends.google.com/trending/rss?geo=IN-GA",
"Jammu and Kashmir": "https://trends.google.com/trending/rss?geo=IN-JK",
"Tripura": "https://trends.google.com/trending/rss?geo=IN-TR",
"Meghalaya": "https://trends.google.com/trending/rss?geo=IN-ML",
"Manipur": "https://trends.google.com/trending/rss?geo=IN-MN",
"Nagaland": "https://trends.google.com/trending/rss?geo=IN-NL",
"Arunachal Pradesh": "https://trends.google.com/trending/rss?geo=IN-AR",
"Mizoram": "https://trends.google.com/trending/rss?geo=IN-MZ",
"Sikkim": "https://trends.google.com/trending/rss?geo=IN-SK"
}
def fetch_trending(region: str):
try:
# Get the RSS feed URL for the selected region
url = RSS_FEEDS[region]
# Fetch and parse the RSS feed
response = requests.get(url)
response.raise_for_status()
tree = ET.ElementTree(ET.fromstring(response.content))
root = tree.getroot()
# Extract trending titles
trending_titles = [
f"{index + 1}. {item.find('title').text}"
for index, item in enumerate(root.findall(".//item"))
if item.find("title") is not None
]
# Format the output
return "\n".join(trending_titles) if trending_titles else "No trending topics found."
except Exception as e:
return f"Error fetching trends for {region}: {str(e)}"
# Define Gradio interface
dropdown = gr.Dropdown(
label="Select State/Region",
choices=list(RSS_FEEDS.keys()),
value="India" # Default selection
)
output = gr.Textbox(
label="Trending Now",
lines=20
)
iface = gr.Interface(
fn=fetch_trending,
inputs=dropdown,
outputs=output,
title="Google Trends - Trending Now",
description="Select a state/region to view the trending topics. The data is sourced from Google Trends RSS feeds."
)
# Launch the Gradio app
iface.launch()