File size: 3,599 Bytes
d4fd01a
 
 
 
b3fa525
7dbaaf8
b3fa525
4e5caee
 
 
 
 
b3fa525
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7dbaaf8
 
 
d4fd01a
7dbaaf8
 
 
 
d4fd01a
 
 
 
 
7dbaaf8
4e5caee
 
 
 
 
d4fd01a
7dbaaf8
 
d4fd01a
7dbaaf8
d4fd01a
 
7dbaaf8
b3fa525
7dbaaf8
b3fa525
7dbaaf8
 
 
 
 
 
 
d4fd01a
7dbaaf8
 
 
b3fa525
 
d4fd01a
 
 
 
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
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()