rtik007 commited on
Commit
7912d4a
Β·
verified Β·
1 Parent(s): bda4584

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +118 -0
app.py ADDED
@@ -0,0 +1,118 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ from bs4 import BeautifulSoup
4
+ import random
5
+ from datetime import datetime
6
+
7
+ signs = [
8
+ 'aries', 'taurus', 'gemini', 'cancer', 'leo', 'virgo',
9
+ 'libra', 'scorpio', 'sagittarius', 'capricorn', 'aquarius', 'pisces'
10
+ ]
11
+
12
+ def get_trending_news():
13
+ """Fetch and return trending news headlines."""
14
+ url = "http://newsapi.org/v2/top-headlines?country=us&apiKey=a37ab6843a934a16aea7f8bbeb83cd96"
15
+ try:
16
+ page = requests.get(url).json()
17
+ articles = page["articles"]
18
+ results = [ar["title"] for ar in articles[:25]]
19
+ return "\n".join(f"{i+1}. {title}" for i, title in enumerate(results))
20
+ except:
21
+ return "Could not fetch trending news at this time."
22
+
23
+ def get_horoscope(sign_name):
24
+ """Fetch and return horoscope for the specified sign."""
25
+ headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}
26
+ sign_ids = {sign.lower(): idx+1 for idx, sign in enumerate(signs)}
27
+ sign_id = sign_ids.get(sign_name.lower())
28
+
29
+ if not sign_id:
30
+ return "Sign not found."
31
+
32
+ try:
33
+ url = f'https://www.horoscope.com/us/horoscopes/general/horoscope-general-daily-today.aspx?sign={sign_id}'
34
+ res = requests.get(url, headers=headers)
35
+ soup = BeautifulSoup(res.text, 'html.parser')
36
+ horoscope_text = soup.select('.main-horoscope p')[0].getText().strip().capitalize()
37
+ sign = soup.select('h1')[0].getText().strip().capitalize()
38
+ return f"{sign} Horoscope: {horoscope_text}"
39
+ except:
40
+ return "Could not fetch horoscope at this time."
41
+
42
+ def get_history_today():
43
+ """Fetch and return historical events that happened on this day."""
44
+ try:
45
+ url = 'https://www.onthisday.com/'
46
+ res = requests.get(url)
47
+ soup = BeautifulSoup(res.text, 'html.parser')
48
+ history_list = soup.select('.event')[:3]
49
+ return "\n".join(event.getText().strip() for event in history_list)
50
+ except:
51
+ return "Could not fetch historical events at this time."
52
+
53
+ def create_section(title, content, color):
54
+ """Helper function to create styled HTML sections"""
55
+ return f"""
56
+ <div style="
57
+ background: {color};
58
+ padding: 20px;
59
+ border-radius: 10px;
60
+ margin: 10px 0;
61
+ box-shadow: 0 2px 4px rgba(0,0,0,0.1);
62
+ ">
63
+ <h3 style="margin-top: 0; color: #333;">{title}</h3>
64
+ <div style="color: #000; line-height: 1.6;">{content.replace('\n', '<br>')}</div>
65
+ </div>
66
+ """
67
+
68
+ def update_all(sign):
69
+ """Main function to update all components"""
70
+ colors = [
71
+ "#E4E0E1", "#FFDDC1", "#D4E157", "#81D4FA",
72
+ "#FFAB91", "#A5D6A7", "#FFF59D", "#CE93D8",
73
+ "#B39DDB", "#90CAF9", "#FFE082", "#FFCCBC"
74
+ ]
75
+
76
+ current_date = datetime.now().strftime("%d %b %Y")
77
+ news = create_section("πŸ“° Trending News", get_trending_news(), random.choice(colors))
78
+ horoscope = create_section("✨ Horoscope", get_horoscope(sign), random.choice(colors))
79
+ history = create_section("πŸ“… On This Day", get_history_today(), random.choice(colors))
80
+
81
+ return f"# 🌟 Daily Update: {current_date}", news, horoscope, history
82
+
83
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
84
+ current_date = datetime.now().strftime("%d %b %Y")
85
+ title = gr.Markdown(f"# 🌟 Daily Update: {current_date}")
86
+
87
+ with gr.Row():
88
+ sign_dropdown = gr.Dropdown(
89
+ choices=signs,
90
+ value="aries",
91
+ label="Select Your Zodiac Sign",
92
+ interactive=True
93
+ )
94
+ refresh_btn = gr.Button("πŸ”„ Refresh", variant="primary")
95
+
96
+ news_html = gr.HTML()
97
+ horoscope_html = gr.HTML()
98
+ history_html = gr.HTML()
99
+
100
+ # Event handlers
101
+ demo.load(
102
+ fn=update_all,
103
+ inputs=sign_dropdown,
104
+ outputs=[title, news_html, horoscope_html, history_html]
105
+ )
106
+ sign_dropdown.change(
107
+ fn=update_all,
108
+ inputs=sign_dropdown,
109
+ outputs=[title, news_html, horoscope_html, history_html]
110
+ )
111
+ refresh_btn.click(
112
+ fn=update_all,
113
+ inputs=sign_dropdown,
114
+ outputs=[title, news_html, horoscope_html, history_html]
115
+ )
116
+
117
+ if __name__ == "__main__":
118
+ demo.launch()