-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #98 from akarshghildyal/horoscope
feat: horoscope in api programs
- Loading branch information
Showing
2 changed files
with
40 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import streamlit as st | ||
import requests | ||
from bs4 import BeautifulSoup | ||
|
||
def horoscope(): | ||
def get_horoscope_by_day(zodiac_sign: int, day: str): | ||
try: | ||
if not "-" in day: | ||
res = requests.get(f"https://www.horoscope.com/us/horoscopes/general/horoscope-general-daily-{day}.aspx?sign={zodiac_sign}") | ||
else: | ||
day = day.replace("-", "") | ||
res = requests.get(f"https://www.horoscope.com/us/horoscopes/general/horoscope-archive.aspx?sign={zodiac_sign}&laDate={day}") | ||
|
||
soup = BeautifulSoup(res.content, 'html.parser') | ||
data = soup.find('div', attrs={'class': 'main-horoscope'}) | ||
return data.p.text, None | ||
except Exception as e: | ||
return None, str(e) | ||
|
||
zodiac_signs = { | ||
"Aries": 1, "Taurus": 2, "Gemini": 3, "Cancer": 4, "Leo": 5, "Virgo": 6, "Libra": 7, "Scorpio": 8, "Sagittarius": 9, "Capricorn": 10, "Aquarius": 11, "Pisces": 12 | ||
} | ||
|
||
st.title("Daily Horoscope") | ||
|
||
sign = st.selectbox("Select your Zodiac sign", options=list(zodiac_signs.keys())) | ||
day = st.selectbox("Select the day", options=["today", "yesterday", "tomorrow"]) | ||
convert_button = st.button(label="Get Horoscope") | ||
|
||
if convert_button: | ||
zodiac_sign = zodiac_signs[sign] | ||
horoscope_text, error = get_horoscope_by_day(zodiac_sign, day) | ||
if horoscope_text: | ||
st.success(horoscope_text) | ||
else: | ||
st.error(f"Could not retrieve horoscope data: {error}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters