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"""