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', '
') return f"""

{title}

{formatted_content}
""" 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()