File size: 4,566 Bytes
7912d4a
 
 
 
 
ed65f5e
 
 
7912d4a
 
 
 
 
 
 
5060941
ebce44a
7912d4a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0506136
 
7912d4a
 
 
 
 
 
 
 
 
0506136
7912d4a
 
 
 
 
 
 
 
 
 
 
 
5060941
7912d4a
 
 
 
 
 
 
 
9e5fcb6
7912d4a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import gradio as gr
import requests
from bs4 import BeautifulSoup
import random
from datetime import datetime
import os

HF_API_KEY = os.getenv("NewsAppToken")

signs = [
    'aries', 'taurus', 'gemini', 'cancer', 'leo', 'virgo',
    'libra', 'scorpio', 'sagittarius', 'capricorn', 'aquarius', 'pisces'
]

def get_trending_news():
    """Fetch and return trending Technology news headlines."""
    url = f"http://newsapi.org/v2/top-headlines?country=us&category=technology&apiKey={HF_API_KEY}"
    try:
        page = requests.get(url).json()
        articles = page["articles"]
        results = [ar["title"] for ar in articles[:25]]
        return "\n".join(f"{i+1}. {title}" for i, title in enumerate(results))
    except:
        return "Could not fetch trending news at this time."

def get_horoscope(sign_name):
    """Fetch and return horoscope for the specified sign."""
    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'}
    sign_ids = {sign.lower(): idx+1 for idx, sign in enumerate(signs)}
    sign_id = sign_ids.get(sign_name.lower())
    
    if not sign_id:
        return "Sign not found."
    
    try:
        url = f'https://www.horoscope.com/us/horoscopes/general/horoscope-general-daily-today.aspx?sign={sign_id}'
        res = requests.get(url, headers=headers)
        soup = BeautifulSoup(res.text, 'html.parser')
        horoscope_text = soup.select('.main-horoscope p')[0].getText().strip().capitalize()
        sign = soup.select('h1')[0].getText().strip().capitalize()
        return f"{sign} Horoscope: {horoscope_text}"
    except:
        return "Could not fetch horoscope at this time."

def get_history_today():
    """Fetch and return historical events that happened on this day."""
    try:
        url = 'https://www.onthisday.com/'
        res = requests.get(url)
        soup = BeautifulSoup(res.text, 'html.parser')
        history_list = soup.select('.event')[:3]
        return "\n".join(event.getText().strip() for event in history_list)
    except:
        return "Could not fetch historical events at this time."

def create_section(title, content, color):
    """Helper function to create styled HTML sections"""
    # Replace newlines with HTML line breaks first
    formatted_content = content.replace('\n', '<br>')
    return f"""
    <div style="
        background: {color};
        padding: 20px;
        border-radius: 10px;
        margin: 10px 0;
        box-shadow: 0 2px 4px rgba(0,0,0,0.1);
    ">
        <h3 style="margin-top: 0; color: #333;">{title}</h3>
        <div style="color: #000; line-height: 1.6;">{formatted_content}</div>
    </div>
    """

def update_all(sign):
    """Main function to update all components"""
    colors = [
        "#E4E0E1", "#FFDDC1", "#D4E157", "#81D4FA",
        "#FFAB91", "#A5D6A7", "#FFF59D", "#CE93D8",
        "#B39DDB", "#90CAF9", "#FFE082", "#FFCCBC"
    ]
    
    current_date = datetime.now().strftime("%d %b %Y")
    news = create_section("📰 Trending Tech News", get_trending_news(), random.choice(colors))
    horoscope = create_section("✨ Horoscope", get_horoscope(sign), random.choice(colors))
    history = create_section("📅 On This Day", get_history_today(), random.choice(colors))
    
    return f"# 🌟 Daily Update: {current_date}", news, horoscope, history

with gr.Blocks(theme=gr.themes.Soft()) as demo:
    current_date = datetime.now().strftime("%d %b %Y")
    title = gr.Markdown(f"# 🌟 Daily Update: {current_date}")
    tagline = gr.Markdown("## Stay connected effortlessly—get top news, historical highlights, and astrological insights, all in one place, every morning!")
    
    with gr.Row():
        sign_dropdown = gr.Dropdown(
            choices=signs,
            value="aries",
            label="Select Your Zodiac Sign",
            interactive=True
        )
        refresh_btn = gr.Button("🔄 Refresh", variant="primary")
    
    news_html = gr.HTML()
    horoscope_html = gr.HTML()
    history_html = gr.HTML()

    # Event handlers
    demo.load(
        fn=update_all,
        inputs=sign_dropdown,
        outputs=[title, news_html, horoscope_html, history_html]
    )
    sign_dropdown.change(
        fn=update_all,
        inputs=sign_dropdown,
        outputs=[title, news_html, horoscope_html, history_html]
    )
    refresh_btn.click(
        fn=update_all,
        inputs=sign_dropdown,
        outputs=[title, news_html, horoscope_html, history_html]
    )

if __name__ == "__main__":
    demo.launch()